Module 1 - Chapter 2 - PPT
Module 1 - Chapter 2 - PPT
Module 1: Chapter 2
OVERVIEW OF C
Prof.Rajeshwari R,
Assistant Professor
Dept. of CSE, CMRIT, Bangalore
Structure of C Language program
1 ) Comment line
2) Preprocessor directive
3 ) Global variable declaration
4) main function( )
{ Local variables;
Statements;
}
User defined function
}
}
Structure of C Program
main(): It is the user defined function and every function has one main()
function from where actually program is started and it is encloses within the pair
of curly braces. The main( ) function can be anywhere in the program , but in
general practice it is placed in the first position.
void main(void ) or
void main()
Output: C language
Example : /*First c program with return statement*/
#include <stdio.h>
int main ( )
{
printf ("welcome to c Programming language.\n");
return 0;
}
//variable ‘a’is declared first and then asking the user to initialize
the value for ‘a’
Header Files : When we work with large projects, it is desirable to separate out
some procedures from main() function of the program. Some procedures need to
be used several times in different programs. So one solution is to copy the code of
that procedure from one program to another several times , which results in waste
of time.
So another solution is to make procedures and store them in a different file called
header files. System defined Header files contain the definitions of Library
functions like printf(), scanf(), getch() , puts(), pow(), clrscr()
Conventionally , header files ends with a ‘ . h’ extension and names can use only letters,
digits, dashes , and underscores. Header files can be system define in compiler it self or
programmers can design their own header files too.
Examples :
stdio.h : standard input & output ( functions like printf() ,scanf() are defined.)
math.h : Math functions like pow(), sin(), log() , sqrt() are defined.
These are called as formatted Input and output functions because they have special format to
read and write The text from streams and then converting them into binary stream.
Syntax of scanf() : scanf ( “ Control string” , & var1, & var2, ……..,& var n).
Format specifiers : For integer variables ------ %d
For float variables -------- %f
For character variables ------- %c
Input Statements ( reading input)
scanf()
// built-in/standard /formatted input function to read input
syntax: scanf(“format specifier”, address of input variable);
//formatspecifier: int-%d, float-%f,char-%c
//& - address operator
The #include is a preprocessor command that tells the compiler to include the
contents of stdio.h (standard input and output) file in the program.
The stdio.h file contains functions such as scanf() and printf() to take input and display
output respectively.
If you use the printf() function without writing #include <stdio.h>, the program will not
compile.
printf() is a library function to send formatted output to the screen. In this program,
printf() displays Hello, World! text on the screen.
The return 0; statement is the "Exit status" of the program. In simple terms, the
program ends with this statement.
Executing a C Program
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: "); //asking the user to enter input. 25
return 0;
} Output
Enter an integer: 25
You entered: 25
C Program to Add Two Integers
#include <stdio.h>
int main() {
Output
int number1, number2, sum; Enter two integers: 12
printf("Enter two integers: "); 11
scanf("%d %d", &number1, &number2); 12 + 11 = 23
// calculating sum
sum = number1 + number2;
printf("%d + %d = %d", number1, number2, sum);
return 0;
}
C Program to Multiply Two Floating-Point Numbers
#include <stdio.h>
int main() {
float a, b, product;
printf("Enter two numbers: "); Output
scanf("%f %f", &a, &b); Enter two numbers:
2.4
// Calculating product 1.12
product = a * b; Product = 2.69
return 0;
}
Swap 2 Numbers Using Third Variable
#include<stdio.h>
int main()
{
int a,b,temp;
printf(“enter 2 numbers”); // a=10 b=20
scanf(“%d %d”, &a , &b); / / reading values of a and b
printf(“before swapping a= %d b=%d”, a,b); // before swapping a=10 b=20
temp=a; //c=10
a=b; // a=20
b=te mp // b=10
printf(;“after swapping a=%d b=%d”, a,b); // after swapping a=20 b=10
return 0;
}
Swap 2 Numbers without Using Third Variable
#include<stdio.h>
int main()
{
int a,b;
printf(“enter 2 numbers”); // a=10 b=20
scanf(“%d %d”, &a , &b); / / reading values of a and b
printf(“before swapping a= %d b=%d”, a,b); // before swapping a=10 b=20
a=a+b; //a=10+20=30
b=a-b; // b=30-20=10
a=a-b; // a=30-10=20
printf(“after swapping a=%d b=%d”, a,b); // after swapping a=20 b=10
return 0;
}
Swap 3 Numbers Using fourth Variable
#include<stdio.h>
int main()
{
int a,b,c, temp;
printf(“enter 3 numbers”); // a=10 b=20 c=30
scanf(“%d %d %d”, &a , &b, &c); // reading values of a ,b and c
printf(“before swapping a =%d b=%d c=%d”, a,b,c); // before swapping a=10 b=20
temp=a; //temp=10
a=b; // a=20
b=c; //b=30
c=te mp;// c=10
printf(“after swapping a=%d b=%d c=%d”, a,b,c); // after swapping a=20 b=30
c=10
return 0;
}
Swap 3 Numbers without Using fourth Variable
#include<stdio.h>
int main()
{
int a,b,c;
printf(“enter 3 numbers”); // a=10 b=20 c=30
scanf(“%d %d %d”, &a , &b, &c); // reading values of a and b
printf(“before swapping a=%d b=%d c=%d”, a,b,c);
// before swapping a=10 b=20 c=30
a=a+b+c ; //a=10+20+30=60
b=a-b-c ; // b=60-20-30=10
c=a-b-c ; // c=60-10-30 =20
a=a-b-c ; //a=60-10-20=30
printf(“after swapping a=%d b=%d c=%d”, a,b,c); // after swapping a=30 b=10 c=20
return 0;
}
Program to find Quotient and Remainder
#include<stdio.h>
int main()
{
int divisor, dividend, q, r;
printf(“enter divisor, dividend\n”); // divisor=5 dividend=23
scanf(“%d %d”, &divisor, ÷nd );
q=dividend/divisor; // q=23/5=4
r=dividend% diviso r;//r=23%5=3
printf(“q=%d r=%d”, q, r);
return 0;
}
Find Area of Circle
#include<stdio.h>
int main()
{
float r, area;
printf(“enter input”);
scanf(“%f”, &r);
area=3.14*r*r;
printf(“area=%f”, area);
return 0;
}
Find Area of rectangle
#include<stdio.h>
int main()
{
float l, b, area;
printf(“enter input”);
scanf(“%f%f”, &l,&b);
area=l*b;
printf(“area=%f”, area);
}
Find Area of triangle
#include<stdio.h>
int main()
{
Integers are whole numbers that can have both zero, positive and negative values
but no decimal values. For example, 0, -5, 10
We can use int for declaring an integer variable.
int id=8; //valid
int id=1.8; //invalid
Variable Declaration:
type variable_list;
#include<stdio.h>
#include<stdio.h> int main()
int main() {
{ int a;
int a=10; // when variable is initialized printf(“enter value for a”); // a=50
by the programmer, without any user scanf(“%d”,&a); //when programmer
input, no need to use scanf() to read the is asking the user to enter initial value
value as the programmer only specified for a variable, we need to use scanf() to
the initial value. read that input value, as programmer is
printf(“value of a=%d”, a); // a=10 not aware of the value entered by the
return 0; user.
} printf(“value of a=%d”, a); //a=50
return 0;
}
Types of Variables in C
There are many types of variables in c:
1. local variable
2. global variable
3. static variable
4. automatic variable
5. external variable
Types of Files