Programming Tutorials

Writing The First C program

By: Ram in C Tutorials on 2008-07-08  

Having an idea about the types of variables, constants & keywords, let us write our first C program. All the instructions in C are written as separate statements. Hence, a complete C program would comprise of a series of statements.  

The statements in a program must appear in the same order in which they are to be executed.

Blank spaces could be inserted between two words to improve the readability of the statement. However, no blank spaces are allowed within a variable, constant or keyword.

All statements are entered in small case letters. C has no specific rules for the position at which a statement is to be written. That’s why it is often called a free-form language. Every C statement must end with a ‘;’. Thus ; acts as a statement terminator.  

Let us now write down our first C program. It would simply finds average of 3 numbers  

/* Average of 3 numbers */

main()

{

int a, b,c,tot ;
float avg ;
a = 10 ;
b = 20 ;
c = 30 ;

/* to find total of the 3 given nos */

tot =a+b+c

/*to find average of 3 nos*/

avg= tot/3.0

printf ( "%f" ,avg ) ;

} 
 

Now a few tips about the program...

- Comment about the program should be enclosed within /* */. For example, the first  statement is a comment statement.(However, comments are not absolutely necessary)

- Any number of comments can be written at any place in the program.

- A program with comment statements make it easy to understand.

- main() is a collective name given to a set of statements. All statements that belong to main() are enclosed within a pair of braces { } as shown below.  
 

main()

{

statement 1 ;

statement 2 ;

statement 3 ;

}  
 

- Technically speaking main() is a function. Every function has a pair of parentheses () associated with it.

- Any variable used in the program must be declared before using it. For example,  

int a, b,c,tot ;

float avg ;  

- Any C statement always ends with a ;

For example,

a = 10 ;  

- In the statement,

tot = a + b + c ;

         avg=tot/3.0; 

and / are the arithmetic operators. The arithmetic operators available in C are          +, -, * and /. There are about 45 operators available in C.  

- Once the value of avg is calculated it needs to be displayed on the screen. All output to screen is achieved using built-in library functions. One such function is printf(). We have used it display on the screen the value contained in avg.  

The general form of printf() function is,

printf ( "<format string>", <list of variables> ) ;

<format string> can contain,

%f for printing real values

%d for printing integer values

%c for printing character values  

In addition to format specifiers like %f, %d and %c the format string may also contain any other characters. These characters are printed as they are when the printf() is executed.  

ANATOMY OF C

 
Typical C program

 
# include

(pre-processor instructions)

Int main (void)

(ma (main() is always the first function called)


 

 
      statements                                      (functions are made up of statements)
 
 function (a)

statements


Function (b)






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in C )

Latest Articles (in C)