Unit - II
Basic structure of C program
Basic structure of C program, use of library functions, input output statements,
flowchart.Decision Control structures and loop Control structures conditional loop
and unconditional loop: WHILE, DOWHILE, FOR, IF, IFELSE, NESTEDIF, LADDER
IFELSE etc.
Introduction
C is a procedural programming language initially developed by Dennis Ritchie in the year 1972 at
Bell Laboratories of AT&T Labs. It was mainly developed as a system programming language to
write the UNIX operating system.
The main features of the C language include:
• General Purpose and Portable
• Low-level Memory Access
• Fast Speed
• Clean Syntax
These features make the C language suitable for system programming like an operating system or
compiler development.
Why Should We Learn C?
Many later languages have borrowed syntax/features directly or indirectly from the C language. Like
syntax of Java, PHP, JavaScript, and many other languages are mainly based on the C language. C++
is nearly a superset of C language (Only a few programs may compile in C, but not in C++).
So, if a person learns C programming first, it will help him to learn any modern programming
language as well. As learning C help to understand a lot of the underlying architecture of the
operating system. Like pointers, working with memory locations, etc.
Writing the First Program in C
The following code is one of the simplest C programs that will help us the basic syntax structure of a
C program.
1
, Numerical Methods and Computer Programming, Unit II
Example:
#include <stdio.h>
int main() {
Output
int a = 10;
10
printf("%d", a);
return 0;
}
Let us analyse the structure of our program line by line.
Structure of the C program
By structure, it is meant that any program can be written in this structure only. Writing a C program in
any other structure will hence lead to a Compilation Error. The structure of a C program is as follows:
Components of a C Program:
1. Header Files Inclusion – Line 1 [#include <stdio.h>]
The first and foremost component is the inclusion of the Header files in a C program. A header file is
a file with extension .h which contains C function declarations and macro definitions to be shared
between several source files. All lines that start with # are processed by a preprocessor which is a
program invoked by the compiler. In the above example, the preprocessor copies the pre-processed
code of stdio.h to our file. The .h files are called header files in C.Some of the C Header files:
• stddef.h – Defines several useful types and macros.
• stdint.h – Defines exact width integer types.
• stdio.h – Defines core input and output functions
2
, Numerical Methods and Computer Programming, Unit II
• stdlib.h – Defines numeric conversion functions, pseudo-random number generator, and
memory allocation
• string.h – Defines string handling functions
• math.h – Defines common mathematical functions.
2. Main Method Declaration – Line 2 [int main()]
The next part of a C program is to declare the main() function. It is the entry point of a C program and
the execution typically begins with the first line of the main(). The empty brackets indicate that the
main doesn’t take any parameter (See this for more details). The int that was written before the main
indicates the return type of main(). The value returned by the main indicates the status of program
termination. See this post for more details on the return type.
3. Body of Main Method – Line 3 to Line 6 [enclosed in {}]
The body of a function in the C program refers to statements that are a part of that function. It can be
anything like manipulations, searching, sorting, printing, etc. A pair of curly brackets define the body
of a function. All functions must start and end with curly brackets.
4. Statement – Line 4 [printf(“Hello World”);]
Statements are the instructions given to the compiler. In C, a statement is always terminated by a
semicolon (;). In this particular case, we use printf() function to instruct the compiler to display
“Hello World” text on the screen.
5. Return Statement – Line 5 [return 0;]
The last part of any C function is the return statement. The return statement refers to the return values
from a function. This return statement and return value depend upon the return type of the function.
The return statement in our program returns the value from main(). The returned value may be used
by an operating system to know the termination status of your program. The value 0 typically means
successful termination.
• Explain the types of variables used in C language with example.
In the C programming language, variables are used to store data that can be manipulated during the
execution of a program. Variables can be categorized based on their data types and storage classes.
Below is an explanation of the primary types of variables in C, along with examples.
3
, Numerical Methods and Computer Programming, Unit II
I. Data Types
Basic Data Types
1. int: Used to store integers (whole numbers).
int age = 25;
2. float: Used to store single-precision floating-point numbers.
float salary = 55000.50;
3. double: Used to store double-precision floating-point numbers.
double pi = 3.1415926535;
4. char: Used to store a single character.
char grade = 'A';
II. Derived Data Types**
1. Array: Collection of elements of the same type.
int numbers[5] = {1, 2, 3, 4, 5};
char name[10] = "John";
2. Pointer: Stores the memory address of another variable.
int *p;
int x = 10;
p = &x;
3. Structure: Collection of variables of different types.
struct Student {
int id;
char name[50];
float percentage;
};
struct Student s1 = {1, "Alice", 85.5};
III. Enumeration
1. enum: Used to define a variable that can hold a set of predefined constants.
enum Day {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
enum Day today = Wednesday;
IV. Storage Classes
1. auto: The default storage class for local variables.
4