By – Richa Gupta
CONTENTS 
 What is C? 
 History 
 Why we use c 
 Basics of C environment 
 Getting started with C 
 Data types / Keywords 
 Variables / C character set 
 Operators 
 The If – else statement 
 Switch variables 
 Loops
What is c ? 
o Programming Language written by Brian Kernighan and D 
D Dennis Ritchie . 
o C has been designed to have both high level languages and 
l low level languages so it is often called middle level 
languages. 
o Highly structured language . 
o Handle bit level operation . 
o There are zillions of lines of C legacy code . 
o It is also used in application program . 
o To develop system software- compilers , operating system.
Year Language Developed by Remarks 
1960 ALGOL International 
Committee 
Too general, too 
abstract 
1963 CPL Cambridge 
University 
Hard to learn , 
difficult to 
implement 
1967 BCPL Martin Richards 
at Cambridge 
University 
Could deal with 
only specific 
problems 
1970 B Ken Thompson 
at AT & T 
Could deal with 
only specific 
problems 
1972 C Dennis Ritchie 
at AT & T 
Lost generality of 
BCPL and B 
restored 
History
Why we use c ? 
Mainly because it produces codes that runs nearly as fast as 
code written in assembly language some examples of the use of 
c might be : 
 Operating systems 
 Language compilers 
 Assemblers 
 Text editors 
 Print spoolers 
 Network drivers 
 Modern programs 
 Data bases 
 Language interpreters 
 Utilities.
Basics of C environment 
C system consist of 3 parts 
- Environment 
- Language 
- c standard library. 
Development environment has 6 phases . 
• Edit : Writing the source code by using some IDE or editor. 
• Pre-processor : Already available routine. 
• Compile : Translates or converts source to object code for a 
s specific platform i.e., source code -> object codes. 
• Link : Links object code with libraries and stores on disk. 
• Load : Puts the program in memory. 
• Execute : Runs the program.
Executing a c program . 
Computer Edit program 
source code 
Compile 
Object code 
Library files Link object code executable Computer
Getting started c program. 
#include<stdio.h> // preprocessor directive . 
#include<conio.h> 
void main() // Main function. 
{ //Start of the program. 
printf(“hello world”); // Function from C library that 
getch(); is used to print strings to the 
clrscr(); output of our screen. 
} // End of the program.
Data Types . 
1. Integer int 2 bytes 
2. Long integer long int 4 bytes 
3. Character char 1 bytes 
4. Float float 4 bytes 
5. Double double 8 bytes 
Some Keywords 
Auto double Int struct 
Break Else Long Switch 
Case Enum Register Typedef 
Char Extern Return Union 
Const Float Short Unsigned 
Continue For Signed Void 
Default Goto Size of Volatile 
do If static While
Variables 
 These names of variables consists of numbers , letters , 
and underscores. 
 The restriction on names is that you may not use 
keywords as the name of variables or functions. 
 Declarations are important in multi file programs where a 
variables defined in one file must be referred to second 
The C Character Set 
A character denotes any alphabet , digit or special 
symbol used to represent information. 
Alphabets A ,B ,……………, Y , Z 
A , b ,………….., y , z 
Digits 0 , 1 , 2, 3 , 4 , 5 , 6, 7 , 8 , 9 
Special symbols ~ ‘ ! @ # % & * ( ) _ - + = 
 | { } [ ] ; : “ ‘ < > , . ? /
Operators 
1. Arithmetic operator 
a) Binary operator 
+ , - , * , /, % 
b) Unary operator 
++ , -- , - 
++ increment by 1 
-- decrement by 1 
- negative 
2. Assignment operator 
int a = 10, b = 5 , c : 
c = a+b : 
3. Compound Assignment operator 
+=, -= , /= , %= , * = 
4. Comparison operator 
>,< ,>= ,<= , != , ==
5. Logical operator 
&& (And) , || (or) , ! (not). 
&& (And) 
Cond 1 Cond 2 Result 
T T T 
T F F 
F T F 
F F F 
All condition true the true otherwise false . 
|| (or) 
Cond 1 Cond 2 Result 
T T T 
T F T 
F T T 
F F F 
All conditions false result false otherwise true .
! (Not) 
Cond 1 Result 
T F 
F T 
• Prefix Increment value than use ++a. 
• Postfix Use value than increment a++. 
 Write a program to add two numbers. 
