You are on page 1of 60

Muhammad Irfan

Communication:
Means “sharing of information”
1. If both person understand each other its called
effective communication.
2. If one person understand English and other
understand Chinese then one of these have to learn
one of the language to understand each other.
3. If we want to command on Computer we have to
learn Computer language.
History of “C” Language:
1. Machine language
2. Assembly Language
3. FORTRAN
4. COBOL
5. CPL
6. BCPL
7. B
8. C
9. C++
10. Java
Machine Language:
It consists of 0,1
It is also called as pure binary language. Because it
only contains 100% 0,1.
It is also called as;
o LLL ( Low Level Language)
o MOL ( Machine Oriented Language)
It is difficult to understand, learn, write and debug.
A program written in machine language runs fast,
because computer understand 0,1 very easily.
A person who works in machine language is called as
SYSTEM EXPERT.
Assembly Language:
In assembly language 100% of machine language was
taken out of which 20% was converted into English
like symbols called as (Mnemonics).
Where as the remaining 80% was still 0,1.
A translator was used with assembly language which
was capable to convert the 20% Mnemonics into 0,1
form.
That translator was called as Assembler.
A program written in assembly language runs fast but
slow as compared to machine language due to
translator (Assembler).
FORTRAN:
Stands for FORmula TRANslation
It is such a language which is used in a field where we
need fast and accurate calculation.
FORTRAN is used in the following fields;
o Engineering
o Mathematics
o Statistics
o Chemistry
o Physics etc…….
COBOL:
Stands for COmmon Business Oriented Language.
It is such a language in which we can develop
software, which will solve our problems regarding
business.
It can be purely applied to business field.
CPL:
Stands for Combined Programming Language.
75 % portion of FORTRAN and 75% portion of
COBOL were combined with each other.
CPL:
And CPL was introduced.
But it was flopped due to it’s huge size.
BCPL:
Stands for Basic Combined Programming Language.
Basic and important features from FORTRAN and
some basic and important features from COBOL were
combined with each other.
And they introduced a new language called as BCPL.
It got flopped due to its limitation.
“B” Language:
Ken Thompson was researcher at A.T & T’s (American
Telephone & Telegraph’s) Bell Lab.
Ken Thompson wrote language.
Ken Thompson thought what should be the name of
my language.
From Bell he took the “B” and kept the name of
language “B” Language.
Telephone was discovered by “ Alexander Graham
Bell”.
“C” Language:
Denis Ritchie was researcher of AT & T’s Bell Lab.
Denis Ritchie took permission from Ken Thompson
and modified “B” language.
A new language was introduced.
He also thought what should be the name of my
language now.
From alphabetical series after “B” there comes “C”
and he kept the name of language as “C” language.
Denis Ritchie gave C-language to ANSI (American
National Standard Institution).
“C” Language:
ANSI registered C-language that it can work in every
field e.g. Engineering, Mathematics, Business etc…
Programmer at that time left other languages and
adopted C-language.
This language was introduced in 1972.
It is also called as fundamental language.
C-language consists of 90 to 95 % English.
A translator is used that converts that English into
machine understandable form.
“C” Language:
That translator is called as “Compiler”.
There are three categories of language.
o H.L.L ( High Level Language)
o M.L.L ( Middle Level Language)
o L.L.L (Low Level Language)
C-language has the feature of H.L.L and L.L.L, that’s
why also called as M.L.L.
“C” Language Character Set:
1. A… Z
2. a....z
3. #,_,+,-,<,>,=,!.........
Variable:
 A named memory location, which holds something,
is called as variable.
 Variable always get created inside “RAM”.
Variable Types:
 There are three main types of variable;
