1. Examination preparation of C written by – Md. Shahariar Sarkar.
Introducton to C Chapter
Basic I/O 1
What is C language?
C programming language is a general-purpose, high-level language .It was originally developed
by Dennis M. Ritchie to develop the UNIX operating system at bell Laboratory in 1971. C was
originally first implemented on the DEC PDP-11 computer in 1972.
The UNIX OS was totally written in C by 1973. In 1978, Brian Kernighan and Dennis Ritchie
produced the first publicly available description of C, now known as the K&R standard. After
that the language was formalized in 1988 by the American National Standard Institute. (ANSI)
and its known as ANSI standard.
As the successor of B language, it was named as C language.
Why C is more popular?
C has become more popular because of the following reasons.
It is easy to learn.
It is a structured language.
It produces efficient program.
It can handle low level activities.
It can be compiled on a variety of computer platforms.
Where, C is used?
C is the most widely used programming language in the world for its high power productivity. It
is used in different field of computer such as.
Operating system Windows, UNIX, LINUX
Application software such as Word, Excel, Foxpro etc.
Programming language such as Java, Visual basic etc all are the products of C.
Print spoolers.
Network Drivers.
Database Management System (DBMS) - MySQL is written by C.
2. Examination preparation of C written by – Md. Shahariar Sarkar.
Structure of a C program:
Include header file section.
Global declaration section.
main()
{
Definition of the function.
}
User-defined functions
{
Definition of the function.
}
3. Examination preparation of C written by – Md. Shahariar Sarkar.
Example of Rule -1.
1.Write a program that print the following sentence
Hello world ! I am a programmar
#include<stdio.h>
int main()
{
printf(“Hello world ! I am a programmar”);
return 0;
}
2. write a program that print the following sentence
in two line.
Hello world !
I am a programmar.
#include<stdio.h>
int main()
{
printf(“Hello world n”);
printf(“I am a programmar”);
return 0;
}
Here, n –new line
Now,t- tab
a-alert bell.
v- vertically distance.
What is header file of C?
Header file is a file of C library that
contains the declarations and definition
of library function.
Ex-stdio.h is the header file that
contains the declarations and definition
for certain input or output functions
such as printf, scanf etc.
What is function? How many types
of function. Define and give
exmaple.
A function is a well-defined program
segment that performs some specific
tasks.
There are two types of function in C.
i)Library function / Built- in function.
ii)User defined function.
Library function/Built-in function:
The functions whose name and syntax
are predefined that can not be changed
by the programmer are called Built-in
function. They are also called library
functions because the definition of
these functions are stored in the header
files of C library.
Ex: printf(), scanf() etc.
User-defined function:
The function that is defined by the user
is called user defined function. Using
defferent library functions, a
programmer makes user defined
function.
Ex- main() function is an user defined
function.
Rule-1: If we want to print any sentence
/word by C language then we have to
write-
printf(“ Sentence”);
4. Examination preparation of C written by – Md. Shahariar Sarkar.
3. Write the program no.2 using the t, v, a.
#inlcude<stdio.h>
int main()
{
printf(“tHello world”);
return 0;
}
5. Examination preparation of C written by – Md. Shahariar Sarkar.
Rule-2. If any arithmetic
number/charcter is needed to a
program then a variable has to be
declared.
Variable declaration-
data_type variable_name;
Example: int num;
For multiple variables
data_type variable1, variable2;
float value;
Data type
name
Data type Size
(byte)
Range Format
Specifier
Character
char
1 -27
to
27
-1 %c
Unsigned
Character
unsigned
char
1 0 to 27
%c
Integer int 2/4
-215
to
215
-1
%d
Long long
4
-231 to
231
-1
%ld
Float float
4
1.7e – 308
to 1.7e+30
8 %f
Double double
8
3.4e – 493
2 to 1.1e+4
932 %lf
Long
Double
Long double
10
3.4e – 493
2
to 1.1e+49
32
%lf
Make a table for all data types of C.
Rule-4. If we want to print the value
of a variable then we have to write-
printf(“specifier”, variable_name);
Rule-3. If we want to give the value of
a variable as an input (from keyboard
to screen) then we have to write-
scanf(“specifier” ,&variable_name);
6. Examination preparation of C written by – Md. Shahariar Sarkar.
Examples of rule-2,3and 4:
4. Write a program that takes
two integer number and
calculate the addition.
#include<stdio.h>
int main()
{
int a, b,sum;
scanf(“%d %d”,&a,&b);
sum=a+b;
printf(“%d”,sum);
return 0;
}
5. Write a program that take the
radius of a circle as an input and
calculate the area of the circle.
#include<stdio.h>
int main()
{
double r, area;
scanf(“%lf ” , &r);
area=3.1416*r*r;
printf(“ %lf ” , area);
return 0;
}
What is C token? Classify and briefly describe them?
In C language, Every meaningful individual unit is called C
token.
C has six types of tokens-
1. Keywords/reserved words.
2. Identifier.
3. Constants.
4. Strings.
5. Special Symbols.
6. Operators.
Keywords/reserved words:
In c language, some words have specific meaning that cannot
be changed, are called keywords or reserved words.
Example- auto, break, while, for, main, if, else etc.
Identifiers:
Identifiers are the name of variables, functions and arrays. They
are user-defined name.
Example- int num;
Here, num is a variable identifirer.
Constans:
The fixed values that cannot change during execution of the
program are called constants.
Example: int a=10;
Here, 10 is constant.
Strings:
The words that are written between double quotations are called
strings.
Ex- printf(“Hello world”);
Here, ‘Hello world’ is a string.
7. Examination preparation of C written by – Md. Shahariar Sarkar.
6. Write a program that
calculate the area of a rectangle.
#include<stdio.h>
int main()
{
int height,width,area.
printf(“ Please Enter height”);
scanf(“%d” , &height);
printf(“Please Enter width”);
scanf(“%d”, &width);
area=height*width;
printf(“The area= %d” , area);
return 0;
}
7. Write a program that takes
three integer numbers and
interchange them.
#include<stdio.h>
int main()
{
int a,b,c, temp;
printf(“Please Enter numbers”);
scanf(“%d %d %d”,&a,&b,&c);
temp=a;
a=b;
b=c;
c=temp;
printf(“%d %d %d”,a,b,c);
return 0;}
Special symbols:
In C language, different types of special symbols are used.
Ex- {}; ( ); $ etc.
Operator:
There are different types of operator in C language such that +;
-; && etc.
Write the Classification of operator with example.
There are different types of operator in C as follows-
Type of Operator Symbolic representation
Arithmetic operators +, -, *, /, %
Relational operators >, <, ==, >=, <=, !=
Logical operators &&(AND), ||(OR), !(NOT)
Increment and decrement operator ++ , --
Assignment operator =
Bitwise operator &, |, ^, >>, <<, ~
Comma operator ,
Conditional operator ?:
8. Examination preparation of C written by – Md. Shahariar Sarkar.
8. Write a program to calculate
the area of any triangle.
#include<stdio.h>
#include<math.h>
int main(){
float a,b,c;
float s,area;
printf("Enter size of each sides of
triangle");
scanf("%f %f %f",&a,&b,&c);
s = (a+b+c)/2;
area = sqrt(s*(s-a)*(s-b)*(s-c));
printf("Area of triangle is:
%.3f",area);
return 0;
}
a c
b
Formula of area of any triangle:
Area = √(s*(s-a)*(s-b)*(s-c))
Where s = (a + b + c)/2
sqrt() →√
What is variable? How many types of variable? Write the
convention to write a variable in C.
Variable: A variable is an identifier that can hold a single value
and it can be changed. When a variable is declared its allocate a
memory space according to its data type.
Depending of declaration place in the program, variable can be
classified into two categories-
1. Local variable
2. Global variable.
Local variable: The variable which is declared inside a function
and it cannot be accessed by other function is called Local
variable.
Example:
#include<stdio.h>
int main()
{
int value;
}
Here value is a local variable of main() function.
Global Variable: The variable which is declared outside of all
function and all function can access that variable is called
Global variable.
Exaple:
#include<stdio.h>
int mark;
int count()
{
……………….
……………….
}
9. Examination preparation of C written by – Md. Shahariar Sarkar.
9. Write a program to calculate
the area of a right angled triangle.
#include<stdio.h>
int main(){
float h,w;
float area;
printf("Enter height and width of
the right angled triangle : ");
scanf("%f%f",&h,&w);
area = 0.5 * h * w;
printf("Area of right angled triangle
is: %.3f",area);
return 0;
}
10. Write a program to calculate
the area and volume of sphere.
#include<stdio.h>
#include<math.h>
int main(){
float r;
float surface_area,volume;
printf("Enter radius of the sphere :
");
scanf("%f",&r);
surface_area = 4* 3.14 * r * r;
volume = (4/3) * 3.14 * r * r * r;
printf("Surface area of sphere is:
%.3f",surface_area);
printf("nVolume of sphere is :
%.3f",volume);
return 0;
}
int main()
{
……………….
………………..
}
Here mark is global variable, in this case, count() and main()
both function can use the variable mark.
Convention to write the name of a variable or identifier:
1. First character must be an alphabet or underscore.
2. Must be consist of only letters, digits or underscore.
3. Only first 31 characters are significant.
4. Keyword cannot be used.
5. Must not contain white space.
What are different storage class? Briefly describe.
Storage class: A storage class defines the scope and life- time of
variables or function within a C program.
There following storage classes, which can be used in C
program-
1. auto
2. register
3. static
4. extern
Auto storage class: Auto storage class is a default storage class
for all local variables. The compiler allocates separate memory
for each automatic (auto) variable of different functions, even if
they have the same name. Use auto specifier make a variable
automatic. If there is no specifier before variable then also the
compiler treat it as an automatic variable.
int main()
{
int count;
auto int month;
}
10. Examination preparation of C written by – Md. Shahariar Sarkar.
Register storage class: Register storage class is used to allocate
space of a variable in a register instead of RAM.
It can't have the unary '&' operator applied to it (as it does not
have a memory location).
Example:
int main()
{
register int miles;
scanf(“%d” , miles);
}
Static storage class: When a local variable is declared as static,
the compiler allocates a permanent memory space for that
variable and when a global variable is declared as static it is
known only by its own file, so it cannot be accessed by other
file.
void addition()
{
static int a;
}
Extern storage class: When a variable is needed to use in a file
that is declared later in that file or another file then extern
storage class is used.
void main()
{
extern int a, b;
printf(“%d %d”, a,b);
}
int a=10, b=20;
11. Examination preparation of C written by – Md. Shahariar Sarkar.
Exercise:
1. Write a program to transform the temperature from Celsius to Fahrenheit.
Formula: C/5= (F-32)/9.
2. Write the program using the following library functions
pow(a,b)=ab
abs(-a)=a [Finds absolute value of integer]
cos(ϴ)→Finds the cosine value.
sin(ϴ)→Finds the sine value.
tan(ϴ)→Finds the tangent value.
sqrt(a)→√a.
toascii(a)→Finds ASCII value.
tolower(a)→Finds lower case.
toupper(a)-Finds Upper case.
Sample answer of question: 2
#include<stdio.h>
int main()
{
char a;
scanf("%c",&a);
printf("%c",toupper(a));
return 0;
}