Unit2 PDF
Unit2 PDF
Overview of C
#include<stdio.h>
int main( )
{
}
Preprocessor Statements:
#include <stdio.h>
#define PI 3.14
void main()
{
printf("%f",PI);
}
Rules for Constructing Integer Constants
Real constants are often called Floating Point constants. The real
constants could be written in two forms—Fractional form and
Exponential form.
•A real constant must have at least one digit.
•It must have a decimal point.
•It could be either positive or negative.
•Default sign is positive.
•No commas or blanks are allowed within a real constant.
Ex.: +325.34
426.0
-32.76
-48.5792
Rules for Constructing Character Constants
Ex.: 'A'
'I'
'5'
'='
C Variables
An entity that may vary during program execution is called a variable.
Variable names are names given to locations in memory.
#include <stdio.h>
void main()
{ int res;
res = printf("Hello\n");
printf("Total printed characters are: %d\n",res);
}
Return type and value of scanf function
#include <stdio.h>
int main()
{ int x,y;
int res;
printf("Enter two number: ");
res=scanf("%d%d", &x,&y);
printf("Total inputs are: %d\n",res);
return 0;
}
Data Type Range Bytes Format
signed char -128 to + 127 1 %c
unsigned char 0 to 255 1 %c
short signed int -32768 to +32767 2 %d
short unsigned int 0 to 65535 2 %u
signed int -32768 to +32767 2 %d
unsigned int 0 to 65535 2 %u
long signed int -2147483648 to 4 %ld
+2147483647
long unsigned int 0 to 4294967295 4 %lu
float -3.4e38 to 4 %f
+3.4e38
double -1.7e308 to 8 %lf
+1.7e308
long double -1.7e4932 to 10 %Lf
+1.7e4932
Note: The sizes and ranges of int, short and long are compiler dependent. Sizes in this figure are
for 16-bit compiler.
I byte = 8 bits either 0 or 1
Formula is 2n-1
216-1 = 65536-1=65535
Range is 0 to 65535
singed
0 to 65535 integer
-2n-1 to 2n-1 -1
-215 to 215 -1
-32768 to +32767
Identifiers : Identifiers are the name of functions, variables and
arrays. The identifiers are user-defined words in the C language.
These can consist of lowercase letters, uppercase letters, digits, or
underscores, but the starting letter should always be either an
alphabet or an underscore.
++x is equivalent to x = x + 1
a = 10, b = 5, c = 0
nonzero values
are regarded as
true and zero
value is
regarded as
false
OR ( II) Operator
This operator gives the net result false, if both the conditions
have the value false, otherwise the result is true.
a = 10, b = 5, c =0
Consider the logical expression(a >= b) I I (b > 15)
This gives result true because one condition is true
Not ( ! ) Operator
a = 10, b = 5, c = 0
! ( a = = 10 )
x=8
y=5
max = a > b ? a : b;
sizeof operator
sizeof is an unary operator. This operator gives the size of
its operand in terms of bytes. The operand can be a
variable, constant or any datatype ( int, float, char etc ).
Output: 1,4
Bitwise Operators
12 = (In Binary)
0 0 0 0 1 1 0 0
25 = (In Binary)
________ & 0 0 0 1 1 0 0 1
8 (In decimal) 0 0 0 0 1 0 0 0
Bitwise OR Operator ( | )
12 0 0 0 0 1 1 0 0
25 0 0 0 1 1 0 0 1
0 0 0 1 1 1 0 1 29
Bitwise XOR (exclusive OR) Operator ^
0 0 1 0 0 0 1 1 35
1 1 0 1 1 1 0 0
#include <stdio.h>
int main() {
printf("Output = %d\n",~35);
printf("Output = %d\n",~-12);
return 0;
}
Output = -36
Output = 11
The bitwise complement of 35 (~35) is -36 instead of 220, but
why?
2+3*5
+ operator has higher precedence than < and =, and < has
more precedence than =, so first a+b will be evaluated,
then < operator will be evaluated, and at last the whole
value will be assigned to x.
a = 2, b = 3, c = 4,
{ l=4;
b=2;
area=l*b;
printf("%d",area) ;
}
if...else
•This is a bi-directional conditional control statement.
•This statement is used to test a condition and take one of the two
possible actions.
Statement l; Statements;
………
}
if(condition)
if(condition)
{
statement l;
statement;
else
………..
statement 2 ;
}
else
{
statement;
……….
}
Program to print a message if negative number is
entered
#include<stdio.h>
Void main ( )
{
int num;
printf("Enter a number”);
scanf("%d“, &num};
if (num<0)
printf ("Number entered is negative);
}
if ( a = 10 )
printf ( "Even this works" ) ;
if ( -5 )
printf ( "Surprisingly even this works" ) ;
yr_of_ser = cy - yoj ;
if ( yr_of_ser > 3 )
{ bonus = 2500 ;
printf ( "Bonus = Rs. %d", bonus ) ;
}
Return 0;
}
In a company an employee is paid as under:
if ( bs < 1500 )
{
hra = bs * 10 / 100 ;
da = bs * 90 / 100 ; }
else
{
hra = 500 ;
da = bs * 98 / 100 ;
}
}
Nesting of if...else
if (condition 1)
{
if (condition 2)
statement A1;
else
statement A2;
}
else
{
if(condition 3)
statement B1
else
statement B2;
}
Program to find largest number from three given numbers
#include<stdio.h>
main( )
{ int a,b,c,large;
printf ("Enter three· numbers ") ;
scanf("%d%d%d",&a,&b,&c) ;
if (a>b)
{ if(a>c)
large=a;
else
large=c;
}
else
{ if (b>c)
large=b;
else
large=c;
}
printf(largest number is %d\n", large) ; }
else if Ladder
This is a type of nesting in which there is an if. ..else statement in every else part
except the last else part. This type of nesting is frequently used in programs and is
also known as else if ladder.
Here each condition is checked, and when a condition is found to be true, the
statements corresponding to that are executed, and the condition comes out of
the nested structure without checking remaining conditions. If none of
theconditions is true then the last else part is executed.
if (per>=85)
grade= 'A' ;
else if(per>=70)
grade='B';
else if (per>=55)
grade= 'C' ;
else if (per>=40)
grade= 'D' ;
else
grade='E';
Switch Case
switch (expression) {
case constant1:
statements;
Switch case statement break;
evaluates a given
expression and based on case constant2:
the evaluated statements;
value(matching a certain break;
condition), it executes .
the statements associated .
with it. Basically, it is .
used to perform different default:
actions based on different // code to be executed if
conditions(cases). // expression doesn't match
any constant
}
*Program that demonstrate switch-case-default*/
void main()
{
int ch;
float n1,n2,res=0;
clrscr();
printf("\n\n **************** WELCOME TO THE WORLD OF
MATHEMATICS************");
printf("\n\n\t1.) Addition");
printf("\n\t2.) Subtraction");
printf("\n\t3.) Multiplication");
printf("\n\t4.) Division");
printf("\n\t5.) Exit");
}
1. C program to check whether a given character is ‘a’ or not.
2. C program to find largest from three numbers given by user to explain working of if-
else-if statement or ladder.
• It enables to alter the flow of the program so that instead instead of writing writing
the same code again and again, we can execute the same code for a finite number
of times.
• Counter
• Increment/decrement
For Loop
for( expressionl; expression2; expression3)
{ statement
statement
}
expression1 is executed only once when the loop starts and is used to Initialize the loop
variables. This expression is generally an assignment expression. Expression2 is a condition
and is tested before each iteration of the loop. This condition generally uses relational and
logical operators. Expression3 is an update expression and is executed each time after the
body of the loop is executed.
Write a C program to print all alphabets from a to z.
Program to find Factorial of a number
#include<stdio.h>
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++){
fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);
return 0;
}
Fibonacci Series
#include <stdio.h>
int main()
{
int n1=0, n2=1, n3, i, number;
printf("Enter the number of elements: “);
scanf(“%d”, &number);
Printf(“%d %d”, n1,n2);
for(i=2;i<number; i++)
{
n3=n1+n2;
printf(“%d”, n3);
n1=n2;
n2=n3;
} return 0;
}
Write a C program to print multiplication table of any number.
#include <stdio.h>
int main() {
int n;
printf("Enter an integer: ");
scanf("%d", &n);
void main()
{
int i=0,sum=0;
while (i<=10)
{
sum=sum+i;
i=i+1; //condition updated
}
Printf(“\n sum =%d”,sum)
}
Program to print numbers in reverse order with a difference
of 2
int k=10;
while(k>=2)
{
Output 10 8 6 4 2
Program to' print the sum of digits of any number
int main()
{
int n, sum=0, rem
printf ("Enter the number”)
scanf("%d, &n);
while(n>0)
{
rem=n%10 //taking the last digit of the number
sum=sum+rem;
n=n/10
}
printf ("Sum of digits = %d\n, sum) ;
return 0;
}
output: Enter the number: 1452 Sum of digits = 12
Program to find the product of digits of any number
int main() {
int n, prod=l, rem;
printf ("Enter the number :");
scanf("%d",&n) ;
while(n>0)
{ rem=n%10;
prod=prod *rem;
n=n/10;
}
for(k=1;k<=i;k++)
{
printf("*");
}
printf("\n");
}
}
#include<stdio.h>
int main()
{
int i,j,k;
for(i=3;i>0;i--)
{
for(j=1;j<=i;j++)
{
printf(" ");
}
for(k=3;k>=i;k--)
{
printf("*");
}
printf("\n");
}
}
To check whether a given no. is prime or not
#include <stdio.h>
using namespace std; if (flag==0)
int main() printf( "Number is Prime.“);
{ return 0;
int n, i, m=0, flag=0; }
printf("Enter the Number to check Prime: “);
scanf(“%d”, &n);
m=n/2;
for(i = 2; i <= m; i++)
{
if(n % i == 0)
{
printf("Number is not Prime.“);
flag=1;
break;
}
}
Program to find Prime Number between 1 to 100
#include <stdio.h>
int main ()
{
int i, j;
for(i=2; i<100; i++)
{
for(j=2; j <= (i/j); j++)
{
if(!(i%j))
break;
}
if(j > (i/j))
printf("%d is prime\n", i);
}
return 0;
Break and Continue Statement
C break statement
The break is a keyword in C which is used to bring the program control out of the loop.
The break statement is used inside loops or switch statement.
The break statement breaks the loop one by one, i.e., in the case of nested loops, it
breaks the inner loop first and then proceeds to outer loops.
Syntax:
1. //loop or switch case
2. break;
#include<stdio.h>
#include<stdlib.h>
void main ()
{
int i;
for(i = 0; i<10; i++)
{
printf("%d ",i);
if(i == 5)
break;
}
printf("came outside of loop i = %d",i);
}
continue statement :- The continue statement is used inside the body of the loop
statement. It is used when we want to go to the next iteration of the loop after skipping
some of the statements the loop.
It is generally used with a condition. When a continue statement is encountered all the
remaining statements (statements after continue) in the current iteration are not executed
and the loop continues with the next iteration.
The difference between break and continue is that when a break statement is encountered
the loop terminates and the control is transferred to the next statement following the
loop, but when a continue statement is encountered the loop is not terminated and the
control is transferred to the beginning of the loop