0% found this document useful (0 votes)
3 views14 pages

Part 2

The document outlines the structure and components of C programming, detailing types of statements, operators, and expressions. It explains expression, compound, and control statements, as well as the use of symbolic constants and the structure of a C program including sections like documentation and global declarations. Additionally, it covers various operators such as arithmetic, logical, relational, and bitwise operators, along with their functionalities and examples.

Uploaded by

Sampurna prusty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views14 pages

Part 2

The document outlines the structure and components of C programming, detailing types of statements, operators, and expressions. It explains expression, compound, and control statements, as well as the use of symbolic constants and the structure of a C program including sections like documentation and global declarations. Additionally, it covers various operators such as arithmetic, logical, relational, and bitwise operators, along with their functionalities and examples.

Uploaded by

Sampurna prusty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Statements:

 A statement in a computer program called out some action.


 There are three types of statements used in ‘c’ language.
o Expression statement
o Compound Statement
o Control Statement
Expression Statement:
 An Expression statement consists of a valid ‘c’ expression followed by “semicolon”.
 The expression statement used to evaluate a group of expressions.
 Eg: sum = x + y;
avg = (n1+n2+n3)/3;
a = 10;
Compound Statement:
 A group of valid ‘c’ expressions placed within ‘{‘ and ’}’ is called compound statement.
 The individual statement may be an expression statement, a control statement (or) even a compound
statement.
 The compound statement is not ended with a semicolon.
 Eg: {
r = n%10;
s = s + (r*r*r);
n = n\10;
}
Control Statement:
 The control statement is used for program flow and to check the condition of given expression.
 The keywords of the control statements are predefined and the programmer may not use them.
 Eg: if (a>b)
printf (“a is big”);
else.
printf (“b is big”)
Symbolic Constants:
 There are two ways for creating symbolic constants.
o Using the quantifier constant
o Defining a set of integer constant using enum keyword.
Using the qualifier Const:
 Any value declared as const can’t be modified during the execution of the program in any way.
 In ‘C’ we can use const in a constant expression as follows.
 Syntax: const data-type variable name = value;
Eg: const float PI = 3.14;
 Similarly we can create constants by using the pre-processors statement #define.
 Syntax: #define variable value
Eg: #define PI 3.14
 The above statement should not end with a semi colon.
 const is used to create typed constants where as #define allows to create constants that have no type
information.
Note:
 As with long and short if we use const modifier they will be treated as int.
 In general constants are represented in capital letters or upper case letters.
Using enum keyword:
 By using enumerated data type we can assign values to a list of strings.
 Eg: enum color{Black, Blue, Green}
 Here Black = 0, Blue = 1, Green = 2
Structure of ‘C’ Program:
 Every programming language has a certain structure which has to be followed while writing programs in
that language.
 ‘C’ language also contains a structure to write programs.
 As this language supports modular programming, it contains one or more functions.
 One of those functions that should be mentioned in every c-program is “main function”.
 ‘C’ language contains one or more sections in a program. The structure of ‘C’ program is given below.
o Documentation Section
o Link Section
o Global Declaration
o main ().
 Declarations
 Executable Statements
o Sub functions
Documentation Section:
 In general documentation section consists of information about the program. It contains details like author
of the program, task of the program, date compiled, date written ,etc .
 All the above information is placed as comments.
 This section is “optional”.
Link Section:
 The link section provides the instructions to the compiler about library functions by linking to various
header files.
 In general the link section contains pre-processor statements.
 Eg: #include <stdio.h>
#include <conio.h>
#define PI 3.14
This section is optional
Global Declaration:
 When a variable is declared inside a function the variable scope is within that function only. These variables
are called local variables.
 If we want to make a variable available throughout the program irrespective of the function then the variable
is declared as global.
 Any variable declared above the main function is considered as “global variables”.
main ():
 Every ‘C’ program execution starts at main().
 Every program must include main().
 The entire ‘c’ program instructions are divided in to two parts namely
o Declarations
o Executable statements
Declarations:
Any variable which is used in the program should be declared first. ‘C’ program doesn’t allow to declare the
variables at the middle of the program.

Executable Statements:
It contains various executable statements that perform a specific task.
 The above two parts must be placed in a pair of braces ({ and }).
 Note that each and every statement in a ‘c’ program must end with semicolon except the pre-processor
statements.
Sub-Functions:
 This section contains user defined functions.
 In general, these functions are placed immediately after the main function.

OPERATORS AND EXPRESSIONS


