A ‘C’ program contains executable statements. A compiler helps to translate the executable statements into machine language.
When a user runs the program, he/she machines the language statements which are executed by the compiler.
Types of executable statements
The types of executable statements in C language are as follows −
- Input – output statements
- Assignment statements
Input-output statements
Storing a value into memory is called ‘input operation’.
After executing the computations, the results are stored in memory and the results can be displayed to the user by ‘output operation’.
All i/o operations are performed using input / output functions.
The most common I/O functions are supplied through the preprocessor directive # include<stdio.h>.
The most commonly used I/O functions are printf ( ) and scanf ( ).
printf ( ) function
The syntax is as follows −
printf("format string", print list);
For example,
printf ("average of 3 numbers = %f",avg);
The printf ( ) displays the value of its format string
scanf ( ) function
The syntax is as follows −
scanf ("format string", input list);
For example, scanf ("%d %f", &a, &b);
The scanf ( ) copies data typed at the keyboard into memory during program execution.
The input list is preceded by ampersand ( &).
Assignment statements
The assignment statements store a value in a variable and is used to perform arithmetic operations in a program.
Syntax
The syntax is as follows −
variable=expression
For example,
- c = a+b;
- avg = sum/3;
- r1 = (b*b – 4 * a*c);
Example
Following is the C program for computing an average of three numbers −
#include<stdio.h> #include<stdio.h> main(){ int a,b,c,d; float avg; printf("Enter values for a,b,c:\n"); scanf("%d%d%d",&a,&b,&c);// The scanf ( ) copies data typed at the keyboard into //memory during program execution. d=a+b+c; //assignment stmt avg=d/3; printf("Average avg=%f",avg); }
Output
You will see the following output −
Enter values for a,b,c:2 3 4 Average avg=3.000000