The Components of a C Program


The Components of a C Program

Every C program consists of several components combined in a certain way.
Most of this book is devoted to explaining these various program components
and how you use them. To help illustrate the overall picture, you should begin
by reviewing a complete (though small) C program with all its components
identified. Today you will learn:
• About a short C program and its components
• The purpose of each program component
• How to compile and run a sample program

A Short C Program

This is a very simple program.
All it does is accept two numbers that are entered from the keyboard and
calculate their product. At this stage, don’t worry about understanding the details of how the program works. 

multiply.c—A program that multiplies two numbers

1: /* Program to calculate the product of two numbers. */
2: #include <stdio.h>
3:
4: int val1, val2, val3;
5:
6: int product(int x, int y);
7:
8: int main( void )
9: {
10: /* Get the first number */
11: printf(“Enter a number between 1 and 100: “);
12: scanf(“%d”, &val1);
13:
14: /* Get the second number */
15: printf(“Enter another number between 1 and 100: “);
16: scanf(“%d”, &val2);
17:
18: /* Calculate and display the product */
19: val3 = product(val1, val2);
20: printf (“%d times %d = %d\n”, val1, val2, val3);
21:
22: return 0;
23: }
24:
The printf() Statement
The printf() statement (lines 11, 15, and 20) is a library function that displays information
on-screen. The printf() statement can display a simple text message (as in lines 11
and 15) or a message and the value of one or more program variables (as in line 20).
The scanf() Statement
The scanf() statement (lines 12 and 16) is another library function. It reads data from
the keyboard and assigns that data to one or more program variables.

Imtiazahmad3566@gmail.com 

Post a Comment

1 Comments