Operators
 An operator is a symbol that informs to the computer to perform a particular task.
 The data items that operators act upon are called “operands”.
 If the operator requires only one operant then it is called “unary operator”. If it requires two then it is called
“Binary Operator”.
 ‘C’ language supports a different set of operators which are listed below.
o Arithmetic Operators
o Logical Operators
o Relational Operators
o Assignment Operators
o Increment / Decrement Operators
o Bit wise Operators
o Unary Operators
o Conditional Operators
o Special Operators
Arithmetic Operators:
 Arithmetic operators are basic and common operations performed using and computer language.
 There are five arithmetic Operators available in ‘C’. They are +,-,*,/,%.
 In any of the arithmetic operations, if one of the operant is of higher data type then other operant of smaller
data type will also converted to higher data type and the result is also higher data type.
 For example, in the division operation if one of the operant is floating point type and other is of integer type
then the result is floating point type.
 Integer divided by an integer will always give an integer.

Logical Operator:
 When we want to take a decision basing on 2 (or) more conditions then we will use logical operators.
 In three logical operators available in ‘C’ are
Operator Purpose
&& and
|| or
! not
Logical and: (&&)
 Logical And results in true if both the conditions are true otherwise the result is false.
Truth Table:
A B A&B
T T T
T F F
F T F
F F F

Logical Or: (||)


 Logical Or results in true if either of the conditions is true.
 The result is false if all the conditions are false.
Truth Table:
A B A||B
T T T
T F T
F T T
F F F
Logical not: (!)
 If the condition is true then the result is false and if the condition is false the result is true.

Truth Table:
A !A
T F
F T
Relational Operators:
 Relational Operators are used to perform comparison between two values.
 These operators’ returns true (1) if the comparison condition is true otherwise false (0).
 The operators used for comparison in ‘C’ are listed below.
Operator Purpose
< Less than
<= less than or equal to
> greater than
>= greater than or equal to
== equal to
!= Not equal to

Assignment Operators:
 This operator is used to assign a constant value (or) the result of an expression to a variable.
 In this operation Right hand side expression is evaluated first and then the result is assigned to left hand side
variable.
Operator Meaning
= Assign Right Hand value to LHS value
+= Value of LHS add to value of RHS and assign it back to the variable in LHS
Eg: a+=2 => a = a+2
-= Value of RHS variable will be subtracted from the value of LHS and assign it back to the
variable in LHS.
Eg: a-=2 => a = a-2
*= Value of LHS variable will be multiplied to the value of RHS and assign it back to the
variable in LHS.
Eg: a*=2 => a=a*2
/= Value of LHS variable will be divided by the value of RHS and assign it back to the
variable in LHS.
Eg: a/=2 => a=a/2
%= The Remainder will be stored back to the LHS after integer, division is carried out
between the LHS variable and RHS variable.
Eg: a%=2 => a=a%2
>>= Right shift and assign the value to the LHS.
Eg: a>> =2 => a=a>>2.
<<= Left shift and assign the value to the LHS.
Eg: a<<=2 => a=a<<2.
&= Bit wise AND operation and assign the value to the LHS.
Eg: a&=b => a=a&b.
|= Bit wise OR operation and assign the value to LHS.
Eg: a|=b => a=a | b
~= Bit wise complement and assign the value to LHS.

Increment/Decrement Operator {++/- -}


 Increment operator increases the value by ‘1’ where as decrement operator decreases the value by ‘1’.
 This operator can be used in two forms namely Prefix and Postfix.
 In case of Prefix notation the operator precedes the operant {++a}.
 In case of Postfix notation the operator is followed by operand {a++}.
 In Prefix notation increment operation will be given the highest priority i.e., first the value is incremented by
‘1’ and other operations are performed later.
 In Postfix notation, increment operator will be given the least priority i.e., the increment operation is
performed at last after all operations are performed.
 The above points are applicable to the decrement operator also.
Eg: 1) a=5 2) a=5
c= ++a c=a++
c=6,a=6 c=5, a=6.

Explanation:
 In above expression-1, c= ++a two operations namely assignment and increment are involved. Since the
operator ++ is used as prefix, first we perform incrimination i.e., a=a+1 and then this value is assigned to
‘C’.
 In the expression-2, c= a++ two operations namely assignment and increment are involved. Since the
operator ++ is used as postfix, first we assign the value of ‘a’ to ‘c’. i.e., c=a => a=5 and then the value of
‘a’ is incremented i.e., a=a+1=6

