Basics of C Programs
Structure of C Program
int main() int main()
{ {
….. instruction 1;
…… instruction 2;
….. instruction 3;
…… instruction 4;
….. …..
…… ……
return 0; return 0;
} }
Every instruction must be terminated with semi-colon ;
Structure of C Program
First all variables will be declared,
• For this we have to identify:
- number of inputs and their names
- number of outputs and their names
- intermediate variables(if needed) and their names
Structure of C Program
Types of variables:
- integer
- floating point number
-characters
- strings(words)
Structure of C Program
Types of variables:
- for integer we use int
- for floating point number we use float
Structure of C program
int main()
{
int A,B,C;
......
…..
……
return 0;
}
Structure of C program
int main()
{
float x, y;
......
…..
……
return 0;
}
Structure of C program
int main()
{
int num1,num2,C=90;
float avg;
......
…..
……
return 0;
}
Structure of C program
int main()
{
int A=10,B,C;
float avg;
......
…..
……
return 0;
}
Note: Declare all the variables needed in our program.
Structure of C program
Every instruction may belong to either of 3 categories:
taking input from keyboard
Calculation/Logic
printing output to monitor
Structure of C program
Printing output to monitor:
we use a pre-defined function
printf()
Printing output in C: printf()
printf() is used to print anything on monitor
Syntax :
printf(“……..”);
for example:
printf(“PSIT”);
will print PSIT on monitor.
Printing output in C: printf()
int main()
{
printf(“PSIT”);
printf(“ Kanpur”);
}
This program will print
PSIT Kanpur
Printing output in C: printf()
int main()
{
printf(“Ramesh Kumar,\ndeepak”);
printf(“\n 678, Govind Nagar,”);
printf(“\nknapur-208011”);
}
Ramesh Kumar,
deepak
678, Govind Nagar,
Knapur-208011
Printing output in C: printf()
What if we want to print the value of a variable?
int main()
{
int X=10,y=20;
printf(“X”);
printf(“y”);
return 0;
}
Xy
But we want to print the value of X not X itself.
we want to print the value of y not y itself.
Printing output in C: printf()
we use format-specifier to print the value of a variable
For int %d
For float %f
Printing output in C: printf()
int main()
{
int X=10;
printf(“%d”, X);
return 0;
}
10
Will print 10 ( value of X ) on monitor
Printing output in C: printf()
int main()
{
int X=10;
float y=2.5;
printf(“%d”,X);
printf(“ ”);
printf(“%f”,y);
return 0;
}
10 2.5
Printing output in C: printf()
int main()
{
int a,b;
a=10;
b=20;
printf(“a=%d”, a);
printf(“ b=%d”, b);
return 0;
}
a=10 b=20
Printing output in C: printf()
int main()
{
int a,b;
a=10;
b=20;
printf(“value of a=%d”,100);
printf(“ value of b=%d”,b);
return 0;
}
value of a=100 value of b=20
Printing output in C: printf()
%d is the place holders for any variable
In printf() function where %d is present,
%d will be replaced by the value of integer provided.
The same is true for %f also.
Printing output in C: printf()
int main()
{
int a,b;
a=10;
b=20;
printf(“ a= %d”, b);
printf(“ b=%d”,a);
return 0;
}
What will be printed???
Printing output in C: printf()
int main()
{
int a,b;
a=10;
b=20;
printf(“ a= %d”, b);
printf(“ b=%d”,a);
return 0;
}
a=20 b=10
Printing output in C: printf()
int main()
{
int a,b;
a=10;
b=20;
printf(“ a= %d”, b);
printf(“ b=%d”, a);
return 0;
}
Every thing inside printf() will be printed as it is.
%d will be replace by the value of integer
a=20 b=10 provided
Printing output in C: printf()
int main()
{
int a,b;
a=10;
b=20;
printf(“ a= %d b=%d sum=%d”, a,b, a+b);
return 0;
}
a=10 b=20 sum=30
Printing output in C: printf()
One more thing……..
include header file at the top of program to refer pre-defined
functions.
#include<stdio.h>
Taking input from user via keyboard
To take input from user we use a pre-defined function:
scanf()
Taking input from user via keyboard
scanf() is used to take the input from user.
Syntax:
int a,b;
scanf(“%d”,&a);
scanf(“%d”,&b);
Do not use any thing other than %d or %f inside
double quotes of scanf() function.
Taking input from user via keyboard
scanf() is used to take the input from user.
Syntax:
int main()
{
int a,b;
scanf(“%d”,&a);
scanf(“%d”,&b);
………
……….
}
Taking input from user via keyboard
scanf() is used to take the input from user.
Syntax:
int main()
{
float a,b;
scanf(“%f”,&a);
scanf(“%f”,&b);
………
……….
}
Steps to make program in C
1. Open application (DEV C++)
2. Open a new file
3. Give name to file
4. Write the code (save the code)
5. Compile the code if error edit the code
6. Run the code
7. Verify the output (Testing)
https://sourceforge.net/projects/orwelldevcpp/
i) WAP to swap two numbers by using third variable.
ii) WAP to swap two numbers without using third variable.
iii) WAP to convert temperature in Fahrenheit to Celsius.
iv) WAP to find area and circumference of circle with radius r.
v) WAP to calculate simple interest and compound interest while
amount, time and rate of interest is given by the user.
vi) WAP to find the equation of straight line passing through the
point (x1, y1) and (x2, y2).
vii)WAP to interchange three values in such way put the value of y
in x and value of z in y and value of x in z.
viii)) WAP to find the final velocity by using following equation
v=u+at.
ix) WAP to find the GROSS SALARY of a person whose DA is 20% of
basic Salary and HRA is 25% of the Basic Salary.
x) WAP to print the sum of the digits of three-digit number.
xi) WAP to find the reverse of three-digit number.