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.
(pre-processor instructions)
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
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:
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.
Most Viewed Articles (in C )
Passing double value to a function in C
Sum of the elements of an array in C
Printing a simple histogram in C
Simple arithmetic calculations in C
Passing pointer to a function in C
Find square and square root for a given number in C
Using memset(), memcpy(), and memmove() in C
Latest Articles (in C)
Simple arithmetic calculations in C
Find square and square root for a given number in C
Printing a simple histogram in C
Sum of the elements of an array in C
Passing pointer to a function in C
Passing double value to a function in C
Infix to Prefix And Postfix in C
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
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate