means that new value of year is equal to the old value of the year+1;
It is also possible to assign a value to a variable at the time
variable is
declared. This takes the following form:
data-type variable_name = constant;
Some examples are:
int val=100;
char yes= ‘x’;
The process of giving values to variable is called initialization. C
permits the initialization of more than one variable in one
statement using multiple assignment operators.
For example:
a=b=c=0;
x=y=z=max;
The external and static variables are initialized to zero by default.
1.2.8 Defining symbolic constants
These constants may appear repeatedly in a number of places in the
program. Mathematical constant “pi”. We face 2 problems in the subsequent
use of such programs.
a. Problem in modification of the program.
b. Program in understanding the program.
Syntax:
#define symbolic name value of
constant
Ex:
# define STRENGTH 100
# define PASS MARK 50
#define MAY 200
#define PI 3.14159
Symbolic name are sometimes called constant identifier.
I .Rules
Symbolic names have the same form as variable name (written in
capital)
No blank space between the bound sign ‘#’ and the word define is
permitted.
‘#’ must be the first character in line.
A blank space is required between # define and symbolic name and
between the symbolic name and constant.
#define statement is must
Symbolic names are NOT declared for data types its data type depends
on type of constant.
#define statements may appear anywhere in the program but before it
is referenced in the program.
II. Declaring a variable as constant:
The value of certain variable to remains constant during the execution of
the program.
Declaring variables with the qualifier const at the time of
initialization.
Ex: const int class_size=40;
It tells the complier that the value of int variable class size must
not be modified by the program.
III. Declaring a variable as volatile:
It could be used to tell explicitly the compiler that a variable value may
be changed at any time by external sources.
Volatile int date;
1.3 OPERATORS AND EXPRESSION
An operator is a symbol that tells a computer to perform certain
mathematical or logical manipulations. C operators can be classified in to
a number of categories.
a. Arithmetic operators
b. Relational operators
c. Logical operators
d. Assignment operators
e. Increment and decrement operators
f. Conditional operators
g. Bitwise operators
h. Special operators
a. Arithmetic Operator
C provides the entire basic arithmetic operator.
Two types:
i. Binary operators
ii. Unary operators
i. Binary operators
Numeric calculations between two constant values
Binary arithmetic Exampl
operator es
+ 2+2=4
- 5-3=2
/ 10/2=5
% 11%3=2
* 2*3=6
ii. Unary operators
Unary operators are increment operator (++), decrement operator(--) and
minus(-)
Operat Description
or
- Minus
++ Increment
-- Decrement
& Address operator
Sizeof Gives the size of
operator
Arithmetic operations:-
a) Integer Arithmetic
When both the operands in a single arithmetic expression such as a+b
are integer the expression called an integer expressions and the
operation is called integer arithmetic.
a = 14
a-b =10
a+b = 18
a*b = 56
a/b = 3
a%b = 2
During integer division if both operands are of same sign the result is
truncated towards zero if one of them is negative the direction of function
is implementation dependent.
6/7 = 0 and -6/-7 = 0
-6/7 may be 0 or -1(machine dependent)
Ex:
main ()
{
int months, days;
printf (“Enter days \n”);
scanf(“%d”,&days);
days=days%3;
printf(“months=%d, days=%d”, months, days);
}
b) Real Arithmetic
An arithmetic operation involving only real operands is called real
arithmetic. An real operand may assume value either in decimal or
exponential notation
If x,y,z are floats
X=6.0/7.0=0.8571
Y=1.0/3.0=0.3333
Z=-2.0/3.0=-0.666
The operator % cannot be used with real operands.
c) Mixed-mode Arithmetic
When one of the operands is real and other is integer, the expression is
called a mixed mode arithmetic expression. If either operand is of the real
type then only the real operation is performed and result is always a real
number
15/10.0=1.5
When as 15/10=1
The arithmetic operators are used for performing basic arithmetic
operations on numerals and characters. The following table lists arithmetic
operator
Operat Examp Resu
Name Description
or le lt
Adds the two operands(Can also
a+b Addition 3+5 8
be used for String concatenation)
Subtracts the second operand
A–b Subtraction 3–5 -2
from the first Operand
A*b Multiplication 3*5 15 Multiply both the operands
Divides the first operand by the
a/b Division 15 / 3 5
second
Returns the remainder after
Modulo
a%b 3%2 1 dividing the first number by the
Division
second
b. Relational Operators
Relational operators are symbols that are used to test the relationship
between two variables or between a variable and a constant. These
operators are used for checking conditions in control statements. The table
shows the various relational operators and their meaning.
Operat Examp
Name Description
or le
a==
== Equal operator True if a equals b else False
b;
True if a does not equal b else
!= Not Equal operator a ! = b;
False
< Less than operator a < b; True if a less than b else False
True if a greater than b else
> Greater than operator a > b;
False
Less than or Equal a<= True if a less than or equals b
<=
operator b; else False
Greater than or equal a>= True if a greater than or equals
>=
operator b; b else False
c. Logical Operators:
Operat Symb
Example
or ol
AND && exp1 &&
8
exp2
exp1 ||
OR ||
exp2
NOT ! ! exp1
Operators, which are used to combine two or more relational
expressions, are called as logical operators. These operators are used to test
more than one condition at a time. The table shows the various logical
operators and their meaning:
d. Assignment Operators
Assignment operator is the most common operator almost used with
all programming languages. It is represented by “=” symbol in C which is
used to assign a value to a variable lying to the left side of the assignment
operator.
But if the value already exists in that variable then it will be
overwritten by the assignment operator(=)
Syntax: <variable> = <expression>;
Example:
X = y = z = 2;
X = (y + z);
e. Increment and decrement operators
C allows 2 very useful operators not generally found in other
languages. These are the increment and decrement operators: ++ and --
The operator ++ adds 1 to the operand, while – subtracts 1. Both are unary
operators and take the following form:
Operator Symbol Action
9
Examples
Increment ++ Increments the operand by one
++x, x++
Decrement -- Decrements the operand by one --x,
x--
Unary operator:-
The operators that act upon a single operand to produce a new value are
called as unary operators. When the operator is used before the variable, the
operation is
10