| Printf The printf statement allows you to send output to standard out(Computer Screen). Here is another program that will help you learn more about printf: #include <stdio.h> int main() { int d, e, f; d = 5; e = 7; f = d + e; printf("%d + %d = %d\n", d,e,f); getchar(); return 0; } Here save the file as Second.c. Compile and Run the program. Explanation: • The line int d, e, f; declares three integer variables named d, e and f. Integer variables hold whole numbers. • The next line initializes the variable named d to the value 5. • The next line sets e to 7. • The next line adds d and e and "assigns" the result to f. The computer adds the value in d (5) to the value in e (7) to form the result 12, and then places that new value (12) into the variable f. The variable f is assigned the value 12. For this reason, the = in this line is called "the assignment operator." • The printf statement then prints the line "5 + 7 = 12." The %d placeholders in the printf statement act as placeholders for values. There are three %d placeholders, and at the end of the printf line there are the three variable names: d, e and f. C matches up the first %d with d and substitutes 5 there. It matches the second %d with e and substitutes 7. It matches the third %d with f and substitutes 12. Then it prints the completed line to the screen: 5 + 7 = 12. The +, the = and the spacing are a part of the format line and get embedded automatically between the %d operators as specified by the programmer. Next Page >> |