Variable Types:
1. Integer variable (2 bytes): that space in memory,
which stores integer values is called as integer
variable e.g. (1,2,3.....)
2. Float | Real variable (4 bytes) : That space in
memory which stores float values, called as float
variable, e.g. (9.8, 88.12, 77.4……)
3. Character variable (1 byte): That space in memory,
which stores character values is called as character
variable e.g. (‘1’, ‘A’, ‘@’……..)
Constant:
“That thing, which get store inside variable, is called
as Constant”.
Constant Types:
There are three main types of Constant;
1. Integer Constant: (1,2,3,4,5,6………………..)
2. Float | Real Constant: (9.8, 8.12, 3.12……..)
3. Character Constant: (‘A’, ‘B’, ‘2’, ‘!’,……..)
Note: Any single character enclosed in single commas is
called character Constant, e.g. ‘A’ ‘1’, ‘@’. ‘9’……)
Data Type:
“These are reserved word, use to create or declare
variables of different types inside the memory”.
Data Type: Meaning:
int Stands for Integer, use to make
integer variable.
float Stands for Float, use to make float
variable.
char Stands for Character, use to make
character variable.
Data Type:
How to declare or create variable using Data Type;
Data Type: Variable Name:
int x;
float y;
char c;
Note: Compiler check each data and convert to
machine language, which only consist of 0,1.
Rules for Naming variable:
1. Variable name may consists of;
o Alphabets
o Numbers
o And a special symbol _ .
2. Variable name must start from an alphabet or _.
3. Reserved words can’t be the name of variable.
4. No spaces allowed in the name of variable.
C-Language Programming Structure:
main ()
{
Statement 1;
Statement 2;
Statement 3;
Statement n;
}
Starting for this (main ()) and Ending with } is called
main block.
This symbol ({) is called start of program and This
symbol (})is called end of program.
We will write the C-language program in the editor.
Save the file by any name have extension .c
File name length may not exceed from 8 characters.
File name may not have space inside it.
Output Function:
“If we want to print something over the screen, we will use
the following function;
printf(“Allah”);

main ()
{
clrscr();
printf(“text”);
}
File save by the any name e.g. (first.c).
Now we need to compile our program.
What happens when we compile program; File name.obj

File name.c Translator File name.exe


Source Code (Compiler)
File name.bak
1. .obj is (Machine Readable form)
2. .exe is (Executable file)
3. .bak is (Backup file)
Note: When ever we want to write C-language, we have
to remind three things;

1. Editor or
2. RAM 3. Output
Source code
main()
{
clrscr();
printf(“1st line”);
printf(2nd line”);
printf(3rd line”);
}
Note: when we compile it on the user screen it will come on
one line until that 80 columns finished so for break
through the normal sequence we use (escape sequence).
Escape Sequences: “These are special character,
preceded by \, and are used to break the normal
sequence of printing.”
Escape Sequence; Meaning;
\n Go to next line.
\t Go ahead 8 character (tab)
\a Alert (beep sound)
\” To print “
\’ To print ‘
\\ To print \
main()
{
clrscr();
printf(“\”1st lint\””);
printf(“\n\’2nd line\’”);
printf (“\n\\3rd line\\”);
printf(“\n\n\n\n\t\t\t\t\t\tK\aA\aR\aD\aA\aN);
}
Note: do it practical and check the difference of each
option.
Operators: “Operators are special symbols, on to which
the compiler performs a specific action, the action
performed depends, upon the nature of the operator
used.”

Types: There are three types of operators;


1. Unary Operator,
2. Binary Operator,
3. Ternary Operator.
1. Unary Operators: “That operators which perform
action over single operand, are called as Unary
Operators.”
++ (Increment Operator),
-- (Decrement Operator),
- (Negation),
! (Not).
2. Binary Operators: “That operator which perform
action over two operands, are called as Binary
Operator.”
2. Binary Operators:
Assignment Operator (=)
Arithmetic Operator (+,-,*,/,%)
Relational Operator (<,>,<=,>=,==,!=)
Logical Operator (&&,||,!)
Compound assignment Operator (+=,-=,*=,/=,%=)
3. Ternary Operator: Those operators which perform
action over three operands, are called as Ternary
Operators.”
(?:) The only Ternary Operators used in C-language
Assignment Operator;
Denoted as =
Used to copy whatever on it’s

RIGHT SIDE TO LEFT SIDE


There must be variable on the left side of assignment
operator.
On right side we can write (Variable, Constant, or
Expression)
Format Specifier: “These are character preceded by %,
and are used to;
Access value from variable,
Store value inside variable (During Run Time)
Format Specifier; Meaning;
%d Use with Integer Variable,
%f Use with Float Variable,
%c Use with Character Variable.
Note: Have a look of practical in next slide.
main()
{
int a;
float b;
char c;
clrscr();
a=24;
b=21.10;
c=‘!’;
printf(“%d”,a);
printf(“\n%f”,b);
printf(“\n%c”,c);
}
Simple calculator practical (Statistic)
main()
{
int n1,n2
int sum,sub,mul,div;
n1=100;
n2=5;
sum=n1+n2;
sub=n1-n2;
mul=n1*n2;
div=n1/n2;
clrscr();
printf(“Value of n1 is : %d”,n1);
printf(“\nValue of n2 is : %d”,n2);
printf(“\nAddition is : %d”,sum);
printf(“\nSubtraction is : %d”,sub);
printf(“\nMultiplication : %d”,mul)’
printf(‘\nDivision : %d”,div);
}
Input Function: “In C-language scanf function is used
to store value inside a variable.”
Syntax: “scanf (“Format Specifier”, Address operator
variable name);
e.g. (“%d”,&a);