#include <stdio.h> 
#include<conio.h> 
void main () 
{ 
int a=10, b=5; 
printf(“a+b=%d”,a+b); 
getch(); 
clrscr(); 
}
The If – else statement 
if (condition) if (condition) 
{ 
Statement 1; statement- sequence 1; 
else } 
Statement2; else 
{ 
statement - sequence 2; 
}
 Write a program to check given no. is even or 
odd. 
#include <stdio.h> 
#include<conio.h> 
void main () 
{ 
int num; 
printf(“enter a number”); 
scanf(“%d”, & num); 
if (num%2 ==0) 
printf(“even number”); 
else 
printf(“odd number”); 
getch(); 
clrscr(); 
}
Switch variables 
{ 
case value; 
case value; 
} 
 Write a program to show the given character is vowel or constant . 
#include<stdio.h> 
#include<conio.h> 
void main () 
{ 
char c; 
printf(“enter character :”); 
scanf(“%c” & c); 
switch (c) 
{ 
case ‘a’ 
case ‘e’ 
case ‘i’ 
case ‘o’ 
case ‘u‘ 
printf (“vowel”); 
break; 
default 
printf (“const”); 
} 
getch(); 
clrscr(); 
}
Loops 
A loop is a sequence of statements which is specified once but 
which may be carried out several times in succession. 
There are three methods by way of which we can repeat a part of a 
program. They are : 
a. Using a for statement 
b. Using a while statement 
c. Using a do- while statement
Write a program to implement for loop. 
#include<stdio.h> 
#include<conio.h> 
Void main(); 
{ 
int c; 
for (c=1;c<=5;c++); 
{ 
printf(“n%d”,c); 
} 
getch(); 
Clrscr(); 
} 
for loop 
for (initialization;condition;increament) 
{ 
statement 
}
While loop 
While (condition) 
{ 
statement; 
increment; 
} 
 Write a program to implement while loop. 
#include<stdio.h> 
#include<conio.h> 
void main() 
{ 
int c =1; 
while (c<=5) 
{ 
printf(“n%d”,c); 
c++; 
} 
getch(); 
clrscr(); 
}
Do While 
do 
{ 
statement; 
increment 
} 
while (condition); 
 Write a program to implement do while loop. 
#include <stdio.h> 
#include<conio.h> 
void main() 
{ 
int sum =0, num; 
do 
{ 
printf (“enter a number n”) 
scanf (“%d”,& num); 
sum += num; 
} 
while (num!=0); 
printf (“sum=%d”,sum); 
getch(); 
clrscr(); 
}

