Writing The First C program
By: Ram
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
statements |
Function (b) |
Archived Comments
1. very useful for beginners
View Tutorial By: sri at 2011-08-01 08:57:10
2. 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
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Sum of the elements of an array in C
Printing a simple histogram in C
Find square and square root for a given number in C
Simple arithmetic calculations 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