Structure of a C Program Chapter 2
Objectives of this chapter:
C Program Structure
C program
#include
main()
function a()
function b()
#include
main()
{
int num;
num=1;
printf("%d is my number. \n",num);
}
For the above example,
#include is a preprocessor instruction.
int num; is a declaration statement.
Num = 1; is an assignment statement.
printf("%d is my number. \n",num); is a function statement.
the semicolon (;) at the end of a line identifies the line as a C statement or instruction.
Note : the semicolon is part of the statement, not a separator between statements as it is in Pascal.
Declaration statements.
Declaration statements are used to declare any variables that are to be used in the program.
all variables in a C program must be declared before they are used.
The declaration of variabhle in C has the following general form:
type variahble;
for example,
int num;
the above statement declares a variable called num of integer data type.
The reserved word int is one of the basic C data types.
Reserved words or keywords are specific words used to express a language; they may not be used for any other purpose.
All reserverd words or keywords in C must be in lowercase.
Variable name / identifier choice
use meaningful names
the maximum number of characters for a variable name is compiler dependant
variable names can consist of
letters
digits
the underscore (_) which is treated as a character.
Example:
Valid Names Invalid Names
average $Zoom[**
cow1 1cow
Big_Number Big-Number
_kcab don’t
C language is case sensitive; that is upper and lowercase letters are distinct.
For example the variable names num, Num, and NUM each refer to a different variable.
Assignment statement
the assignment statement is one of the most basic operations to give a variable a value.
For example:
num=1;
means "give the variable num the value of 1".
The assignment statement, like the declaration statement, is completed with a semicolon.
The assignment statement’s general form:
identifier = expression;
the = is called the assignment operator, and C has more than one assignment operator.
#include
main()
{
printf("Hello Universe! \n");
printf("Welcome to C language. \n");
}
the statements in the above program uses a standard C function called printf().
Function statements are statements that calls other functions.
The parantheses () after a function name makes it a function statement.
the items enclosed in the parantheses are information passed from the main() function to the function called.
Such information is called the argument(s) or parameter(s) of a function.
Control statements.
These type of statements provide program "flow" control.
There are three forms of program flow control:
sequence
executing a series of statements
selection or branching
using a test or condition to decide between alternative actions.
Repetition or looping
repeating a sequence of statements until some condition is met.
Null statement
essentially does nothing.
Example:
;