Bitwise Operators:
 Bitwise operators are one of the salient features of ‘C’ language.
 These are specially designed to manipulate the data at Bit level.
 The Bitwise operators are not applicable for float (or) Double data type.
 The following are the some of the Bit wise operators available in ‘C’ language.
Operator Purpose
& Bitwise AND
| Bitwise OR
^ Bitwise Exclusive OR
~ Bitwise 1’s component
<< Left Shift
>> Right Shift
Bitwise AND: (&)
 To generate a ‘1’ bit in the result, Bitwise AND need a ‘1’ in both the numbers.
 Bitwise AND operator is called “MASK” operator.
Eg: a=28, b=17
a =11100
b =10001
a&b =10000
a&b =16
Bitwise OR: (|)
 The Bitwise OR result is ‘1’ if at least one of the numbers is ‘1’.
Eg: a=28,b=17
a= 11100
b= 10001
a|b =11101 => a|b = 29
Bitwise Exclusive OR: (^)
 It is similar to task Bitwise OR and the result ‘1’ is produced if 1 is present in either but not both.
Eg: a=28, b=17
a= 11100
b= 10001
a^b=01101 => a^b =13
Bitwise 1’s complement: (~)
 The complement operator switches all the Bits in a binary pattern i.e., all 0’s becomes 1’s and all 1’s
becomes 0’s.
Eg: i) Bitwise complement of 12 is -13
ii) Bitwise complement of 0 is -1.
Shift Operations:
 The shift operations take binary patterns and shift the bits to the left (or) right.
 Keeping the same number of bits by dropping the shifted bits at the end and filling in with 0’s at other end.
 C-language provides 2 types of shift operators namely left and right shift.
Left Shift: {<<}
 This operator is used for left shifting .
Eg: a=00011100
a<<1 = 00111000
a<<2 = 01110000 => a<<2 =112
Right Shift: {>>}
 This operator used for right shifting.
Eg: a = 00011100
a>>1 = 00001110
a>>2 = 00000111 => a>>2 = 7
Unary Operator:
 These operators require only one operant.
 A Unary operator usually precedes their single operand.
 Sometimes the unary operators may also follow the operands.
 The following are the some of the unary operators available in ‘c’.
Operator Purpose
& Address of a variable
! Negation
++ Increment Operator
sizeof() Size of the subsequent data type in Bytes
Type forced type of conversion
Address Operator: (&)
 The Address operator is used to get the address of another variable.
Eg: p= &a

Pointer Operator: (*)


 Pointer Operator is used to get the content of the address which is pointer is pointing to
Eg: int a=5, *p;
p=&a;
*p => *(1020) => 5
Unary Minus: (-)
 It is used to represent negative values.
Negation: (!)
 It is used to switch the value i.e., if the condition is true the result is false and vice-versa.
Bitwise Complement: (~)
 The Bitwise complement operator switches all the bits in a given bit pattern.
Increment / Decrement Operator: (++ / --)
 Increment operator increases the value of a variable by ‘1’ and decrement operator decreases the value of
a variable by ‘1’.
Cast Operator: {Type casting}
 The type conversion is used to convert the set of declared data type to some other required type.
Sizeof ();
 The sizeof() operator is used to find the size of the data (or) variable in Bytes.
Conditional Operator: (? :)
 ‘C’ includes a very special type of operator called conditional operator.
 It is also called “ternary” operator since it requires three expressions. It acts like a short hand version of if-
else construction.
 Syntax: exp1? exp2: exp3
 In exp1 is true then exp2 is evaluated otherwise exp3 will be evaluated.
Eg: a>b? a=b

Special Operators:
 In addition to the above operators ‘C’ language supports some special operators like comma operator,
parenthesis for grouping expression, membership operators.

Comma Operator:
‘C’ language uses the comma operator is two ways.
 The first use of the comma operator is as a separator in variable declaration.
Eg: int a, b, c;
 Another use is as an operator in a ‘for’ looping construct.

Parenthesis for Grouping Expression:


 The precedence and associativity of each operator determines the evaluation of an expression when more
than one operator is involved.
 When parenthesis is put around an element of an expression then that element is evaluated before any
operation outside the parenthesis.
 Eg: (2+3) * 5 = 5*5 =25
Membership Operator:
 To represent the variables belonging to certain structures like Arrays, Structures, and Unions
Membership operator is used.
 Membership Operators are [], ->
PRECEDENCE AND ASSOCIATIVITY:
 Every operator has a precedence value. An expression containing more than one operator is known as a
complex expression.
 Complex expressions are executed according to precedence of operators.
 Associativity specifies the order in which the operators are evaluated with the same precedence in a complex
