0% found this document useful (0 votes)
46 views41 pages

C Programming Language

The document introduces C programming and presents several examples that illustrate important features of C. It begins by considering a simple C program that prints multiple lines with a single printf statement by using newline characters. It then provides examples that demonstrate arithmetic operators by adding two integers, converting between Celsius and Fahrenheit temperatures, and calculating the price of eggs. Further examples show converting days to years, months and days, and finding the sum of digits in a three-digit number. The document also discusses relational operators with a simple example.

Uploaded by

kazi habiba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views41 pages

C Programming Language

The document introduces C programming and presents several examples that illustrate important features of C. It begins by considering a simple C program that prints multiple lines with a single printf statement by using newline characters. It then provides examples that demonstrate arithmetic operators by adding two integers, converting between Celsius and Fahrenheit temperatures, and calculating the price of eggs. Further examples show converting days to years, months and days, and finding the sum of digits in a three-digit number. The document also discusses relational operators with a simple example.

Uploaded by

kazi habiba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

C

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.

In this presentation we introduce C programming and present several examples


that illustrate many important features of C.

We begin by considering a simple C program .


❑A simple C program: Printing multiple lines with a single printf
•One printf can print several lines by using
additional newline characters .

•Each time the \n (newline) escape sequence


is encountered, output continues at the
beginning of the next line
❑ Input :
#include <stdio.h>

int main (void)


{
printf("welcome\n to\n c!\n");

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:

#include <stdio.h> enter the two values


20 10
main() sum=30
{
int n1,n2,sum=0;
printf("enter the two values\n");
scanf("%d%d",&n1,&n2);

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;

•A variable name in C is any valid identifier.

•An identifier is a series of characters consisting of letters, digits and


underscores (_) that does not begin with a digitAn identifier can be of any
length, but only the first 31 characters are required to be recognized by C
compilers according to the C standard.

•C is case sensitive—uppercase and lowercase letters are different in C, so a1


and A1 are different identifiers.
Character set

The characters in C are grouped into the following categories:

Letters (Uppercase: A….Z),(Lowercase: a…..z)

Digits (All decimal digits 0—9)

Special characters(, . \ % $ / ;)

White spaces
Keywords:
Identifiers

Identifiers refer to the names of variables, functions and arrays.


These are user-defined names and consists of a sequence of letters, and
digits, with a letter as a first character.

Both uppercase and lowercase letters are commonly used. The underscore
character is also permitted in identifiers.
constants
Variables

Variable name Valid? Remarks

First_tag Yes

char No char is a keyword

Price$ No Dollar sign is illegal

Blank space is not


group one No
permitted

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

F=(9*C)/5+32; enter the value of F=-40


printf("temperature in F=%f\n",F); temperature in C=-40.000000

printf("enter the value of F=");


scanf("%f",&F);

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:

include <stdio.h> Output:


int main ()
{
int price,amount,dozen,taka,unit_price;
enter the price of one dozen egg
120
printf("enter the price of one dozen egg\n");
how many eggs you want to buy??
scanf("%d",&price);
4
you have to pay 40
unit_price=price/12;
Process returned 0 (0x0) execution
printf("how many eggs you want to buy??\n");
time : 27.518 s
scanf("%d",&amount);
Press any key to continue.
taka=unit_price*amount;
printf("you have to pay %d",taka);

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;

printf("sum of three digits=%d\n",sum);

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

Example of 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

Example of 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:
#include <stdio.h>
enter a number : 39
main() odd number
{
int x; Process returned 0 (0x0)
execution time : 22.121 s
printf("enter a number : "); Press any key to continue.
scanf("%d",&x);

x%2==0? printf("even number\n"):


printf("odd number\n");

}
Example of conditional operators:

Input: Output:

#include<stdio.h> enter a year:


main() 2007
{ This is not a leap year
int years,n;

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:

#include <stdio.h> Right shift by 1: 1


int main()
{ Left shift by 1: 4
int num=2,i=1;
printf("Right shift by %d: %d\n",i, Process returned 0 (0x0) execution
num >> i); time : 0.203 s
printf("\n"); Press any key to continue.
printf("Left shift by %d:
%d\n",i,num << i);
return 0;
}
Speacial operators
Example of size of operators:

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

printf("Would you like to know my name?\n"); Would you like to know my


printf("Type Y for YES and N for NO: "); name?
Type Y for YES and N for NO:
answer = getchar(); /* .... Reading a character...*/ n

if(answer == 'Y' || answer == 'y') You are good for nothing


printf("\n\nMy name is BUSY BEE\n");
else
printf("\n\nYou are good for nothing\n");
}
Putchar function

Example of putchar function:

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);

printf("\nLargest value is "); if (A>B) Enter three values


{
if (A>C) 23445 67379 88843
printf("%f\n", A);
else Largest value is 88843.000000
printf("%f\n", C);
}
else
{
if (C>B)
printf("%f\n", C);
else
printf("%f\n", B);
}
}
Switch - statement

Code of switch statement:


include <stdio.h>
int main()
{ char ch;
int x,y;
printf("enter an operator:\n");
scanf("%c",&ch);
switch(ch){
case'+':printf("enter two values:\n");
scanf("%d%d",&x,&y);
x+y;
printf("the addition of the values is:%d",x+y);
break;
case'-':printf("enter two values:\n");
scanf("%d%d",&x,&y);
x-y;
printf("the subtraction of the values is:%d",x-y);
break;
case'*':printf("enter two values:\n");
scanf("%d%d",&x,&y);
x*y;
printf("the multiplication of the values is:%d",x*y);
break;
case'/':printf("enter two values:\n");
scanf("%d%d",&x,&y);
x/y;
printf("the division of the values is:%d",x/y);
break;
default:printf("it is not + or - or * or / \n");
}
}
Decision making and looping

Loops or repetitions allows a code segment to be executed for many times.


In programming a loop is used to repeat a block of code until the specified
condition is met.

There are three types of loops:

1.for loop

2.While loop

3.Do while loop


Example of for 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

You might also like