SlideShare a Scribd company logo
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.
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.
}
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”);
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;
}
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);
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.
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 ?:
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()
{
……………….
……………….
}
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;
}
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;
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;
}

More Related Content

PDF
C Programming
PPTX
Input and Output In C Language
PDF
C programming Workshop
PPT
Mesics lecture 5 input – output in ‘c’
PPTX
Introduction to Basic C programming 02
DOCX
Important C program of Balagurusamy Book
PPTX
What is c
C Programming
Input and Output In C Language
C programming Workshop
Mesics lecture 5 input – output in ‘c’
Introduction to Basic C programming 02
Important C program of Balagurusamy Book
What is c

What's hot (20)

PPTX
C Programming Language Tutorial for beginners - JavaTpoint
DOC
20 C programs
DOCX
C Programming
DOC
'C' language notes (a.p)
PPT
C program
PDF
I PUC CS Lab_programs
ODP
OpenGurukul : Language : C Programming
PDF
C programing Tutorial
PPT
C the basic concepts
PDF
Hands-on Introduction to the C Programming Language
PDF
C Programming Tutorial - www.infomtec.com
PPTX
C programming language tutorial
PPT
C language basics
PPTX
Programming in C
PPSX
Concepts of C [Module 2]
PPT
Introduction to Basic C programming 01
PPSX
Programming in C [Module One]
PDF
Intro to C++ - language
C Programming Language Tutorial for beginners - JavaTpoint
20 C programs
C Programming
'C' language notes (a.p)
C program
I PUC CS Lab_programs
OpenGurukul : Language : C Programming
C programing Tutorial
C the basic concepts
Hands-on Introduction to the C Programming Language
C Programming Tutorial - www.infomtec.com
C programming language tutorial
C language basics
Programming in C
Concepts of C [Module 2]
Introduction to Basic C programming 01
Programming in C [Module One]
Intro to C++ - language
Ad

Viewers also liked (20)

PPTX
Data structures 4
PPT
lecture 9
PPT
PPTX
Sorting1
PPT
Cis435 week06
PDF
Chapter 8 sorting algo
PDF
Data structure
PDF
Linear time sorting algorithms
PPT
Cs105 l15-bucket radix
PPT
Shell sort
PDF
Sample chapters [data structure and algorithmic thinking with python]
PPT
Algorithms presentation on Path Matrix, Bell Number and Sorting
DOCX
Data structures and algorithm
PPT
Sorting Seminar Presentation by Ashin Guha Majumder
PPTX
SORTTING IN LINEAR TIME - Radix Sort
PPT
3.3 shell sort
PPT
Shell sort
PPT
Sorting Techniques
PPTX
BUCKET SORT
Data structures 4
lecture 9
Sorting1
Cis435 week06
Chapter 8 sorting algo
Data structure
Linear time sorting algorithms
Cs105 l15-bucket radix
Shell sort
Sample chapters [data structure and algorithmic thinking with python]
Algorithms presentation on Path Matrix, Bell Number and Sorting
Data structures and algorithm
Sorting Seminar Presentation by Ashin Guha Majumder
SORTTING IN LINEAR TIME - Radix Sort
3.3 shell sort
Shell sort
Sorting Techniques
BUCKET SORT
Ad

Similar to C programming (20)

PPTX
Introduction to C Unit 1
PPTX
C Language ppt create by Anand & Sager.pptx
PPTX
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
PDF
Reduce course notes class xii
PPTX
C programming
PPTX
c_pro_introduction.pptx
PDF
Unit 2 introduction to c programming
DOCX
Programming in c
DOC
Datastructure notes
PDF
C notes.pdf
DOCX
Report on Flutter.docx prasentation for study
PPTX
Chapter1.pptx
PPTX
Basic of C Programming | 2022 Updated | By Shamsul H. Ansari
PPTX
basic C PROGRAMMING for first years .pptx
ODP
C prog ppt
DOCX
C tutorials
DOC
Basic c
DOCX
C language tutorial
DOC
C notes diploma-ee-3rd-sem
PPTX
1_Introduction of programming language C.pptx
Introduction to C Unit 1
C Language ppt create by Anand & Sager.pptx
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
Reduce course notes class xii
C programming
c_pro_introduction.pptx
Unit 2 introduction to c programming
Programming in c
Datastructure notes
C notes.pdf
Report on Flutter.docx prasentation for study
Chapter1.pptx
Basic of C Programming | 2022 Updated | By Shamsul H. Ansari
basic C PROGRAMMING for first years .pptx
C prog ppt
C tutorials
Basic c
C language tutorial
C notes diploma-ee-3rd-sem
1_Introduction of programming language C.pptx

Recently uploaded (20)

PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Electronic commerce courselecture one. Pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPT
Teaching material agriculture food technology
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
NewMind AI Weekly Chronicles - August'25-Week II
Electronic commerce courselecture one. Pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Assigned Numbers - 2025 - Bluetooth® Document
The Rise and Fall of 3GPP – Time for a Sabbatical?
MYSQL Presentation for SQL database connectivity
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Review of recent advances in non-invasive hemoglobin estimation
Teaching material agriculture food technology
Encapsulation_ Review paper, used for researhc scholars
Spectral efficient network and resource selection model in 5G networks
A comparative analysis of optical character recognition models for extracting...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Chapter 3 Spatial Domain Image Processing.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Network Security Unit 5.pdf for BCA BBA.
Advanced methodologies resolving dimensionality complications for autism neur...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...

C programming

  • 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; }