C Module 1 Full
C Module 1 Full
PRESENTED BY
Dr.Surendran.R
Professor CSE
Dept.,
CIT
Overview of C
GRAPHICS
Data types in C
Arithmetic (+,-,*,/,%)
Relational (<,>,<=,>=,==,!=)
Logical (&&,||,!)
Bitwise (&,|)
Assignment (=)
Compound assignment(+=,*=,-=,/=,
%=,&=,|=)
Shift (right shift >>, left shift <<)
Comments in C
Input
scanf(“%d”,&a);
Gets an integer value from the user and stores
it under the name “a”
Output
printf(“%d”,a)
Prints the value present in variable a on the
screen
For loops
The syntax of for loop is
for(initialisation;condition checking;increment)
{
set of statements
}
Eg: Program to print Hello 10 times
#include<stdio.h>
int main()
{
int I;
for(I=0;I<10;I++)
{
printf("\nHello");
}
return 0;
}
Go to
The goto statement is a jump statement which jumps
from one point to another point within a function.
#include <stdio.h>
int main()
{
int n = 0;
loop:
{
printf("\n%d", n);
n++;
}
if (n<10)
goto loop;
return 0;
While loop
The syntax for while loop
while(condn)
{
statements;
}
Eg:
#include <stdio.h>
int main()
{
int a=10;
while(a != 0)
{ printf("\n%d",a);
a--;
}
return 0;
}
Do While loop
The syntax of do while loop
do
{
set of statements
}while(condn);
Eg:
#include <stdio.h>
int main()
{
int i=10;
do
{
printf("%d\n",i);
i--;
}while(i!=0);
return 0; }
Conditional statements
if (condition)
{
stmt 1; //Executes if Condition is true
}
else
{
stmt 2; //Executes if condition is false
}
Conditional statement
switch(var)
{
case 1: //if var=1 this case executes
stmt;
break;
case 2: //if var=2 this case executes
stmt;
break;
default: //if var is something else this will execute
stmt;
}
Input and Output
Input
scanf(“%d”,&a);
Gets an integer value from the user and stores
it under the name “a”
Output
printf(“%d”,a)
Prints the value present in variable a on the
screen
Header files
Type a program
Save it
Compile the program – This will
generate an exe file (executable)
Run the program (.exe will create)
Program structure
A sample C Program
#include<stdio.h>
int main()
{
--other statements
}
Writing the first program
#include<stdio.h>
int main()
{
printf(“Hello”);
return 0;
}
Syntax of function
Declaration section
<<Returntype>> funname(parameter list);
Function Call
Funname(parameter);
Definition section
<<Returntype>> funname(parameter list)
{
body of the function
}
Example function
#include<stdio.h>
void fun(int a); //declaration
int main()
{
fun(10); //Call
}
void fun(int x) //definition
{
printf(“%d”,x);
}
C Questions
1."\r" represents _______________
Choose one answer.
A. Back Space
B. Carriage return
C. Alert
D. Form Feed
Ans: B.
2. Which of the following cannot be checked in a switch case sta
Choose one answer.
A. Float
B. Integer
C. Character
D. Enum
Ans: A
3. Which of the following correctly shows the hierarchy of arithmetic operations in
C?
Choose one answer.
A. / + * -
B. * - / +
C. * / + -
D. + - / *
Ans: C
4. Print b to z using putchar
#include<stdio.h>
#include<conio.h>
void main()
{
char c = 'a';
while(c++ <= 'z') putchar(c);
getch();
}
5. In a function two return statements should never occur successively.
Choose one answer.
A. True
B. False
Constants
A constant is an entity whose value can’t be changed
during the execution of a program.
Constants are classified as:
1. Literal constant
2. Qualified constants
3. Symbolic constants
1. Literal constant
integer constants,
floating-point constants,
character constants,
Integer constant
constant
Decimal 0..9
Octal 0…7
Hexa Decimal 1,2,3,……9, A,B,C,D,E,F
E.g.
Marks=90;
floating-point constant
Sequence of numeric digits with presence of decimal
points
E.g.
character constant
Only single character enclosed in single quote
E.g.
‘s’,’M’,’3’
String constants
Sequence of characters enclosed in double quotes
E.g.
“hai”
“2345”
Specifications of different
constants
Special constants
Sample program with \n \t
#include<stdio.h>
int main()
{
printf("This \a sentence will \t be printed \n in \t\t multi-
line \n\n bye");
return 0;
}
2. Qualified constants:
Qualified constants are created by using const qualifier.
E.g. const char a = ‘A’
The usage of const qualifier places a lock on the variable
after placing the value in it. So we can’t change the value
of the variable a
3. Symbolic constants
Symbolic constants are created with the help of the define
pre-processor directive.
For ex #define PI 3.14 defines PI as a symbolic constant
with the value 3.14.
VARIABLES
Data_type variable_name;
Example:
int age;
char m;
int a, b, c;
Initializing Variables
Variables declared can be assigned or initialized using an assignment
operator ‘=’.
The declaration and initialization can also be done in the same line.
Syntax: variable_name = constant
or
data_type variable_name = constant
DATA TYPES
Operands
An operand specifies an entity on which an operation is to be
performed. It can be a variable name, a constant, a function call.
E.g: x=y%z Here x, y , z are operands
Operator
An operator is a symbol that is used to perform specific
mathematical or logical manipulations.
For e.g, x=y%z Here = , % are the operators
Question:
Find the operators and operands on following
i++
--j
K=x*b/z
Ans:
1. operators: ++ operands I
2. operators: -- operands j
3. operators: =,*,/ operands k,x,b,z
Simple Expressions & Compound Expressions
An expression that has only one operator is known as a
simple expression.
E.g: a+2
An expression that involves more than one operator is called a
compound expression.
E.g: b=2+3*5
Precedence of operators
The precedence rule is used to determine the order of
application of operators in evaluating sub expressions.
Each operator in C has a precedence associated with it.
The operator with the highest precedence is operated first.
Associativity of
operators
The associativity rule is applied when two or more
operators are having same precedence in the sub
expression.
An operator can be left-to-right associative or right-to-
left associative.
Category Operators Associativity Precedence
Level-1
Unary +, -, !, ~, ++, - -, &, sizeof Right to left
Level-2
Multiplicative *, /, % Left to right
Level-3
Additive +, - Left to right
Level-4
Shift << , >> Left to right
Level-5
Relational <, <=, >, >= Left to right
Rules for evaluation of expression
ANS: b
Query 2
#include<stdio.h>
int main()
{
int a=12,b=11,c=19,x,y;
x = a-3%2+c*2/4%2+b/4;
y = a = b+5-b+9/3;
printf("x=%d, y=%d\n",x,y);
return 0;
}
a)x=12,y=11 b)x=14, y=8 c)x=18,y=21 d)x=19,y=26
ans: b
*,/,%
x=12-3%2+19*2/4%2+11/4;
12-3%2+38/4%2+11/4 Y=11+5-11+3=8
12-3%2+9%2+2
12-1+38/4+2
12-1+1+2
14
Query 3
ans: c
Query 4
#include<stdio.h>
int main(void)
{
int a=14,b,c;
a=a%5;
b=a/3;
c=a/5%3;
printf("a=%d, b=%d, c=%d\n",a,b,c);
return 0;
}
a=14%5=4;
b=4/3=1;(a=4)
c=4/5%3=0
0%3=0
5%13 5
Classification of
Operators
Classification based on Number of Operands
Unary Operator: A unary operator operates on only
one operand. Some of the unary operators are,
Operator Meaning
- Minus
++ Increment
-- Decrement
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modular Division
10>3 a
Classification based on role of operands
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Miscellaneous Operators
Arithmetic Operators
They are used to perform arithmetic operations like
addition, subtraction, multiplication, division etc.
A=10 & B=20
Operator Description Example
printf() getchar()
scanf() gets()
putchar()
puts()
Unformatted Functions
3 types I/O functions.
Character I/O
String I/O
File I/O
a) Character I/O:
1. getchar() This function reads a single character data from the standard input.
(E.g. Keyboard)
Syntax :
eg:
char c;
c=getchar();
2. putchar() This function prints one character on the screen at a time.
Syntax :
eg
char c=‘C’;
Example Program
#include <stdio.h>
int main()
{
int i;
char ch;
ch = getchar();
putchar(ch);
return 0;
}
Output:
A
A
b) String I/O
ld Long int
hd Short int
double or Long
lf
double
%ld /li means long int variable
%hd /hi means short int variable
%u address of variable
%u useiged
%x hexa decimal
%d or %i (octal/hexa)for Integer
Input Function scanf( )
It is used to get data in a specified format. It can accept
data of different data types.
Syntax
scanf(“Control String”, var1address, var2address, …);
Format String or Control String is enclosed in quotation
marks. It may contain
1. White Space
2. Ordinary characters
3. Conversion Specifier Field
It is denoted by % followed by conversion code and
#include <stdio.h>
main( )
{
int num1, num2, sum;
printf("Enter two integers: ");
scanf("%d %d",&num1,&num2);
sum=num1+num2;
printf("Sum: %d",sum);
}
Output
Enter two integers: 12 11
Sum: 23
Decision Making and
Branching
Decision making statements in a programming
language help the programmer to transfer the control
from one part to other part of the program.
Flow of control: The order in which the program
statements are executed is known as flow of control. By
default, statements in a c program are executed in a
sequential order.
The default flow of control can be altered by using flow
control statements. These statements are of two types.
Branching statements
T
Statement
#include <stdio.h>
int main()
{
int n;
printf("enter the number:");
scanf("%d",&n);
if(n>0)
printf("the number is positive");
return 0;
}
(ii)The if-else statementF
T
Output:
Enter a number:15
Example:2 To check whether the two given numbers are equal
#include <stdio.h>
int main()
{
int a,b;
printf("Enter a and b: ");
scanf("%d%d",&a,&b);
if(a==b)
printf("a and b are equa");
else
printf("a and b are not equal");
return 0;
}
Output:
Enter a and b: 2 4
iii) Nested if statement
If the body the if statement
contains another if statement, then it is This is if
if (test expression1)
known as nested if statement {
ladder
…..
Syntax: if (test expression)
if (test expression2)
{
{
if (test expression) ….
if (test expression) statement;
if (test expression3)
if (test expression)
{
….
….
statement;
if (test expression n)
else }
}}}
Statement F;
….
iv) Nested if-else statement
#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter three numbers\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf("a is greatest");
}
}
else
{
if(b>c)
printf("b is greatest");
else
printf("C is greates");
}
return 0;
}
v) Switch statement
break; ……
default: break;
printf("Illegal entry"); default:
program statement;
break;
} program statement;
}
return 0;
Example2
#include<stdio.h>
int main()
{
int n;
printf("Enter a number\n");
scanf("%d",&n);
switch(n%2)
{
case 0:printf("EVEN\n");
break;
case 1:printf("ODD\n");
break;
}
return 0;
}
Output:
Enter a number
5
ODD
b)Unconditional
branching statements
i)The goto Statement
This statement does not require any condition. Here
program control is transferred to another part of the
program without testing any condition.
Syntax:
goto label;
label is the position where the control is to be transferred.
#include<stdio.h>
int main()
{
printf("www.");
goto x;
y:
printf("mail");
goto z;
x:
printf("yahoo");
goto y;
z:
printf(".com");
return 0;
}
b)break statement
A break statement can appear only inside a body of , a switch or a loop
A break statement terminates the execution of the nearest enclosing loop or switch.
break;
#include<stdio.h>
int main()
{
int c=1;
while(c<=5)
{
if (c==3)
break;
printf("\t %d",c);
c++;
}
return 0;
}
Example :2
#include<stdio.h>
int main()
{
int i;
for(i=0;i<=10;i++)
{
if (i==5)
break;
printf(" %d",i);
}
return 0;
}
iii) continue statement
A continue statement can appear only inside a loop.
A continue statement terminates the current iteration of the nearest enclosing loop.
Syntax:
continue;
Example :1
#include<stdio.h>
int main()
{
int i;
for(i=0;i<=10;i++)
{
if (i==5)
continue;
printf(" %d",i);
}
return 0;
}
iv) return statement:
A return statement terminates the
execution of a function and returns the control to
the calling function.
Syntax:
return;