Introduction to c programming

  • 1.
  • 2.
    CONTENTS  Whatis C?  History  Why we use c  Basics of C environment  Getting started with C  Data types / Keywords  Variables / C character set  Operators  The If – else statement  Switch variables  Loops
  • 3.
    What is c? o Programming Language written by Brian Kernighan and D D Dennis Ritchie . o C has been designed to have both high level languages and l low level languages so it is often called middle level languages. o Highly structured language . o Handle bit level operation . o There are zillions of lines of C legacy code . o It is also used in application program . o To develop system software- compilers , operating system.
  • 4.
    Year Language Developedby Remarks 1960 ALGOL International Committee Too general, too abstract 1963 CPL Cambridge University Hard to learn , difficult to implement 1967 BCPL Martin Richards at Cambridge University Could deal with only specific problems 1970 B Ken Thompson at AT & T Could deal with only specific problems 1972 C Dennis Ritchie at AT & T Lost generality of BCPL and B restored History
  • 5.
    Why we usec ? Mainly because it produces codes that runs nearly as fast as code written in assembly language some examples of the use of c might be :  Operating systems  Language compilers  Assemblers  Text editors  Print spoolers  Network drivers  Modern programs  Data bases  Language interpreters  Utilities.
  • 6.
    Basics of Cenvironment C system consist of 3 parts - Environment - Language - c standard library. Development environment has 6 phases . • Edit : Writing the source code by using some IDE or editor. • Pre-processor : Already available routine. • Compile : Translates or converts source to object code for a s specific platform i.e., source code -> object codes. • Link : Links object code with libraries and stores on disk. • Load : Puts the program in memory. • Execute : Runs the program.
  • 7.
    Executing a cprogram . Computer Edit program source code Compile Object code Library files Link object code executable Computer
  • 8.
    Getting started cprogram. #include<stdio.h> // preprocessor directive . #include<conio.h> void main() // Main function. { //Start of the program. printf(“hello world”); // Function from C library that getch(); is used to print strings to the clrscr(); output of our screen. } // End of the program.
  • 9.
    Data Types . 1. Integer int 2 bytes 2. Long integer long int 4 bytes 3. Character char 1 bytes 4. Float float 4 bytes 5. Double double 8 bytes Some Keywords Auto double Int struct Break Else Long Switch Case Enum Register Typedef Char Extern Return Union Const Float Short Unsigned Continue For Signed Void Default Goto Size of Volatile do If static While
  • 10.
    Variables  Thesenames of variables consists of numbers , letters , and underscores.  The restriction on names is that you may not use keywords as the name of variables or functions.  Declarations are important in multi file programs where a variables defined in one file must be referred to second The C Character Set A character denotes any alphabet , digit or special symbol used to represent information. Alphabets A ,B ,……………, Y , Z A , b ,………….., y , z Digits 0 , 1 , 2, 3 , 4 , 5 , 6, 7 , 8 , 9 Special symbols ~ ‘ ! @ # % & * ( ) _ - + = | { } [ ] ; : “ ‘ < > , . ? /
  • 11.
    Operators 1. Arithmeticoperator a) Binary operator + , - , * , /, % b) Unary operator ++ , -- , - ++ increment by 1 -- decrement by 1 - negative 2. Assignment operator int a = 10, b = 5 , c : c = a+b : 3. Compound Assignment operator +=, -= , /= , %= , * = 4. Comparison operator >,< ,>= ,<= , != , ==
  • 12.
    5. Logical operator && (And) , || (or) , ! (not). && (And) Cond 1 Cond 2 Result T T T T F F F T F F F F All condition true the true otherwise false . || (or) Cond 1 Cond 2 Result T T T T F T F T T F F F All conditions false result false otherwise true .
  • 13.
    ! (Not) Cond1 Result T F F T • Prefix Increment value than use ++a. • Postfix Use value than increment a++.  Write a program to add two numbers. #include <stdio.h> #include<conio.h> void main () { int a=10, b=5; printf(“a+b=%d”,a+b); getch(); clrscr(); }
  • 14.
    The If –else statement if (condition) if (condition) { Statement 1; statement- sequence 1; else } Statement2; else { statement - sequence 2; }
  • 15.
     Write aprogram to check given no. is even or odd. #include <stdio.h> #include<conio.h> void main () { int num; printf(“enter a number”); scanf(“%d”, & num); if (num%2 ==0) printf(“even number”); else printf(“odd number”); getch(); clrscr(); }
  • 16.
    Switch variables { case value; case value; }  Write a program to show the given character is vowel or constant . #include<stdio.h> #include<conio.h> void main () { char c; printf(“enter character :”); scanf(“%c” & c); switch (c) { case ‘a’ case ‘e’ case ‘i’ case ‘o’ case ‘u‘ printf (“vowel”); break; default printf (“const”); } getch(); clrscr(); }
  • 17.
    Loops A loopis a sequence of statements which is specified once but which may be carried out several times in succession. There are three methods by way of which we can repeat a part of a program. They are : a. Using a for statement b. Using a while statement c. Using a do- while statement
  • 18.
    Write a programto implement for loop. #include<stdio.h> #include<conio.h> Void main(); { int c; for (c=1;c<=5;c++); { printf(“n%d”,c); } getch(); Clrscr(); } for loop for (initialization;condition;increament) { statement }
  • 19.
    While loop While(condition) { statement; increment; }  Write a program to implement while loop. #include<stdio.h> #include<conio.h> void main() { int c =1; while (c<=5) { printf(“n%d”,c); c++; } getch(); clrscr(); }
  • 20.
    Do While do { statement; increment } while (condition);  Write a program to implement do while loop. #include <stdio.h> #include<conio.h> void main() { int sum =0, num; do { printf (“enter a number n”) scanf (“%d”,& num); sum += num; } while (num!=0); printf (“sum=%d”,sum); getch(); clrscr(); }