Writing The First C program

By: Ram Emailed: 1728 times Printed: 2329 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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)


C Home | All C Tutorials | Latest C Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(1)


1. View Comment

I really helped with this tutorial.
In case that I must refresh my memory to code with C.


View Tutorial          By: maya at 2008-07-30 00:35:51

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Ram
smskannel SMS gateway run in background
while, do while and for loops in C
Statements in C
Writing The First C program

More Tutorials in C
Sum of the elements of an array in C
Printing a simple histogram in C
Sorting an integer array in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Command-line arguments in C
Calculator in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C
Unicode and UTF-8 in C
Formatting with printf in C
if, if...else and switch statements in C with samples
Statements in C

More Latest News
Most Viewed Articles (in C )
Using memset(), memcpy(), and memmove() in C
goto and labels in C
Find square and square root for a given number in C
Open, Creat, Close, Unlink system calls sample program in C
Using calloc() Function in C
perror() Function - example program in C
Character Arrays in C
scanf and sscanf sample program in C
lseek() sample program in C
Sorting an integer array in C
Arrays sample program in C
register Variables in C
Arrays of Structures example program in C
union example program in C
fgets(), fputs() - Line Input and Output - sample program in C
Most Emailed Articles (in C)
Listing Files and Directories sample program in C
Arithmetic Operators in C
Relational and Logical Operators in C
Increment and Decrement Operators in C
register Variables in C
File Inclusion in C
Pointers and Function Arguments in C
Pointers and Arrays in C
Character Pointers and Functions in C
Pointer Arrays and Pointers to Pointers in C
Multi-dimensional Arrays in C (Explained using date conversion program)
Initialization of Pointer Arrays in C
The Birth and history of C Programming Language
Basics of C
Getting Started with C