You are on page 1of 14

C

WELCOME TO THE BOOTCAMP


Printing
Reading
int main(void) {
int nHrs ;
float profit;
char c;

scanf("%d",&nHrs);
scanf("%f",&profit);
scanf("%c",&c);
}
Data Types
#include<stdio.h> //standard i\o header file
void main()
{
//declaration
int i;
//initilization
i=1;
printf("i=%d \n",i);
double d=3.5;
i=d;
printf("i=%d \n",i);//will print integer (casting)
printf("d=%lf \n",d);
d=4; //casting to float
printf("d=%lf \n",d);

char c='A';// not double quotes


printf("c=%c \n",c);
}
Data Types
#include<stdio.h> //standard i\o header file
#include<stdbool.h> // for boolean
#define PI 3.14
void main()
{
bool x=true;
double d;
printf("x value =%d \n",x);

char f []="hello world";

//print formatted
printf("%s",f);

//////assignment
const int h=10;
d=PI;// from # define

}
Operators Shorthand
Operators
void main()
{
int x=2;
int y=3;
printf("div= %d\n",x/y);//zero
printf("div= %d\n",y/x);//one
printf("rem= %d\n",x%y);//2
printf("5/2(lf)= %lf \n",5/2);
printf("5/2(d)= %d \n",5/2);
printf("5.0/2(d)= %lf\n",5.0/2);//make one of num double
printf("x/y(d)= %d\n",x/y);//2/3
x+=2;//x=x+2
x*=2+y;//x=x*(2+b)

}
Operators
void main()
{
int i=0;
i+=1;//1
i++;//2 post inc
printf("i=%d \n",i);
++i;//3 //pre inc same like post
printf("i=%d \n",i);
printf("i++=%d \n",i++);//print then inc (3)
printf("++i=%d \n",++i);//inc then print 5

int h=i++; //assign to h then inc


printf("h= %d i =%d \n",h,i);//h=5,i=6
int k=++i;//inc then assign
printf("k= %d i =%d \n",k,i);//k=7,i=7
}
Casting
• int main(void) {
• int nHrs = 40;
• int nDays = 7;

• float avg = nHrs/(float)nDays; //result=5.7


• float avg = nHrs/nDays; //result =5 (integer division)

• printf("%d hours in %d days\n", nHrs, nDays);


• printf("work %.1f hours per day!\n", avg);
• }
Conditional Statements
In C, however, there are no distinct
values for true or false, instead, false is 0,
and anything which is non-zero is true.

 The table above shows the C operators


for conditional expressions. The first six
(==, !=, <, <=, >, and >=) are relational
operators—they compare two
expressions for equality or inequality.

For any of these operators, both


operands (the expressions on the left and
right) are evaluated to a value, then
compared appropriately. The operator
then produces a true or false value.
Conditional Statements
 The && and || operators perform the
logical AND and logical OR operations
respectively.

 The logical AND of two values is true if


and only if both values are true,
otherwise it is false. The logical OR of
two values is true if and only if either
of the values are true, otherwise it is
false.
 && and || may know their answer from
only one argument. In the case of &&,
if either operand is false, then the
result is false, regardless of the other
value. Similarly for ||, if either operand
is true, then the result is true
regardless of the other value. they may
only evaluate one operand.
If/else
void main()
• {
int x=0;
//nested if else
if (x>5)//big if
{
if(x<10)// rewrite if (x>5 && x<10)
printf("x>5 && x<10 \n");
else
printf("x>5 && x>10 \n");
}
else//follow big if
if (x<10)
{
printf("x<10 \n");
printf("x<5 \n");
else if(x==10)
printf("x==10 \n");
}
else
}
printf("x>10 \n");
Examples
void main()//checking for positive void main()//checking for relationship
number
{ • int x=10;
{
int x=10,y=5;
if (x>0) if (x>y)
printf("x is greater\n");
printf("x is poistive\n"); else if(x<y)
else printf(" y is greater \n");
printf(" x is negative \n"); else
} printf(" they are equal\n");

}
Exercise
• Post and pre increment
• https://webrewrite.com/c-objective-questions-answers-operators-set-1/

• Problems on conditional statements


• Write a C program to check whether a given number is even or odd.

• Write a C program to read temperature in centigrade and display a suitable


message according to the temperature state below:
Temp < 0 then Freezing weather
Temp 0-10 then Very Cold weather
Temp 10-20 then Cold weather
Temp 20-30 then Normal in Temp

You might also like