Professional Documents
Culture Documents
Structure Of C Programming
C-programming start with a keyword main().Keyword mean language own words. main()
contains a block latter of codes / statement or instruction. Each Statement terminate
with semi-colon (;).
Data
Symbol where data can be performed.
i. Integer data:- A number with no decimal point, e.g
2,3,4,5,6………..;
ii. Float data :- Number with decimal point, e.g
2.2,3.4,9.9,……….:
iii. Character data:- Character data consist of single character . e.g
‘a’, ‘f’, ‘3’, ‘+’, ‘_’…………;
iv. Stream data:- It is combination of character. e.g
MALIK AsGHaR, Abc,123,………….;
Output Statement
It is use to display output statement on screen and it is denoted by printf()
1) Display string :-
Using format specifiers we can print string constant as well as numbers.
General Syntax:-
main()
{
printf(“String”);
}
p1. Display your name .
main()
{
printf("Malik Asghar");
}
p2. Display :-
Welcome
To
Gomal University.
main()
{
printf("Welcome to Gomal University");
2) Display Integer:-
%d specifiers is used for show input or output of integer data.
General Syntax:-
main()
{
printf(“%d”, integer value);
}
p3. Display integer 5.
main()
{
printf(“%d”, 5);
}
p4. Display two integer 4 and 8 .
main()
{
printf("%d%d",4,8);
}
3) Display Float :-
%f specifeir use for display float number .
General syntax :
main()
{
Printf(“%f”,float number );
}
p5. Display 3.4
main()
{
printf("%f",4.4);
}
p6. Display following output :
4.5 , 3.2
2,7
1
main()
{
printf("%f,%f\n%d %d\n%d",4.5,3.2,2,7,1);
}
4) Display character data :
%c is specifier is used to display character data.
General syntax:
main()
{
Printf(“%c”,’char value’);
}
p7. Display a character :-
main()
{
printf("%c",'*');
}
p8. Display following output :
***
**
*
main()
{
printf("%c%c%c\n%c%c\n%c",'*','*','*','*','*','*');
}
p9. Display following output:
Square of 3 = 9.
main()
{
printf("Square of %d %c %d",3,'=',9);
}
p10. Display following output:
A=3;
B=4.5;
main()
{
printf("A %c %d\nB %c %f",'=',3,'=',4.5);
}
Variable
A variable is a set space in computer memory set aside for a certain kind of data and given
name for easily reference . variable are used so that the same space in memory can bolt
different value at different times .
Rules for variable name :
First latter must be alphabetic.
No space is allowed.
No special char (+,-,=,…….) can be used with variable name expect underscore (_).
No reserved word can be uses a variable name . i.e, for, main, printf, scanf etc.
Types Of Variable :-
1) Integer variable :-
Here we can store an integer in variable .
General syntax:
int variable name,
variable ‘int’ is a reserved wors and variable name is a user define word.
int variable has two bytes.
Its range is -32768 to 32767.
General syntax:
Long int V1.
It has four bytes .
Its rage is -2147483648 to 2147483647.
p11. Store 150 in a variable and display its value .
main()
{
int a;
a=150;
printf("%d",a);
}
p12. Display Square of 4 = 16.
main()
{
int a,b;
a=4;
b=16;
printf("Square of %d = %d",a,b);
}
2) Float variable :-
It is use to store decimal point numbers.
General synatax :-
float variable ;
it has four bytes in memory.
Maximam number = 1038
Minimum number = 10-38
General synatax :-
it has eight bytes in memory.
Maximam number = 10308
Minimum number = 10-308
p13. Display following output “a=4.5 and b=3.2”.
main()
{
float a,b;
a= 4.5;
b= 3.2;
printf("a = %f and b = %f",a,b);
}
3) Character variable :
It can hold single bytes.
Only single character can be show .
p14. Display ; “ ch1 = + and ch2 =S.
main()
{
char ch1,ch2;
ch1='+';
ch2='S';
printf("ch1 = %c and ch2 = %c",ch1,ch2);
}
INPUT FUNCTIONS
scanf() function is a input function through which we can enter data with the help of
keyboard .
General syntax:-
Scanf(“specifier”,&var);
p15. Read and display an integers:-
main()
{int n;
printf("Please Enter an Integer:");
scanf("%d",&n);
printf("n = %d",n);
}
p16. Read and display an float number:-
main()
{
int s;
printf("Please Enter an float:");
scanf("%f",&s);
printf("s = %f",s);
}
p17. Read and display character data:-
main()
{
int z;
printf("Please Enter an char :");
scanf("%c",&z);
printf("z = %c",z);
}
p18. Display following output:
Square of 4 = 16.
main()
{
int n,sq;
printf("Enter Number");
scanf("%d",&n);
printf("Enter Square of %d :",n);
scanf("%d",&sq);
printf("Square of %d = %d",n,sq);
}
Operator
Operators are words or symbol that cause program to do something to variable there
are many different types of operators.
A. Arithmetic Operator .
B. Arithmetic Assignment Operator .
C. Increment /Decrement Operator.
D. Relational Operator
E. Logical Operator.
F. Ternary Operator.
A) Arithmetic Operator :-
Arithmetic operator we use the multiplication ( * ) to multiply two number together . C
use the four arithmetic operator that are common in most programming language and
one, the reminder operator which is not common .
i. + Operator.
ii. - Operator.
iii. * Operator.
iv. | Operator.
v. % (mod) Remainder Operator.
i. Addition Operator:-
it is used to add two are more operands. e.g
c=a+b;
it means Add ‘a’ with ‘b’ and assign value in c.
ii. Subtraction Operator:-
iii. it is used for subtract two are more operands. e.g
c=a-b;
it means Subtarct ‘b’ from ‘a’ and store result in c.
iv. Multiplication operator:-
it is used to add two are more operands. e.g
c=a+b;
it means Add ‘a’ with ‘b’ and assign value in c.
v. Division operator:-
it is used for division. There are two types of division.
float divisionand Integer division.
float division:-
e.g float a=9;
float b=2;
c=a|b; the result of ‘a and b’ is ‘4.5’ in this type of division is called float division.
integer division:-
int a=9;
int b=2;
c=a|b; the result of ‘a and b’ is ‘4’ in integer division.
Or
int a=2;
int b=9;
c=a|b; the result of ‘a and b’ is ‘0’ in integer division.
vi. Mod or Reminder operator :-
It is used to find the reiminder e.g
int a= 9;
int b=2;
c=a%b; in this situation the result of ‘a and b’ is ‘1’.
p19. Display sum of two given numbers:-
main()
{
int a,b,c;
printf("Enter Two Number:");
scanf("%d %d",&a,&b);
c=a+b;
printf("%d + %d = %d",a,b,c);
}
p20. Display sum of three given numbers:-
main()
{
int a,b,c,d;
printf("Enter Three Numbers:");
scanf("%d %d %d",&a,&b,&c);d=a+b+c;
printf("%d+%d+%d = %d",a,b,c,d);
}
p21. Display subtraction of two given numbers:-
main()
{
int a,b,c;
printf("Enter Two Number:");
scanf("%d %d",&a,&b);
c=a-b;
printf("%d-%d = %d",a,b,c);
}
p22. Display product of two given numbers:-
main()
{
int a,b,c;
printf("Enter Two Number:");
scanf("%d %d",&a,&b);
c=a*b;
printf("%d*%d = %d",a,b,c);
}
p23. Display division of two numbers:-
main()
{
int a,b,c;
printf("Enter Two Number:");
scanf("%d %d",&a,&b);
c=a/b;
printf("%d/%d = %d",a,b,c);
}
p24. Display reminder of two given numbers:-
main()
{
int a,b,c;
printf("Enter Two Number:");
scanf("%d %d",&a,&b);
c=a%b;
printf("%d mod %d = %d",a,b,c);
}
p25. Display square of given numbers:-
main()
{
int n,sq;
printf("Enter Number:");
scanf("%d",&n);
sq=n*n;
printf("%d * %d = %d",n,n,sq);
}
p26. Display cube of given numbers:-
main()
{
int n,sq;
printf("Enter Number:");
scanf("%d",&n);
p27. sq=n*n*n;
printf("%d * %d * %d= %d",n,n,n,sq);
}
p28. Covert given celisuc to forhrenhit :-
p29. Calculate following equation:-
c = a2+b2 where a =3 and b =4.
main()
{
int a,b,c;
printf("Enter Two Number:");
scanf("%d %d",&a,&b);c=a*a+b*b;
printf("c=%d * %d + %d * %d = %d",a,a,b,b,c);
}
B. Arithmetic Assignment Operators:-
i. + operators:-
Add and assignment operator its add right operand to left operands .
c +=a is equivalent to c = c+a. e.g
suppose : a=a+2;
alternate : a+=2;
ii. - Assignment operator:-
Subtract and assignment operator , subtract right operands fro the left operands and assign result to left
operands . c-=a; is equivalent to c=c-a; e.g
suppose : a=a-2;
alternate : a-=2;
iii. * Assignment operators :-
Multiply and assignment operator , its multiplies right operands with left operand
and assign the result in left operands . c*=a; is equivalent to c=c*a;. e.g
suppose : a=a*2;
alternate : a*=2;
iv. | Assignment operators :-
Divide and assignment operator, it divides left operand with the right operands and
assign result to left operands c|=a; is equivalent to c = c|a. e.g
suppose : a=a|b;
alternate : a|=b;
v. % (mod) Assignment operator :-
Modulus And assignment operators , it takes modulus using two operands and
assign the result to left operands. c%=a; is equivalent to c%=a; . e.g
suppose : a=a%b;
alternate : a%=b;.
AND:-
It is denoted by &&.
The following diagram cleared that “ Result will be true (1) when all
conditions become true“.
NOT:-
It is denoted by ! .
It operator convert true to false and false to true .
Ex : a =3; and b =4;
c = !(a<b); Result: c =0;
Here the value of ‘a’ is less then the value of ‘b’ that’s result is true ,, but use
to the NOT operator that’s convert result to true to false and false to true.
Therefore the result of c is false (0).
Decision Statement
In C-language there are three major descision statement.
a. if – statement.
b. if – else statement.
c. Switch statement.
a. if – statement:-
General syntax:-
if(condition)
statement; Block or body of ‘ if ’.
when condition is true then the compiler will show execute “ block -of- of ”,
otherwise compiler will ignore “ block - of – if ”.
If “ block - of – if ” has more then one then braces are necessary .
General syntax:-
if (condition)
{
Statement 1; “ block - of – if ”
Statement 2;
}
Flow chart:-
if
Condition
Block of if Next statement
after block
000000000000000000000000000000000000000000000000000000000000000000