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
statements |
Function (b) |
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- 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
Comments