Components of C Program
Components of C Program
C program structure
Program’s Components
Variables
Numeric data types
Constants
Statements, expressions and operators
Program instructions are formed using certain symbols and
words according to some rules known as syntax rules or
grammar.
#include <stdio.h>
void main( )
{
printf(“Hello, World!”) ;
}
/* A first program in C */ : It is a comment
#include <stdio.h>:Library of standard
Variables
Constants
Data Types
Constants
Numeric Character
Constants Constants
Single String
Integer Real
Character Constants
Constants Constants
Constants
Integer Constants:
Refers to a sequence of digits
Spaces, commas and non-digit characters are not permitted.
There are 3 types of integers: decimal, octal and hexadecimal.
Decimal – (0-9), preceded by – or+ sign eg., 123, -321
Octal- (0-7) with a leading 0 eg., 037, 0435
Hexadecimal- digits preceded by 0x or 0X, also it may include
alphabets A – F or a-f.
short int
int
long int
Integer Types:
float
double
long double
Character Types:
A single character can be defined as a character (char) type data.
They are stored in 8 bits.
The qualifier signed and unsigned may be applied explicitly to char.
Unsigned char: 0 to 255
signed char: -128 to 127
Void Types:
Has no values
Used to specify the type of functions.
Void applied to function indicates it does not return any value to the
calling function.
◦ A variable is a data name that may be used to store a data
value.
◦ Variable names correspond to locations in the computer's
memory
◦ Every variable has a name, a type, a size and a value
◦ A variable name can be chosen so as to reflect its nature in the
program.
◦ Examples :
Distance, average_number, x1 are all valid
group one, 123, int_type are invalid
Before using variables, you must give the compiler some
information about the variable; i.e., you must declare it.
Declaration does 2 things:
◦ It tells the compiler what the variable name is
◦ It specifies what type of data the variable will hold.
It must be done before they are used in the program and
before any executable statement.
Primary type declaration:
Syntax:
Data_type v1,v2,…,vn;
int sum;
Example: float area;
When we declare a variable
◦ Space is set aside in memory to hold a value of the
specified data type
◦ That space is associated with the variable name
◦ That space is associated with a unique address
or logical manipulations.
C operators can be classified into a number of categories:
Arithmetic
Relational
Logical
Assignment
Increment and decrement
Conditional
Bitwise
Special
Operator Meaning Example
+ Addition x=3+2;
y+z;
x+y+2;
- Subtraction 3−2;
int x=y−z;
y−2−z;
* Multiplication int x=3∗2;
nt x=y∗z;
x∗y∗2;
/ Division float x=3/2; produces x=1
float x=3.0/2 produces x=1.5
int x=3.0/2; produces x=1
% Modulo division int x=3%2; produces x=1
int y=7;int x=y%4; produces 3
int y=7;int x=y%10; produces 7
If both operands of a division expression are integers, you will get an
integer answer. The fractional portion is thrown away.
Examples : 17 / 5 = 3
4/3=1
35 / 9 = 3
v op=exp; x + = 1;
Equals Equals
to to
v = v op exp; x = x+1;
Examples:
a+ = 1; a = a+1;
a- = 1; a = a-1;
a% = b; a = a%b;
a* = 2; a = a*2;
Logical Operators:
Sometimes we need to test multiple conditions in order to
make a decision.
Logical operators are used for combining simple conditions to
make complex conditions.
It yields either value 1 or 0.
Example:
(a>b) && (x==10)
number<0 || number>100
Increment and Decrement Operators:
Both the operators add 1 or subtract 1 from the operand.
Both are unary operators of the form:
++m or m++
--m or m - -
Consider m=5;y=m++;
◦ The value of y=5 and m=6.
◦ Consider m=5;y=++m;
◦ The value of y and m would be 6
Increment and decrement operators can only be applied to variables,
not to constants or expressions.
The position of the ++ determines when the value is
incremented.
If the ++ is after the variable, then the incrementing is done
last (a postincrement).
int amount, count ;
count = 3 ;
amount = 2 * count++ ;
amount gets the value of 2 * 3, which is 6, and then 1 gets
added to count.
So, after executing the last line, amount is 6 and count is 4.
If the ++ is before the variable, then the incrementing is done
first (a preincrement).
int amount, count ;
count = 3 ;
amount = 2 * ++count ;
1 gets added to count first, then amount gets the value of 2 *
4, which is 8.
So, after executing the last line, amount is 8 and count is 4.
Conditional Operator:
A ternary operator pair “?:” is used to construct conditional expressions.
Sizeof operator:
It returns the no. of bytes the operand occupies.
The operand may be a variable, a constant or a data type qualifier,
Ex: m=sizeof(sum);
n=sizeof(long int);
Normally, used to determine the lengths of arrays and structures.
Precedence of arithmetic operators:
Operator(s) Precedence &Associativity
() Evaluated first. If nested, innermost
first. If on same level, evaluated
left to right.
*/% Evaluated second. If there are
several, evaluated left to right.
+- Evaluated third. If there are several,
evaluated left to right.
= Evaluated last, right to left.
Relative precedence of relative and logical operators:
Operator(s)
! Highest
> >= < <=
= = !=
&&
|| Lowest
c is printed, then
incremented
c is incremented, then
printed
An expression is a sequence of operands and operators that
reduces to a single value.
Arithmetic expressions: It is a combination of variables,
constants and operators.
Evaluation of expression:
Expression are evaluated using an assignment statement of the
form:
variable = expression;
Ex: x = a*b-c;
z = a-b / c+d;
Rules for evaluation of expression:
First, parenthesized sub expression from left to right are
evaluated.
The precedence rule is applied in determining the order of
application of operators in evaluating sub-expressions
The associativity rule is applied when two or more operators
of the same precedence level appear in the sub-expression.
Arithmetic expressions are evaluated from left to right using
the rules of precedence.
When parenthesis are used, the expressions within parenthesis
assume highest priority.
Program: main()
{
float a,b,c,x,y,z;
a=9;
b=12;
c=3;
x = a - b / 3 + c * 2 - 1;
y = a - b / (3 + c) * (2 - 1);
z = a - (b / (3 + c) * 2 ) – 1;
printf (“x = %f\n”, x);
printf (“ y = %f\n”, y);
printf (“ z = %f\n”, z);
}
Output:
x = 10.000000
y = 7.000000
z = 4.000000
Implicit type conversion:
◦ automatic conversion
◦ If the operands are of different types, the lower type is converted
to higher type before the operation proceeds.
◦ The final result of an expr is converted to the type of the variable
on the LHS of assignment operator.
◦ However the foll. changes are introduce before the final
assignment
Float to int causes truncation of the fractional part.
Double to float causes rounding of digits
Long int to int causes dropping of excess higher order bits
Implicit type conversion:
long double
double
float
Conversion unsigned long int
hierarchy long int
unsigned int
int
short char
Explicit conversion:
Also known as casting of value.
General syntax:
(type-name) expression
Examples:
X = (int)7.5 7.5 is converted to int by truncation
A = (int)21.3 / (int)4.5 evaluated as 21/4 resulting in 5
B = (double)sum/n division is done in floating point
Y = (int) (a+b) result is converted to int
z = (int) a + b a is converted to int and then added to b
C supports a feature known as type definition that allows
users to define an identifier that would represent an existing
data type.
This user defined data type identifier can then be used to
declare variables.
General form:
typedef type identifier;
Where type refers to an existing data type and identifier refers
to the new name given to the data type.
Ex: typedef int units;
typedef float marks;
units batch1, batch2;
marks name1,name2;
Advantage: we can create meaningful data type names for
increasing the readability of program.
This tells the compiler that the value of the ‘int’ variable must
not be modified by the program
A qualifier ‘volatile ’ can be used to tell explicitly that a
variable’s value may be changed at any time.
Some functions:
◦ Sqrt (x) square root of x
◦ Pow (x,y) x to the power of y (x y)
◦ Ceil(x) x rounded up to the nearest integer
◦ Floor(x) x rounded down to the nearest integer