You are on page 1of 10

C Programming is an ANSI/ISO standard and powerful programming language for developing real

time applications. C programming language was invented by Dennis Ritchie at the Bell Laboratories
in 1972. It was invented for implementing UNIX operating system. C is most widely used
programming language even today. All other programming languages were derived directly or
indirectly from C programming concepts.

 The C programming language is a structure oriented programming language, developed


at Bell Laboratories in 1972 by Dennis Ritchie
 C programming language features were derived from an earlier language called “B”
(Basic Combined Programming Language – BCPL)
 C language was invented for implementing UNIX operating system
 In 1978, Dennis Ritchie and Brian Kernighan published the first edition “The C
Programming Language” and commonly known as K&R C
 In 1983, the American National Standards Institute (ANSI) established a committee to
provide a modern, comprehensive definition of C. The resulting definition, the ANSI
standard, or “ANSI C”, was completed late 1988.

 . C programming basics to write a C


Program:
 Below are few commands and syntax used in C programming to write a simple C
program. Let’s see all the sections of a simple C program line by line.

C Basic commands Explanation


This is a preprocessor command that includes standard input output
#include <stdio.h>
header file(stdio.h) from the C library before compiling a C program
This is the main function from where execution of any C program
int main()
begins.
{ This indicates the beginning of the main function.
whatever is given inside the command “/* */” in any C program,
/*_some_comments_*/
won’t be considered for compilation and execution.
printf(“Hello_World!
printf command prints the output onto the screen.
“);
getch(); This command waits for any character input from keyboard.
return 0; This command terminates C program (main function) and returns 0.
} This indicates the end of the main function.

. A simple C Program:
Below C program is a very simple and basic program in C programming language. This C
program displays “Hello World!” in the output window. And, all syntax and commands in C
programming are case sensitive. Also, each statement should be ended with semicolon (;)
which is a statement terminator.

C
#include <stdio.h>
int main()
{
/* Our first simple C basic prog

1 #include <stdio.h>

2 int main()

3{

4 /* Our first simple C basic program */

5 printf("Hello World! ");

6 getch();

7 return 0;

8}

Output:
Hello World!

3. Steps to write C programs and get the


output:
Below are the steps to be followed for any C program to create and get the output. This is
common to all C program and there is no exception whether its a very small C program or very
large C program.

1. Create
2. Compile
3. Execute or Run
4. Get the Output

4. Creation, Compilation and Execution of a


C program:
Prerequisite:

 If you want to create, compile and execute C programs by your own, you have to install C
compiler in your machine. Then, you can start to execute your own C programs in your
machine.
 You can refer below link for how to install C compiler and compile and execute C programs in
your machine.
 Once C compiler is installed in your machine, you can create, compile and execute C programs
as shown in below link.
 If you don’t want to install C/C++ compilers in your machine, you can refer online compilers
which will compile and execute C/C++ and many other programming languages online and
display outputs on the screen. Please search for online C/C++ compilers in Google for more
details.

5. Basic structure of a C program:


Structure of C program is defined by set of rules called protocol, to be followed by programmer
while writing C program. All C programs are having sections/parts which
are mentioned below.

1. Documentation section
2. Link Section
3. Definition Section
4. Global declaration section
5. Function prototype declaration section
6. Main function
7. User defined function definition section

Example C program to compare all the


sections:
You can compare all the sections of a C program with the below C program.

int sum (int a, int b) /* User defin


{
return a + b; /* definition s
}

1 /*

2 Documentation section

3 C programming basics & structure of C programs

4 Author: fresh2refresh.com

5 Date : 01/01/2012

6 */

8 #include <stdio.h> /* Link section */

9 int total = 0; /* Global declaration, definition section */

10 int sum (int, int); /* Function declaration section */


11 int main () /* Main function */

12 {

13 printf ("This is a C basic program \n");

14 total = sum (1, 1);

15 printf ("Sum of two numbers : %d \n", total);

16 return 0;

17 }

18

19 int sum (int a, int b) /* User defined function */

20 {

21 return a + b; /* definition section */

22 }

Output:
This is a C basic program
Sum of two numbers : 2

Description for each section of the C


program:
 Let us see about each section of a C basic program in detail below.
 Please note that a C program mayn’t have all below mentioned sections except main
function and link sections.
 Also, a C program structure mayn’t be in below mentioned order.

Sections Description
We can give comments about the program, creation or modified date,
author name etc in this section. The characters or words or anything
Documentation which are given between “/*” and “*/”, won’t be considered by C
section compiler for compilation process.These will be ignored by C compiler
during compilation.
Example : /* comment line1 comment line2 comment 3 */
Header files that are required to execute a C program are included in
Link Section
this section
In this section, variables are defined and values are set to these
Definition Section
variables.
Global declaration Global variables are defined in this section. When a variable is to be
section used throughout the program, can be defined in this section.
Function prototype Function prototype gives many information about a function like return
declaration section type, parameter names used inside the function.
Every C program is started from main function and this function
Main function contains two major sections called declaration section and executable
section.
User defined User can define their own functions in this section which perform
function section particular task as per the user requirement.

1. printf() function in C language:


 In C programming language, printf() function is used to print the “character, string,
float, integer, octal and hexadecimal values” onto the output screen.
 We use printf() function with %d format specifier to display the value of an integer
variable.
 Similarly %c is used to display character, %f for float variable, %s for string