Note: In next slide (scanf) option practical.


Simple calculator practical (dynamic)
main()
{
int n1,n2,n3;
int res;
clrscr();
printf (“Enter 1st value :”);
scanf(“%d”,&n1);
printf(“\nEnter 2nd value :”);
scanf(“%d”,&n2);
printf(“\nEnter 3rd value :”);
scanf(“”%d”,&n3);
res=(n1+n2)*n3;
printf(“\n%d + %d + %d = %d”,n1,n2,n3,res);
}
Note:
In exam these kind of questions can be included;
E.g. write a program, which accepts, two values from
keyboard, and then prints it’s sum, sub, mul, or div
result.
E.g. write a program, that accepts one value from
keyboard, and then print it’s cube or square result.
Cube: 2*2*2=8 Square:2*2=4
C-language first preferred to breaket ()
C-language second preferred to / *
C-language third preferred to + -
Square practical:
main()
{
int num,res;
clrscr();
printf(“A num plz”);
scanf(“%d”,&num);
res=num*num;
printf(“\nSquare of %d is %d”,num,res);
}
Cube practical:
main()
{
int num,res;
clrscr();
printf(“A num plz”);
scanf(“%d”,&num);
res=num*num*num;
printf(“\nCube of %d is %d”,num,res);
}
How to find circle area:
main()
{
int r,a;
clrscr();
printf(“Enter the rediuse :”);
scanf(“%d”,&r);
a=3.14*r*r;
printf(“\nThe area of circle is %d”,a);
printf(“\nThe area of circle having rediuse %d is %d”,r,a,);
getche();
}
How to find length & width of rectangle:
main()
{
int a,l,w;
clrscr();
printf(“Enter the length :”);
scanf(“%d”,&l);
printf(“Enter the width :”);
scanf(“%d”,&w);
a=l*w;
printf(“\nThe total area of rectangle is :%d”,a);
getche();
}
How to find obtain marks and percentage:
main()
{
int OS,CA,IC,IP;
int total;
float per;
clrscr();
printf(“Operating System :”);
scanf(“%d”,&OS);
printf(“Computer Application :”);
scanf(“%d”,&CA);
printf(“Introduction to Computer :”);
scanf(“%d”,IC);
printf(“Introduction to Programming :”);
scanf(“%d”,&IP);
total=OS+CA+IC+IP;
per=total*100.0/400.0;
printf(“Total marks :%d”,total);
printf(“\nPercentage is :%f”,per);
getche();
}
Enter 4 digits number which gives reverse order number:
main()
{
int num,d1,d2,d3,d4;
clrscr();
printf(“Enter 4 digit number :”);
scanf(“%d”,&num);
d1=num%10;
num=num/10;
d2=num%10;
num=num/10
d3=num%10;
d4=num/10;
printf(“Number in reverse order is :%d%d%d%d”,d1,d2,d3,d4);
getch();
}
If-else: In case of “if” when the condition is true, it perform
some task, and if the condition is false, nothing happens.
e.g.
If(2>1)
{
printf(“hello”);
}
Output: hello
If(2<1)
{
printf(“bye”)
}
Output: Blank Screen
Write a program, which accept one value and give you the positive,
negative and zero result:
main()
{
int num;
clrscr();
printf(“Enter A Number :”);
scanf(“%d”,&num);
if(num>0)
{
printf(“\nPostive”);
}
if(num<0)
{
printf(“\nNegative)
}
if(num==0)
{
printf(“\nZero”)
}
getche();
}
“C” Language: If-else cont……
If-else: Now we want to perform some task when the
condition is false, in such a case we make use of else.
e.g.
clrscr();
If(condition)
{
printf(“hello”);
}
else
{
printf(“bye”);
}
Write a program, which accept a single value from Keyboard,
then find out either the entered number is EVEN or ODD.
main()
{
int num;
clrscr();
printf(“Enter a number :”);
scanf(“%d”,&num);
if(num%2==0)
{
printf(“\nNumber is EVEN”);
}
else
{
printf(“\Number is ODD”);
}
}
Write a program, which accept marks obtained in
four subject. Calculate it’s total, Calculate it’s
percentage and then Calculate it’s Grade.

