Basics of C Programming
Basics of C Programming
Structure of C Program
int main() int main()
{ {
….. instruction 1;
…… instruction 2;
….. instruction 3;
…… instruction 4;
….. …..
…… ……
return 0; return 0;
} }
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;
}
Syntax :
printf(“……..”);
for example:
printf(“PSIT”);
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
For int %d
For float %f
Printing output in C: printf()
int main()
{
int X=10;
printf(“%d”, X);
return 0;
}
10
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;
}
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;
}
#include<stdio.h>
Taking input from user via keyboard
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
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.