Introduction To C
Introduction To C
PROGRAMMING
RAHUL KHANNA
IT - 2
760/IT/14
INTRODUCTION TO C
C programming is an ANSI/ISO standard and powerful programming language for developing real
time applications. C programming language was invented by Dennis Ritchie at the Bell Laboratories in
1972. It was invented for implementing UNIX operating system. C programming is most widely used
programming language even today. All other programming languages were derived directly or
indirectly from C programming concepts. C programming is the basis for all programming languages.
This C programming tutorial explains all basic concepts in C like history of C language, data types,
keywords, constants, variables, operators, expressions, control statements, array, pointer, string,
library functions, structures and unions etc.
This C programming tutorial is designed for the new learners, students and also for the corporate level
developers who want to learn and refresh their C programming skills.
C programming history
C programming language features were derived from an earlier language called B (Basic
Combined Programming Language BCPL)
In 1978, Dennis Ritchie and Brian Kernighan published the first edition The C Programming
Language and commonly known as K&R C
In 1983, the American National Standards Institute (ANSI) established a committee to provide
a modern, comprehensive definition of C. The resulting definition, the ANSI standard, or
ANSI C, was completed late 1988.
Reliability
Portability
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14
Flexibility
Interactivity
Modularity
Database systems
Graphics packages
Word processors
Spreadsheets
Network drivers
Interpreters
High Level
Middle Level
Examples:
Java, Python
C, C++
Low Level
Low level languages
provides nothing other than
access to the machines
basic instruction set
Assembler
Structure oriented
In this type of language, large
Object oriented
Non structure
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14
Data moves freely around the systems Data is hidden and cannot be
from one function to another
accessed by external functions
N/A
N/A
Examples:
N/A
C programming basics
Below are few commands and syntax used in C programming to write a simple C program. Lets see
all the sections of a simple C program line by line.
S.no
Command
1 #include <stdio.h>
2
3
4
5
6
7
8
Explanation
This is a preprocessor command that includes standard input output
header file(stdio.h) from the C library before compiling a C program
int main()
This is the main function from where execution of any C program
begins.
{
This indicates the beginning of the main function.
/*_some_comments_*/ whatever is given inside the command /* */ in any C program, wont
be considered for compilation and execution.
printf(Hello_World! ); printf command prints the output onto the screen.
getch();
This command waits for any character input from keyboard.
return 0;
This command terminates C program (main function) and returns 0.
}
This indicates the end of the main function.
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14
____________________________________________________________
printf() and scanf() functions are inbuilt library functions in C which are available in C library
by default. These functions are declared and related macros are defined in stdio.h which is a
header file.
We have to include stdio.h file as shown in below C program to make use of these printf()
and scanf() library functions.
1. C printf() function:
printf() function is used to print the character, string, float, integer, octal and hexadecimal
values onto the output screen.
We use printf() function with %d format specifier to display the value of an integer variable.
Similarly %c is used to display character, %f for float variable, %s for string variable, %lf for
double and %x for hexadecimal variable.
2. C scanf() function:
scanf() function is used to read character, string, numeric data from keyboard
Consider below example program where user enters a character. This value is assigned to
the variable ch and then displayed.
Then, user enters a string and this value is assigned to the variable str and then displayed.
C Data Types
C data types are defined as the data storage format that a variable can store a data to perform a
specific operation. Data types are used to define a variable before using in a program. Size of
variable, const and array are determined by data types. There are four data types in the C language.
They are
S.no
1
2
Types
Basic data types
Enumeration data type
Data Types
int, char, float, double
Enum
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14
3
4
Constants:
C Constants are also like normal variables. But, the only difference is, their values cant be modified
by the program once they are defined. Constants refer to fixed values. They are also called as
literals. Constants may be belonging to any of the data type.
Variables:
C variable is a named location in a memory where a program can manipulate the data. This location is
used to hold the value of the variable. The value of the C variable may get changed in the program.
The C variable might be belonging to any of the data types like int, float, char etc.
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14
Practicals
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14
FLOWCHART:
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14
OUTPUT:
SOURCE CODE:
#include<stdio.h>
int main()
{
long int no,sum=0;
int rem,check=0;
printf("Enter the required number:");
scanf("%ld",&no);
printf("\nGiven Number: %d",no);
while(no>0)
{
rem=no%10;
if(rem!=9)
{
if(check==0)
sum=(10*sum)+(rem+1);
else{
sum=(10*sum)+(rem+2);
check=0;
}
}
else
{
sum=(10*sum)+0;
check=1;
}
no=no/10;
}
no=sum; sum=0;
while(no>0)
{
rem=no%10;
sum=(10*sum)+rem;
no=no/10;
}
printf("\nAfter Adding one: %ld",sum);
return 0;
}
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14
Q2. Basic salary is input through the keyboard. His dearness allowance
is 40% of basic salary, and house rent allowance is 20% of basic salary.
Write a program to calculate his gross salary.
ALGORITHM:
1.Start
2.Declare variables for Gross salary,hr,bs.
3.Input Basic Salry
4.Calculations:
Da = 0.4*basic salary
Hra = 0.2*basic salary
Gross salary = HRA + DA + Basic Salary;
5.Print Gross Salary
6.Stop
FLOWCHART:
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14
OUTPUT:
SOURCE CODE:
#include<stdio.h>
int main()
{
float bs,da,hr,gs;
printf("Enter basic salary : ");
scanf("%f",&bs);
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14
da=(40*bs)/100;
hr=(20*bs)/100;
gs=bs+da+hr;
printf("DA:%f\nHR:%f\nGross Salary:%f\n",da,hr,gs);
return 0;
}
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14
OUTPUT:
SOURCE CODE:
#include<stdio.h>
int main()
{
float marks[5],sum=0;
int i;
printf("Enter marks of :\n");
for(i=0;i<5;i++)
{
printf("Subject %d:",i+1);
scanf("%f",&marks[i]);
}
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14
for(i=0;i<5;i++)
{
sum = sum + marks[i];
}
printf("Percentage = %f",sum/5);
return 0;
}
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14
OUTPUT:
SOURCE CODE:
#include<stdio.h>
int main()
{
long int num;
int sum = 0;
printf("Enter the num:");
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14
scanf("%ld",&num);
while(num!=0)
{
sum = sum + num%10;
num = num/10;
}
printf("Sum of digits: %d",sum);
return 0;
}
ALGORITHM:
1.Start
2.Declare variables for sex,marital status, age
3.if marital status is married
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14
Driver is insured
4.If marital status is not married,sex = male and age is more than 30
Driver insured
5. If marital status is not married,sex = female and age is more than 25
Driver is insured
6.Else Driver not insured
7.Stop
FLOWCHART:
OUTPUT:
s
SOURCE CODE:
#include<stdio.h>
int main()
{
char sex,ms;
int age;
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14
SOURCE CODE :
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main() {
float a, b, c, D ,x1, x2, rp , ip;
printf("\nEnter the coefficients of quadratic equation in the form of ax^2+bx+c
= \n");
scanf("%f %f %f", &a, &b,&c);
D=b*b-4*a*c;
if(D>=0) {
x1= (-b+sqrt(D))/(2*a);
x2= (-b-sqrt(D))/(2*a);
printf("The roots are %f %f", x1, x2);
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14
}
else {
rp=b/(2*a);
ip=sqrt(-D)/(2*a);
printf("The roots are %f +i%f ,%f -i%f", rp, ip, rp,ip);
}
return 0 ;
getch();
}
OUTPUT:
break;
case 2:
result = a-b;
break;
case 3:
result = a*b;
break;
case 4:
result = a/b;
break;
}
printf("\nThe result is = %f", result);
return 0;
getch();
}
OUTPUT:
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
int main() {
int hn, ts;
float carbon;
printf("Enter the hardness, carbon content and tensile strength of the steel : ");
scanf("%d %f %d",&hn, &carbon ,&ts);
if (hn>50&&carbon<0.7&&ts>5600)
printf("The grade of steel is 10");
else if(hn>50&&carbon<0.7)
printf("The grade of steel is 9");
else if(carbon<0.7&&ts>5600)
printf("The grade of steel is 8");
else if(hn>50&&ts>5600)
printf("The grade of steel is 7");
else if (hn>50||carbon<0.7||ts>5600)
printf("The grade of steel is 6");
else
printf("The grade of steel is 5");
return 0 ;
getch();
}
OUTPUT:
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14
1
2
1
3 + +
1
20
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
int main() {
float i;
float a;
float sum=0;
for(i=1;i<21;i++) {
a=1/i;
sum=sum+a;
}
printf("Sum of series is %f", sum );
return(0);
getch();
}
OUTPUT
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14
OUTPUT:
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14
OUTPUT:
A
A
A
A
A
A
A
B
B
B
B
B
B
C
C
C
C
C
DEFGFED
DEF
FED
DE
ED
D
D
C
C
C
C
C
B
B
B
B
B
B
A
A
A
A
A
A
A
SOURCE CODE:
#include<stdio.h>
int main()
{ int n=7,row,k,j;
for(row=0;row<n;row++)
{
for(k=65;k<=72-row-1;k++)
printf("%c",k);
for(int j=0;j<row;j++)
printf(" ");
for(int j=row;j>1;j--)
printf(" ");
if(row==0)
{
for(int k=72-row-2;k>=65;k--)
printf("%c",k);
}
else{
for(int k=72-row-1;k>=65;k--)
printf("%c",k);}
printf("\n");
}
return 0;
}
OUTPUT:
__________________________________________________________________________________
IT-2
Rahul Khanna
760/IT/14