main() printf(“\nPercentage is :%f”,per);


{ if(per>=80)
int OS,CA,IC,IP; {
int total; printf(“\nGrade is :A+);
float per; }
clrscr(); else if(per>=70);
printf(“Operting Sys :”); {
scanf(“%d”,&OS); printf(“\nGrade is :B);
printf(“Comp App”); }
scanf(“%d”,&CA); else if(per>=60)
printf(“Intro Comp”); {
scanf(“%d”,&IC); printf(“\nGrade is :C);
printf(“Intro Prog”); }
scanf(“%d”,&IP); else
total=OS+CA+IC+IP; {
per=total*100.0/400.0; printf(“\nSorry you are fail”);
printf(“\n\n\nTotal obtined }
marks :%d”,total); }
Switch-case:
“Switch-case is also one of the decision control construct, in this a
number of cases is written to switch we apply a single value,
that value is compared to the cases, with whom the condition
gets executed.”
If none of the case gets satisfied, default is execute automatically.
e.g.
Syntax of switch:
switch (Variable / Constant)
{
case 1:
case 2:
case 3:
.
.
case n:
default?:
}
Switch-case: Show case without break.
main()
{
int n;
clrscr();
printf(“A number plz :”);
scanf(“%d”,&n);
switch(n)
{
case 1:
printf(“\nI am case 1”);
case 2:
printf(“\nI am case 2”);
case 3:
printf(“\nI am case 3”);
default:
printf(“\nDefault Executed”);
}
getche();
}
Switch-case: (break) option will help us to stop on our desire location where our case is.
main()
{
int n;
clrscr();
printf(“A number plz :”);
scanf(“%d”,&n);
switch(n)
{
case 1:
printf(“\nI am case 1”);
break;
case 2:
printf(“\nI am case 2”);
break;
case 3:
printf(“\nI am case 3”);
break;
default:
printf(“\nDefault Executed”);
}
getche();
}
Iterative/Repeatative Statements (L00p):
“If we want to repeat an instruction, or group of
instruction, we make use of loop”.
When we are using loop, we have to keep in mind,
the following important points;
i. Initialization
ii. Condition
iii.Increment/Decrement
If we miss any of above, loop will not work properly.
In C-language we have the following types of loops;
i. for,
ii. While
iii.Do while
i. Syntax of “for”:
for(initialization; Condition; Inc/Dec)
{
Statement 1;
Statement 2;
.
.
Statement n;
}
“for” example with increment:
main()
{
int n;
clrscr();
for(n=1; n<=5; n++)
{
printf(“\nAllah”);
}
}
“for” with decrement:
main()
{
int n;
clrscr();
for(n=5; n>=1; n--)
{
printf(“\nAllah”);
}
}
Write a program that prints natural numbers from
1-10, on different lines:
main()
{
int n;
clrscr();
for(n=1;n<=10;n++)
{
printf(“\n%d”,n);
}
}
Write a program that prints natural numbers from
10-1, on different lines:
main()
{
int n;
clrscr();
for(n=10 ;n>=1 ;n--)
{
printf(“\n%d”,n);
}
}
Write a program, which prints even numbers from
1-20 in different lines:
Increment order;
main()
{
int n;
clrscr();
for(n=2; n<=20; n++)
if(n%2==0)
{
printf(“\n%d”,n);
}
}
Write a program, which prints even numbers from
1-20 in different lines:
Decrement order;
main()
{
int n;
clrscr();
for(n=20; n>=2; n--)
if(n%2==0)
{
printf(“\n%d”,n);
}
}
gotoxy: “It’s a special function, which is used to set the
cursor position on desired location over the screen.”
e.g. gotoxy(column,row);
Total numbers of columns we have =>80
Total numbers of rows we have =>25
main() Screen
{
clrscr(); Cursor Position
gotoxy(40,12);
}
gotoxy:
main()
{
clarscr(); Out Screen
printf(“shafiq”);
gotoxy(74,1); shafiq shafiq

printf(“shafiq”);
gotoxy(1,24);
printf(“shafiq”);
gotoxy(74,24);
printf(“shafiq”);
shafiq shafiq
}

You might also like