Chapter 1.2 - Exemplify the elementary C concepts through sample programs | Part 2


SAMPLE PROGRAM 2: ADDING TWO NUMBERS

                 Chapter 1 - History of C Programming


C concepts through sample programs


Consider another program. Which performs addition on two numbers and displays the result. The complete program is shown above fig.

Addition of two numbers sample program
Fig 4 - Program to add two numbers 
This program when executed will produce the following output. 
                                    100
                                    106.10
The first 4 & 5 lines of the program are comment lines. It is good practice to use comment lines in the beginning to give information such as the name of the program, author, date, etc. Comment characters are also used in other lines to indicate line numbers. 




The words number and amount are variable names that are used to store numeric data. The numeric data may be either in an integer form or in real form. In C, all variables should be declared to tell the compiler what the variables name are and what type of data they hold. The variable must be declared before they are used. In lines 10and 11, the declarations. 
                                     int number;                                                                                                                  float amount;

tell the compiler that number is an integer (int) and the amount is floating (float) point number. Declaration statement must appear at the beginning of function as shown in Fig - 4. All Declaration statements end with a semicolon; C supports many other data types and they are discussed in detail in our Next Chapter. 
The words such as int and float are called the keyword and cannot be used as variable names


Data is stored in a variable by assigning a data value to it. This is done in lines 14 & 16. In line-14, an integer value 100 is assigned to the integer variable number and in line-16, the result of adding two real numbers 30.75 and 75.35 is assigned to the floating point variable amount. The statements 
                                                number = 100;                                                                                                             amount = 30.75 + 75.35;
are called the assignment statements. Every assignment statement must have a semicolon at the end. 
The next statement is an output statement that prints the value of a number. The print statement
                                   printf("%d\n", numbers);  


contains two arguments. The first argument "%d" tells the compiler that the value of the second argument number should be printed as a decimal integer. Note that these arguments are separated by a comma. 
The newline character \n causes the next output to appear on a new line.
The last statement of the program

                                     printf("5.2f", amount);
prints out the value of the amount in floating point format. The format specification %5.2f tells the compiler that the output must be in floating point, with five places in all and two places to the right of the decimal point.

Post a Comment

0 Comments