variable, %lf for double and %x for hexadecimal variable.
 To generate a newline,we use “\n” in C printf() statement.

Example program for C printf() function:


C

#include <stdio.h>
int main()
{
char ch = 'A';

1 #include <stdio.h>

2 int main()

3 {

4 char ch = 'A';

5 char str[20] = "fresh2refresh.com";

6 float flt = 10.234;

7 int no = 150;

8 double dbl = 20.123456;

9 printf("Character is %c \n", ch);

10 printf("String is %s \n" , str);


11 printf("Float value is %f \n", flt);

12 printf("Integer value is %d\n" , no);

13 printf("Double value is %lf \n", dbl);

14 printf("Octal value is %o \n", no);

15 printf("Hexadecimal value is %x \n", no);

16 return 0;

17 }

Output:
Character is A
String is fresh2refresh.com
Float value is 10.234000
Integer value is 150
Double value is 20.123456
Octal value is 226
Hexadecimal value is 96

2. scanf() function in C language:


 In C programming language, scanf() function is used to read character, string, numeric
data from keyboard
 Consider below example program where user enters a character. This value is assigned
to the variable “ch” and then displayed.

 Then, user enters a string and this value is assigned to the variable “str” and then
displayed.

Example program for printf() and scanf()


functions in C programming language:
C
#include <stdio.h>
int main()
{
char ch;

1 #include <stdio.h>
2 int main()
3 {
4 char ch;
5 char str[100];
6 printf("Enter any character \n");
7 scanf("%c", &ch);
8 printf("Entered character is %c \n", ch);
9 printf("Enter any string ( upto 100 character ) \n");
10 scanf("%s", &str);
11 printf("Entered string is %s \n", str);
12 }

Output :
Enter any character
a
Entered character is a
Enter any string ( upto 100 character )
hai
Entered string is hai

KEY POINTS TO REMEMBER IN C


PRINTF() AND SCANF():
1. printf() is used to display the output and scanf() is used to read the inputs.
2. printf() and scanf() functions are declared in “stdio.h” header file in C library.
3. All syntax in C language including printf() and scanf() functions are case sensitive.

Other Inbuilt file handling functions in C


programming language:
C programming language offers many other inbuilt functions for handling files. They are given
below. Please click on each function name below to know more details, example programs,
output for the respective file handling function.

File
Description
handling functions

fopen () fopen () function creates a new file or opens an existing file.

fclose () fclose () function closes an opened file.

getw () getw () function reads an integer from file.

putw () putw () functions writes an integer to file.


fgetc () fgetc () function reads a character from file.

fputc () fputc () functions write a character to file.

gets () gets () function reads line from keyboard.

puts () puts () function writes line to o/p screen.

fgets () fgets () function reads string from a file, one line at a time.

fputs () fputs () function writes string to a file.

feof () feof () function finds end of file.

fgetchar () fgetchar () function reads a character from keyboard.

fprintf () fprintf () function writes formatted data to a file.

fscanf () fscanf () function reads formatted data from a file.

fputchar () function writes a character onto the output screen from keyboard
fputchar ()
input.

fseek () fseek () function moves file pointer position to given location.

SEEK_SET SEEK_SET moves file pointer position to the beginning of the file.

SEEK_CUR SEEK_CUR moves file pointer position to given location.

SEEK_END SEEK_END moves file pointer position to the end of file.

ftell () ftell () function gives current position of file pointer.

rewind () rewind () function moves file pointer position to the beginning of the file.

getc () getc () function reads character from file.

getch () getch () function reads character from keyboard.

getche () getche () function reads character from keyboard and echoes to o/p screen.

getchar () getchar () function reads character from keyboard.

putc () putc () function writes a character to file.

putchar () putchar () function writes a character to screen.

printf () printf () function writes formatted data to screen.

sprinf () sprinf () function writes formatted output to string.


scanf () scanf () function reads formatted data from keyboard.

sscanf () sscanf () function Reads formatted input from a string.

remove () remove () function deletes a file.

fflush () fflush () function flushes a file.

C program example for prime number:


What is prime number?

A number is considered as prime number when it satisfies the below conditions.

 It should be whole number


 It should be greated than 1
 It should have only 2 factors. They are, 1 and the number itself.

Example for prime numbers: 2, 3, 5, 7, 11, 13, 17, 19, 23 etc.

Why 4, 6, 8, 9, 10, 12, 14, 15, 16 etc are not prime numbers?

Because, the number 4 can be factored as 2*2 and 1*4. As per the rule of prime number, there
should be 2 factors only. They are 1 and the number itself. But, number 4 has 2*2 also. Like
this, all remaining numbers 6, 8, 9, 10, 12, 14, 15, 16 have factors other than 1 and the number
itself. So, these are not called as prime numbers.

Example C program for prime number:


C
#include <stdio.h>
int main()
{
int i, num, p = 0;

1 #include <stdio.h>
2 int main()
3 {
4 int i, num, p = 0;
5 printf("Please enter a number: \n");
6 scanf("%d", &num);
7 for(i=1; i<=num; i++)
8 {
9 if(num%i==0)
10 {
11 p++;
12 }
13 }
14 if(p==2)
15 {
16 printf("Entered number is %d "\
17 "and it is a prime number.",num);
18 }
19 else
20 {
21 printf("Entered number is %d "\
22 "and it is not a prime number.",num);
23 }
24 }

Output of C program for prime number:


Please enter a number: 13
Entered number is 13 and it is a prime number.

You might also like