Structure of C program and Operators
Structure of C program and Operators
C is a structured programming language made up of procedures (functions) that consist of declarations and
other statements. Generally a C program consists of one or more functions of which main is a special function.
The other functions may be from the C library or user defined. All the statements under the functions should be
present within curly brackets ({}) and each statement should be delimited by a semicolon (;).
C program structure consists of the following parts
i). Preprocessor directives.
ii). Global variables and function declarations
iii). Main function.
iv). Other functions
statement1;
|
|
statement n;
}
I). Identifier
This is a general name that refers to variable names, constant names, function names etc
It should be made up of a sequence of characters consisting of letters ‘a’ to ‘z’, digits ‘0’ to ‘9’ and the
underscore ‘_’.
II). Constant.
It refers to an identifier that represents values that cannot be changed during the execution of a program. Each
constant has a type that is determined by its form and value. The constants are classified into the following:
i) Numeric constant.
ii) Character constant.
iii) String constant
i) Numeric constant.
Is a constant made up of numeric digits with optional presence of a decimal point. There are two types,
namely
a) Integer – Refers to a sequence of numeric digits without a decimal point (whole numbers).
b) Floating point numeric constant – Refers to sequence of numeric digits with a decimal point
(fractional numbers).
IV). Variables
A variable is a location in the computer’s memory where a value can be stored for use by a program.
Or A variable is placeholder in which variables are recalled at will.
Values stored in variables can change during program execution, although the value of the variable may change
the place in memory where it’s stored remains the same. Variables have a name and values.
Variable names.
It refers to valid identifiers used to identify storage locations whose values may vary during execution of the
program. In most cases, variables are named with description that transmits the idea of what value it holds.
i) Integer: - It refers to whole numbers, both positive and negative. Unsigned integers refer to positive
values only. In addition, there are long and short integers. The keyword used to define integers is int
and it holds two bytes of memory space. The following is an example an integer declaration and
initialization.
int sum;
sum = 20;
ii) Floating point: - These are numbers that contain fractional parts both positive and negative. The
keyword used to define float variables is “float” and it occupies four bytes of memory space. An
example of declaring and initializing a float variable is as follows;
float number;
number = 0.123;
iii) Double: - It refers to exponential numbers, both positive and negative. The key word used to define
double variables is “double” and it occupies 8 bytes of memory space. The following is an example
of declaring and initializing a double variable.
iv) Character: - It refers to a single character. The key word used to define character variables is “char”
and it holds one byte of memory space. An example of a character variable declaration and
initialization is as follows.
char letter;
letter = ‘A’
Example.
A program illustrating the various data types
#include <stdio.h>
void main ( )
{
int sum;
float number;
double big;
char letter;
sum = 20;
number = 0.123;
big = 3.12e7;
letter =‘A’;
printf ("The value of sum is %d\n", sum);
printf ("The value of number is %f\n", number);
printf ("The value of big is %e\n", big);
printf ("The value of letter is %c\n", letter);
}
VI). Delimiters.
A delimiter is a symbol that has a syntax meaning and significance but doesn’t specify any operation to yield a
value.
The following are delimiters used in C language.
A statement block is a group of statements surrounded by curly braces. C views a statement block as one
statement. The last statement executed becomes the value of the statement block.
In C statement blocks are enclosed in pairs or curly brackets {....} as shown below:
{
statement_1;
statemrnt_2;
statemrnt_3;
.....
statement_n;
}
Types of Statements
Assignment statement: - It uses the assignment = to give or assign the variables on operators left side a value
to the operators right side or the result of an expression on the right side.
Expression statement: - Is a statement that evaluates to a value. Generally, it’s a combination of operators,
constants, variables and function calls. The expressions are classified as arithmetic, relational and logical
IX). Labels.
These are identifiers used with a statement so that the statement can be referred to later during execution of a
program. The rules used to name labels are the same as those of variables. Labels are usually the targets of the
goto statements.
Label_identifier:
X). Comments
These are non-executable program statements meant to enhance program readability and allow easier program
maintenance. Comments are usually optional in the program. The need for use of too many comments can be
avoided by good programming practices, such as use of sensible variable names, indenting program statements
and good logic design.
Modifier Meaning
d Decimal integer
f Floating point number
c Single character
s A sequence of characters
h Short integer
l Long integer
e Double floating point numbers
u Unsigned integer
o Octal numbers
x Hexadecimal numbers
Input Functions
These are inbuilt C functions used to read data to the computer. Input data can be users’ data entry through the
keyboard, or a file stored in a secondary device such as a disk, tape e.t.c
scanf: This is an inbuilt C function, which is used to perform formatted input and reads all types of values.
The function has atleast two arguments
Syntax
scanf (control string, variable list);
Control string: – it refers to the conversion specification characters and determines the number or arguments
that will follow it. It should be given within double quotes (“”).
Variable list: – It refers to variable names. An ampersand (&) symbol should precede each numeric and
character type variable by which the address of the variable is denoted.
Output Functions.
These are inbuilt C functions used to obtain (write) information from a program. The user can get the result or
data on the monitor or data can be written to another file in a secondary storage device, such as a disk or a tape.
The following are examples of output functions.
i) printf ()
ii) putch ()
printf: This is an inbuilt C functions that is used to perform formatted output and used to write all type of
values.
Syntax
printf (control string, variable list);
Control string: – It contains the conversion specification characters along with string constants. The number of
conversion specification characters determines the number of variables that follow it. It should be given within
double quotes (“”)
Variable list: – It refers to the variable names or expressions that can optionally be present in printf function.
Note:
To format input or output is to control how data is read or written, to convert input to the desired type (int, char,
float, etc) and to write output in the desired manner.
1) Arithmetic operators
It refers to operators used for mathematical calculations. Arithmetic expressions consist of constants,
variables, function calls and arithmetic operators.
N/B: A modulus operator assigns the remainder left over after division of two integer numbers. Modulus
operator cannot be applied with floats or doubles.
Hierarchy of operations
The following is the hierarchy of operations followed by C compilers when executing the arithmetic
expressions
• * / % Evaluated from left to right
• + - Evaluated from left to right
However the sub expressions within parenthesis are evaluated first.
Example
Evaluate the following expression
(i + j) /k * n % m – 5 * 6
Where
i = 2, j = 1, k = 7, n = 4, m = 6
Operator Meaning
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
!= Not equal to.
Relational operators are binary operators and hence they require two operands.
Hierarchy of operations
There’s no hierarchy for these operators. The expressions are evaluated first and then the relation is tested.
X Y X&&Y X || Y !X X^Y
T T T T F F
F T F T T T
T F F T T
F F F F F
Assignment operators
It refers to operators used to store a value in a variable for future reference. The following are different types
of assignments.
i). General assignment: – It is used to assign a variable with the result of an expression or value.
Syntax:
Variable = expression;
E.g. area = length * width;
iii). Multiple assignment: – This occurs when more than one variable is assigned with single value or
expression. This is a distinct feature of C language.
Syntax
Variable1 = Variable2 = Variable3… =value/expression.
E.g. k = m = n = 1;
Example
a) int count = 2;
count * = 2;
b) int count = 2;
count % = 2;
c) int i=5,j;
j = ++ i + 5
d) int i=5,j;
j = i -- + 5
If the results of the condition are true, Expression1 is evaluated and the result of the evaluation becomes the
result of the operation. If the condition is false, Expression2 is evaluated and its result becomes the result of
the operation.
Example
i) Given the following declaration and assignment statement:
int s, x= -2;
Evaluate the value of s after the following line of code is executed
s = (x>0)? –1: x*x;
Solution
s=4
ii) The following is the same C programs written using if…else control statements and conditional
expression operator
# include <stdio.h>
void main ( )
{
int number;
printf ("Enter an integer number\n");
scanf ("%d", &number);
if (number <0)
printf ("NEGATIVE\n");
else if ( number >0)
printf ("POSITIVE\n");
else
printf ("ZERO\n");
}
Exercise
(1). Given the following declaration and assignment states
int x=9,y=5,z=4;
float v=5.0;
Evaluate the value of x after each of the following lines of code is executed
(NB: The lines of code are independent of one another)
i) x=++x+5;
ii) x+=z--+5;
iii) x+=y%2;
iv) x=z%v;
v) x%=y+z*z%y;
x= - b √ (b2 + 4ac);