C Programming Language
C Programming Language
C Programming Language
Programming
language
Presented by :
Shafika Rahman(ID:193120004)
Tamanna Tabassum Oishi(ID:193120006)
Kazi Habiba(ID:193120011)
Jannatul Ferdous Sayma(ID:192120008)
Introduction:
The C language facilities a structured and disciplined approach to computer
program design.
return 0;
}
❑Output:
Welcome
to
C!
#include < stdio .h>
is a direction to the C preprocessor .
Here <stdio.h> tells the preprocessor to include the contents of the “standard
input/output header ” in the programe.
• main()
is a part of every C program.
•The parantheses after main indicate that main is a program building block called
a function.C programs contain one or more functions one of which must be main.
Every program in C begins executing at the function “main”.
•A left brace “{” begins the body of every function and a corresponding right
brace “}” ends each function.
• In this code ,
this pair of braces and the portion of the program between the braces is called a
block.
•The entire line including printf ,its argument with in the parentheses called a
statement.
•The backslash (\) is called a escape character. It indicates that printf is supposed
to do something out of the ordinary.the escape sequence “\n” means newline.
Adding Two Integers :
Input: Output:
sum=n1+n2;
printf("sum=%d",sum);
}
Adding Two Integers in C Program:
•All variables must be defined with a name and a data type immediately after
the left brace that begins the body of main before they can be used in a
program.
•The preceding definitions could have been combined into a single definition
statement as follows:
int integer1, integer2, sum;
Special characters(, . \ % $ / ;)
White spaces
Keywords:
Identifiers
Both uppercase and lowercase letters are commonly used. The underscore
character is also permitted in identifiers.
constants
Variables
First_tag Yes
average_number Yes
Keyword may be a
int_type Yes
part of the name
Data type
Operators and Expressions
•An operator is a symbol that tells the computer to perform certain mathematical or
logical manipulations .
Operators are used in programs to manipulate data and variables.
They include:
1.Arithmetic operators(+, -, *, / )
2.Relation operators(<,<=,>,>=,==,!=)
3.Logical operators(&&,||,!)
4.Assignment operators(v = variable,exp = an expression op = C arithmetic operator
5.Increment and decrement operators(++ and --)
6.Conditional operators(where exp1, exp2, exp3 are expressions)
7.Bitwise operators( &, ^ ,| , << ,>>)
8.Special operators(comma operator, sizeof operator, pointer operators (& and *)
and member selection operators(. and ->).
Arithmetic operators
Celsius to Farenhiet and Celsius to Farenhiet
Input:
0utput:
#include <stdio.h>
#define PT 3.1416
int main ()
{ float C,F;
enter the value of C=32
printf("enter the value of C=");
scanf("%f",&C);
temperature in F=89.599998
C=(5*F-160)/9;
printf("temperature in C=%f\n",C);
}
Arithmetic operators
Addition,substruction,multiplication and division :
INPUT: OUTPUT:
#include <stdio.h>
int main () enter two numbers
{ 20 10
int X,Y,add,subtract,multiply; sum=30
float division; subtract=10
printf("enter two numbers\n");
multiply=200
scanf("%d%d",&X,&Y);
division=2.00
add=X+Y;
subtract=X-Y;
multiply=X*Y;
division=X/(float)Y;
printf("sum=%d\n",add);
printf("subtract=%d\n",subtract);
printf("multiply=%d\n",multiply);
printf("division=%.2f\n",division);
return 0;
}
Arithmetic operators
Calculate the price of egg
Input:
return 0;
}
Arithmetic operators
Convert days into years,months and days
Input:
Output:
#include <stdio.h>
main ()
Enter the number of days
{
int days,months,years; 400
printf("Enter the number of days\n"); years=1
scanf("%d",&days); months=1
days=5
years=days/365;
days=days%365; Process returned 0 (0x0)
months=days/30; execution time : 12.059 s
days=days%30;
Press any key to continue.
printf("years=%d\n",years);
printf("months=%d\n",months);
printf("days=%d\n",days);
return 0;
}
Arithmetic operators
The sum of three digit number in C
Input:
include <stdio.h>
Output:
main ()
{ int r,r1,r2,r3,sum; enter three digit number
567
printf("enter three digit number\n"); sum of three digits=18
scanf("%d",&r);
Process returned 0 (0x0)
r1=r/100;
execution time : 9.719 s
r=r%100;
r2=r/10; Press any key to continue.
r3=r%10;
sum=r1+r2+r3;
return 0;
}
Relation operators
Example of relational operators:
Input:
Output:
#include <stdio,h> 0
int main() Process returned 0 (0x0)
{ execution time :0.090 s
int a=2,b=7,c=10; Press any key to continue
c=a==b;
printf("%d",c);
return 0;
}
Logical operators
Input: Output:
#include <stdio.h>
int main() Or If Block Gets Executed
{ And If Block Gets Executed
int num1 = 30; Not If Block Gets Executed
int num2 = 40;
Process returned 0 (0x0)
if(num1>=40 || num2>=40)
execution time : 0.078 s
printf("Or If Block Gets Executed\n");
Press any key to continue.
if(num1>=20 && num2>=20)
printf("And If Block Gets Executed\n");
if( !(num1>=40))
printf("Not If Block Gets Executed\n");
return(0);
}
Assignment operators
Input: Output:
#include <stdio.h>
#define N 100 2
#define A 2 4
16
int main()
{ Process returned 0 (0x0) execution time :
int a; 0.125 s
Press any key to continue.
a = A;
while( a < N )
{
printf("%d\n", a);
a *= a;
}
}
Increment and decrement operators
Example of increment and decrement operators:
Input: Output:
#include <stdio.h>
y=111
int main()
y=113
{
x=14
int x=10;
int y;
Process returned 0 (0x0) execution
time : 0.125 s
printf("y=%d\n",++x+100);
Press any key to continue.
x++;
++x;
printf("y=%d\n",100+x++);
printf("x=%d\n",x);
}
Conditional operators
}
Example of conditional operators:
Input: Output:
printf("enter a year:\n");
scanf("%d",&years);
(years%4==0&&years%100!=0)?printf("This
year is Leap
Year\n"):(years%400==0)?printf("THis year is
Leap Year\n"):printf("This year is not Leap
Year\n");
}
Bitwise operators
Example of bitwise operators:
Input: Output:
Input: Output:
#include <stdio.h>
4
main ()
{ Process returned 0 (0x0)
int X; execution time : 0.140 s
printf("%d",sizeof (X)); Press any key to continue.
return 0;
}
Getchar function
Example of getchar function:
Input:
Output:
#include <stdio.h>
Would you like to know my name?
Type Y for YES and N for NO:
main() Y
{
char answer; My name is BUSY BEE
Input:
Output:
#include <stdio.h>
Enter an alphabet
#include <ctype.h>
a
main() A
{ Enter an alphabet
char alphabet; Q
printf("Enter an alphabet"); q
putchar('\n'); /* move to next line */ Enter an alphabet
alphabet = getchar(); z
if (islower(alphabet)) Z
putchar(toupper(alphabet));
else
putchar(tolower(alphabet));
}
If - statement
Input:
main() Output:
{
int a, b, c, d; Enter four integer values
float ratio; 12 23 34 45
Ratio = -3.181818
printf("Enter four integer values\n");
scanf("%d %d %d %d", &a, &b, &c, &d); Enter four integer values
12 23 34 34
if (c-d != 0) /* Execute statement block */
{
ratio = (float)(a+b)/(float)(c-d);
printf("Ratio = %f\n", ratio);
}
}
If-else-if - statement
Input:
main()
{
float A, B, C;
Output:
printf("Enter three values\n");
scanf("%f %f %f", &A, &B, &C);
1.for loop
2.While loop
Input:
Output:
#include<stdio.h>
int main()
{ int i,sum=0; sum=5050
for(i=1;i<=100;i++)
{ Process returned 0 (0x0)
sum=sum+i; execution time : 0.094 s
} Press any key to continue.
printf("sum=%d",sum);
return 0;
}
Example of while loop:
Input:
Output:
#include <stdio.h> sum=5050
int main()
{ int i=1,sum=0; Process returned 0 (0x0)
while(i<=100) execution time : 0.125 s
{ Press any key to continue.
sum=sum+i;
i++;
}
printf("sum=%d",sum);
}
Example of do-while loop:
Input:
Output:
#include <stdio.h>
sum=5050
int main()
{ int i=1,sum=0;
Process returned 0
do
(0x0) execution
{
time : 0.062 s
sum=sum+i;
Press any key to
i++;
continue.
}
while(i<=100);
printf("sum=%d",sum);
}
Thank
you