expression.
 Associativity is of two ways i.e., left-to-right and right-to-left.
 Left-to-right associativity evaluates an expression stating from left and moving towards right.
 Right-to-left associativity proceeds from right to left.
The precedence and associativity of various operators in C are as shown in the following table
Operators Operation Associativity Precedence
() Function call
[] Array expression or square bracket
-> Structure Operator Left to right 1st
. Structure Operator
+ Unary plus
_ Unary minus
++ Increment
-- Decrement Right to left 2nd
! Not operator
~ One’s complement
* Pointer operator
& Address operator
Sizeof Size of an object
Type Type cast
* Multiplication
Left to right
/ Division 3rd
% Modular division
+ Addition Left to right 4th
- Subtraction
<< Left shift Left to right 5th
>> Right shift
< Less than
<= Less than or equal to 6th
Left to Right
> Greater than
>= Greater than or equal to
== Equality 7th
Left to right
!= Inequality
& Bitwise AND Left to right 8th
^ Bitwise XOR Left to right 9th
| Bitwise OR Left to right 10th
&& Logical AND Left to right 11th
|| Logical OR Left to right 12th
?: Conditional Operator Right to Left 13th
=, *=, -=, &=, += Assignment Operators Right to Left
14th
^=, |=, <<=, >>=
, Comma Operator Left to right 15th

Expressions:-
An expression is a combination of operators and operands which reduces to a single value. An operator
indicates an operation to be performed on data that yields a value. An operand is a data item on which an
operation is performed.
A simple expression contains only one operator.foe example 3+5 is a simple expression which yields a
value 8, -a is also a single expression . A complex expression contains more than one operator. An example of
complex expression is 6+2*7.
An expression can be divided into six categories based on the number of operators, positions of the
operands and operators, and the precedence of operator.

Expression

Primary postfix prefix unary binary ternary

Primary Expressions
In C, the operand in the primary expression can be a Name, a Constant, or an Parenthesized Expression.
Name is any identifier for a variable. A constant is the one whose value cannot be changed during program
execution. Any value enclosed within parenthesis must be reduced to single value. A complex Expression can be
converted into primary expression by enclosing it with parenthesis. The following is an example
(3*5+8) ; (c=a=5*c);
Postfix Expressions
The postfix expression consists of one operand and one operator.
Example: A Function Call, The function name is operand and parenthesis is the operator.
The other examples are post increment and post decrement. In post increment the variable is increased by
1, a++ results in the variable increment by 1. Similarly in post decrement, the variable is decreased by 1, a--
results in the variable decreased by 1.
Prefix Expressions
Prefix Expressions consists of one operand and one operator, the operand comes after the operator.
Examples of prefix expressions are prefix increment and prefix decrement i.e., ++a,--a. The only difference
between postfix and prefix operators is, in the prefix operator, the effect take place before the expression that
contains operators is evaluated. It is other way in case of postfix expressions
Unary Expressions
A unary expression is like a prefix expression consists of one operand and one operator and the operand comes
after the operator.
Example: ++a; -b; -c; +d;
Binary Expressions
Binary Expressions are the combinations of two operands and an operator. Any two variables added,
subtracted, multiplied or divided is a binary expression
Example: a+b; c*d;
Ternary Expressions
Ternary Expressions is an expression which consists of a ternary operator pair “?:”
Example: exp1:exp2:exp3;
In the example, if exp1 is true exp2 is executed else exp3 is executed.
Type Conversion:
 In some situations, some variables are declared as integers and while performing an operation on these
variables we require the result as floating point type. In such situations we use type conversion.
 The type conversion is used to convert the set of declared data types to some other required types.
 Conversion can be carried out in 2 ways
o Converting by using assignment (Implicit casting).
o Using cast operator (Explicit casting).
Converting by Assignment:
 The usual way of converting a value from one data type to another is by using the assignment operator.
int a;
float b;
a=b;
 In this method, if a larger data type value is assigned to smaller data type then the value will be truncated
{cut off}. This method is called “Narrowing”.
 If a smaller data type is assigned to larger data type, this is no loss of data. This method is called
“Widening”.
Cast Operator:
 The cast operator is a technique to forcefully to convert one data type to another.
 The operator used to force this conversion is known as “Cast Operator” and the process is known as type
casting.
Syntax: x= (cast) expression
(or)
X= cast (expression)
Eg: int a, b;
float f;
f= (float) a/b;
(or)
f= float (a)/b;

You might also like