Operators and Control Statement
Operators and Control Statement
Operators and Control Statement
LECTURE 2
C is a high-level language.
Keywords
char, static, if , while, return ..................... Total= about 32
Data Types
int , char, float,
Double, char
Arithmetic Operators
+ (Plus), - (Minus), *
(Multiplication),
/(Division),(Modulus) %
My first C program!
4
#include <stdio.h>
// program prints hello world
int main() {
printf ("Hello world!");
return 0;
}
#include <stdio.h>
// program prints a number of type int
int main() {
int number = 4;
printf (“Number is:”, number);
return 0;
}
Output: Number is 4
Example 2
6
#include <stdio.h>
// program reads and prints the same thing
int main() {
int number ;
printf (“ Enter a Number: ”);
scanf (“%d”, &number);
printf (“Number is %d\n”, number);
return 0;
}
#include <stdio.h>
int main() {
int a = 4; //first number
int b = 5; //second number
int sum;
Scanf(“%d”,&a);
Scanf(“%d”,&b);
sum= a + b;
Printf(“the sum=%d”,sum);
}
Assignment Operator
8
Note
9
Errors
Compilation
Compiler generally gives the line number at
which the error is present.
Run time
C programs are sequential making the
debugging easier.
Some more Data Types
10
User Defined:
typedef: used to rename a data type
typedef int integer; can use integer to declare an int.
enum, struct, union
Some more Arithmetic Operators
11
Some more Arithmetic Operators
12
int main( )
{
int x=10;
}
Example 2
14
int main( )
{
int x=10;
}
Contd…
15
Modulus (remainder): %
example:
12%5 = 2;
Assignment by addition: +=
example:
int a=4;
a+=1; //(means a=a+1) value of a becomes 5
Meaning of a + b * c ?
is it a+(b*c) or (a+b)*c ?
All operators have precedence over each other
*, / have more precedence over +, - .
If both *, / are used, associativity comes into
picture. (more on this later)
example :
5+4*3 = 5+12= 17.
Precedence Table
21
Highest on top
++ -- (Postfix)
++ -- (Prefix)
* / %
+ -
<< >>
< >
&
|
&&
||
Input / Output
22
& in scanf.
Itis used to access the address of the variable used.
example:
scanf(%d,&a);
we are reading into the address of a.
Data Hierarchy.
example:
int value can be assigned to float not vice-versa.
Type casting
Typecasting
24
int main( )
{
int value1 = 10, value2 = 3;
float result;
result = (float)value1 / value2;
printf("Result with type casting: %f", result);
return 0;
}
Decision control Statement
26
if statements
if else statements
nested if statements
the if statement
28
Example
29
int main()
{
int m=40,
int n=40;
if (m == n)
{
printf("m and n are equal");
}
}
The if else statement
30
Example
31
#include <stdio.h>
int main()
{
int m=40,n=20;
if (m == n)
{
printf("m and n are equal");
}
else
{
printf("m and n are not equal");
}
}
The nested if
32
Example
33
#include <stdio.h>
int main()
{
int m=40,n=20;
if (m>n) {
printf("m is greater than n");
}
else if(m<n) {
printf("m is less than n");
}
else {
printf("m is equal to n");
}}
Exercise 1: what would be the output of the following
programs
34
(a) main( )
{
int x = 10, y = 20 ;
if ( x == y ) ;
printf ( "\n%d %d", x, y ) ;
}
(b) main( )
{
int x = 3, y = 5 ;
if ( x == 3 )
printf ( "\n%d", x ) ;
else ;
printf ( "\n%d", y ) ;
}
35
(c) main( )
{
int x = 3 ;
float y = 3.0 ;
if ( x == y )
printf ( "\nx and y are equal" ) ;
else
printf ( "\nx and y are not equal" ) ;
}
36
(d)main( )
{
int i = 65 ;
char j = ‘A’ ;
if ( i == j )
printf ( “C is WOW” ) ;
else
printf( "C is a headache" ) ;
}
Exercise 2
37
QUESTIONS