Module 1
Module 1
INTRODUCTION to C PROGRAMMING
Contents
Algorithms, flowcharts, pseudo codes,
structure of a C program
keywords, identifiers
basic data types
variables, constants
Branching statements
preprocessor directives.
Algorithms
• Be precise
• Be unambiguous
• Not even a single instruction must be repeated infinitely
• After the algorithm gets terminated, the desired result
must be obtained
Key Features of Algorithms
Sequence
condition
Repetition
Advantages
• Help to analyze the problem in a more effective manner
• Act as a guide or blueprint for the programmers to code the
solution in any programming language
Limitations
Laborious and a time-consuming activity
ELSE ELSE
sequence 2 Display Not Eligible
ENDIF ENDIF
Parts of Pseudocodes contd…
ENDWHILE
Parts of Pseudocodes contd…
ENDFOR ENDFOR
Example
Pseudocode to find the area of rectangle
3. Print area
4. End
Introduction to C
#include<stdio.h>
int main()
{
printf(“\n Welcome to the world of C”);
return 0;
}
Using comments
2. /* */ : Multiline comment
Character Set
Characters can be used to form words, numbers and expressions
Categories:
1. Letters
2. Digits
3. Special characters
4. White spaces
Keywords
roll_number, marks,name,RollNo,sum1
1. Numeric variables
2. Character Variables
- include any letter from the alphabet and numbers that are
given with single quotes
Declaring Variables
To declare a variable, specify the data type of the variable
followed by its name
datatype varname;
Example:
int sum;
float salary;
Variable names should always be meaningful and must
reflect the purpose of their usage in the program.
Initializing variables
syntax:
varname = value;
Example:
sum=0;
Initialization of variables can be done at the time of declaration
Example:
int sum=0;
float salary=35000.50;
Variables declared but not initialized usually contains garbage
values
Constants
Identifiers whose value does not change.
Integer Constants
An integer constant refer to a sequence of digits
There are 3 types of integers namely:
1. Decimal integer
2. octal integer
3. hexadecimal integer
1. Decimal integer
Decimalinteger consist of a set of digits 0 through 9,precceded
by an optional – or + sign
ex: 123 -321 0 654321 +78
Embedded spaces, commas and non-digit characters are not
permitted between digits
15 750 20,000 $1234 ----- are illegal numbers
2. octal integer
An octal integer constant consist of any combination of digits from
3. hexadecimal integer
A sequence of digits preceded by 0x or 0X is considered as
hexadecimal integer
They may include alphabets A through F or a through f
#define PI 3.14159
Formatting input/output
1. printf()
2. scanf()
1. printf():
- Example:
printf(“%d %f\n”,var1,var2);
2. scanf()
- Used to read formatted data from the keyboard
- Syntax:
scanf(“%d%f”,&var1,&var2);
Operators in C
A symbol that specifies the mathematical, logical, or relational operation to
be performed.
Categorized into the following groups:
1. Arithmetic operators
2. Relational operators
3. Equality operators
4. Logical operators
5. Unary operators
6. Conditional operator
7. Bitwise operators
8. Assignment operators
9. Comma operators
1. Arithmetic Operators
2. Relational Operators
3. Equality Operators
4. Logical Operators
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
Logical NOT table:
Logical NOT
!True False
!False True
5. Unary Operators
UNARY OPERATORS
Minus - b=10
a=-(b) //a=-10
Increment ++ ++a //a=a+1
Decrement -- --a //a=a-1
6. Conditional Operators
a=10;
b=15;
x=(a>b) ? a : b; --- x is assigned the value of b
This can be achieved using if…else statements as follows:
if(a>b)
x=a;
else
x=b;
7. Bitwise Operators
~ Bitwise NOT
8. Assignment Operators
v = exp
where v → variable,
= → assignment operator
exp → expression
C has a set of ‘shorthand’ assignment operators of the form
v op = exp;
where op = shorthand assignment operator
Ex: x=x+3
x+=3
9. Comma Operator
variable = expression;
1) x = a * b – c;
2) y = b / c * a;
Precedence of Arithmetic Operators
High priority * / %
Low priority +-
The basic evaluation procedure includes ‘two’ left-to-right
passes through the expression
During the first pass, the high priority operators(if any) are
applied as they are encountered
During second pass, the low priority operators(if any) are
applied as they are encountered
Operator Precedence
Ex: x = a – b / 3 + c * 2 - 1 where a=9,b=12,c=3
X= 9 - 12 / 3 + 3 * 2 – 1 is evaluated as follows
First pass
Step 1: x = 9 - 4 + 3 * 2 - 1
Step 2: x = 9 – 4 + 6 - 1
Second pass
Step 3: x = 5 + 6 - 1
Step 4: x = 11 - 1
Step 5: x = 10
The order of evaluation can be changed by introducing
parenthesis into an expression
Ex: 9 – 12 / (3 + 3 ) * ( 2 – 1 )
step 2: 9 – 12 / 6 * 1 step 4: 9 - 2
Third pass
step 5: 7
This time procedure consists of three left-to-right passes
The number of evaluation is 5(i.e., equal to the number of
arithmetic operators)
Parentheses may be nested, and in such cases, evaluation of the expression
will proceed outward from the innermost set of parentheses
Note: Make sure that every opening parantheses has a matching closing
parantheses
Ex: 9 - ( 12 / ( 3 + 3 ) * 2 ) – 1 = 4
9 – (( 12 / 3 ) + 3 * 2) – 1 = - 2
2. If parentheses are nested, the evaluation begins with the innermost sub
expression
4. The associatively rule is applied when two or more operators of the same
5. Arithmetic expressions are evaluated from left to right using the
rules of precedence
Type Conversion
Done when the expression has variables of different data types
Data type is promoted from a lower to higher level
Example:
int a=5;
float b=6.5,c;
c=a+b; // c=11.5
If the two operands in an assignment operation are of different
type, the right hand side(RHS) of the operand is automatically
converted to the data type of left hand side(LHS)
Example:
int a;
float b=65.5;
a=b; // a=65
Typecasting
(type-name) expression
if(test expression)
{
statement 1;
………………
statement n;
}
statement-x;
#include<stdio.h>
int main()
int x=0;
if(x>0)
x++;
printf(“\n x=%d”,x);
}
If-else statement
Syntax:
If(test expression)
{
statement block1;
}
else
{
statement block 2;
}
Statement x;
Example: to find largest of two numbers
#include<stdio.h>
main()
{
int a,b,large;
printf(“\n Enter the values of a and b:”);
scanf(“%d %d”,&a,&b);
if(a>b)
large=a;
else
large=b;
printf(“\n Large=%d”, large);
}
if-else-if statement
also known as nested if construct
syntax:
if(test expression 1)
{
statement block 1;
}
else if(test expression 2)
{
statement block 2;
}
…
else
{
statement block x;
}
Statement y;
Example: to test whether a number entered is positive, negative or
equal to zero
#include<stdio.h>
main()
{
int num;
printf(“\nEnter any number:”);
scanf(“%d”,&num);
if(num==0)
printf(“the value is equal to zero”);
else if(num>0)
printf(“\n the number is positive”);
else
printf(“\n the number is negative”);
}
Dangling else problem
Created when there is no matching else for every if statement
C always pair an else statement to the most recent unpaired if
statement in the current block
Ex:
if(a>b)
if(a>c)
else
if(condition) if(condition)
if(condition-1) {
if(condition-2) if(condition-1)
else if(condition-2)
Statement; }
else
Statement;
Rules for using if, if-else and if-else-f statement
switch(x) If(exp1)
{ {
case 1: // do this // do this
case 2: // do this }
case 3: // do this else if(exp2)
…….. {
default: // do this
// do this } else if(exp3)
} {
// do this
}
Advantages of using a switch case statement
Easy to debug
Easy to read and understand
Ease of maintenance as compared with its equivalent if-else
statements
Like if-else statements, switch statements can also be nested
Executes faster than its equivalent if-else construct