INTRODUCTION TO C PROGRAMMING 1BPLC105E
Module-1
Overview of C: History of C, Importance of C, Basic Structure of C Programs, Programming
Style, Compiling and Executing a ‘C’ Program.
History of C
• C was developed by Dennis Ritchie at Bell Laboratories in 1972.
• It evolved from ALGOL, BCPL, and B.
• ALGOL introduced structured programming concepts.
• BCPL (1967, Martin Richards) and B (1970, Ken Thompson) were typeless system
programming languages.
• C added data types and powerful features, making it suitable for both system and
application programming.
• The UNIX operating system was written almost entirely in C.
Fig. 1.1: History of ANSI C
NOOR SUMAIYA, [Link]/DEPT OF CSE, TOCE Page 1
INTRODUCTION TO C PROGRAMMING 1BPLC105E
• C gained popularity with the 1978 book “The C Programming Language” by Kernighan
and Ritchie (K&R C).
• To avoid incompatibility, ANSI standardized C in 1989 (ANSI C or C89); later approved
by ISO (C90).
• Later versions include C99 and C11, introducing modern enhancements.
Importance of C
❖ C is a robust, high-level, and structured programming language.
❖ Combines the power of assembly language with the ease of a high-level language.
❖ Programs are efficient and fast due to strong data types and operators.
❖ Portable — can run on different machines with minimal changes.
❖ Modular — supports structured and function-based programming.
❖ Allows extension by adding user-defined functions.
Widely used for:
• Operating systems
• Embedded systems
• Compilers
• Business applications
Sample Program 1 — Printing a Message
/* Sample Program 1: Printing a message */
#include <stdio.h>
main()
{
/* This program prints a simple message on the screen */
printf("I see, I remember");
}
Output:
I see, I remember
Note:
• #include <stdio.h> – Loads standard input/output functions like printf.
• printf("I see, I remember"); – Displays the text message.
• Each statement ends with ;
NOOR SUMAIYA, [Link]/DEPT OF CSE, TOCE Page 2
INTRODUCTION TO C PROGRAMMING 1BPLC105E
Sample Program 2 — Adding Two Numbers
/* Sample Program 2: Adding Two Numbers */
/* This program adds two numbers and displays the result */
#include <stdio.h>
main()
{
/* Variable declarations */
int number; /* integer variable */
float amount; /* floating-point variable */
/* Assign values to variables */
number = 100;
amount = 30.75 + 75.35;
/* Display the results */
printf("%d\n", number); /* print integer value */
printf("%5.2f", amount); /* print floating-point value */
}
Output:
100
106.10
Note
• Comment lines describe the purpose and each logical block.
• int number; and float amount; – declare variables.
• %d prints an integer, %f prints a float.
• \n – moves to a new line.
Sample Program 3 — Interest Calculation
/* Sample Program 3: Interest Calculation */
/* This program calculates compound interest for 10 years */
#include <stdio.h>
#define PERIOD 10 /* number of years */
#define PRINCIPAL 5000.00 /* initial investment amount */
main()
{
/* Declaration of variables */
int year = 1;
float amount, value, inrate;
/* Initialization of variables */
NOOR SUMAIYA, [Link]/DEPT OF CSE, TOCE Page 3
INTRODUCTION TO C PROGRAMMING 1BPLC105E
amount = PRINCIPAL;
inrate = 0.11; /* 11 percent interest rate */
/* Loop to calculate interest for each year */
while (year <= PERIOD)
{
value = amount + inrate * amount; /* compute value at end of year */
printf("%2d %8.2f\n", year, value); /* print year and amount */
year = year + 1; /* increment year */
amount = value; /* new amount for next year */
}
}
Partial Output:
1 5550.00
2 6160.50
3 6838.16
...
10 14235.34
Note:
• Every /* … */ comment describes code purpose.
• #define creates symbolic constants (not variables).
• while loop repeats calculations for 10 years.
• %2d prints integers in 2 spaces, %8.2f prints floats in 8 spaces with 2 decimals.
Sample Program 4 — Use of Subroutines (Functions)
/* Sample Program 4: Use of Subroutines */
/* This program multiplies two numbers using a user-defined function */
#include <stdio.h>
/* Function declaration */
int mul(int x, int y);
main()
{
int a = 5, b = 10, c;
NOOR SUMAIYA, [Link]/DEPT OF CSE, TOCE Page 4
INTRODUCTION TO C PROGRAMMING 1BPLC105E
/* Call the function mul() */
c = mul(a, b);
/* Display the result */
printf("Multiplication of %d and %d is %d", a, b, c);
}
/* Function definition */
int mul(int x, int y)
{
/* Returns the product of two numbers */
return (x * y);
}
Output:
Multiplication of 5 and 10 is 50
Note:
• Comments describe function purpose and each section.
• mul() is a user-defined function (like a subroutine).
• Parameters x and y receive copies of a and b.
• The return statement sends the product back to main().
Sample Program 5 — Use of Math Functions
/* Sample Program 5: Use of Math Functions */
/* This program computes cosine values of angles from 0° to 180° */
#include <stdio.h> /* standard input/output header */
#include <math.h> /* header file for mathematical functions */
main()
{
int angle; /* variable for degree values */
float x; /* variable for cosine result */
/* Loop from 0 to 180 degrees in steps of 10 */
for (angle = 0; angle <= 180; angle = angle + 10)
{
x = cos(angle * 3.14159 / 180); /* convert degrees to radians */
printf("%3d %7.4f\n", angle, x); /* print degree and cosine value */
}
}
NOOR SUMAIYA, [Link]/DEPT OF CSE, TOCE Page 5
INTRODUCTION TO C PROGRAMMING 1BPLC105E
Output:
0 1.0000
10 0.9848
20 0.9397
...
180 -1.0000
Note:
• Each comment clarifies purpose and logic.
• #include <math.h> gives access to standard math functions like cos().
• for loop repeats calculation for 0–180 degrees in steps of 10.
• cos(angle * π / 180) – converts degrees → radians.
Basic Structure of a C Program
❖ A C program can be viewed as a collection of building blocks called functions.
❖ Each function contains one or more statements designed to perform a specific task.
❖ A complete C program is created by writing and combining these functions in a logical
sequence.
Documentation Section
Link Section (#include section)
Definition Section
Global Declaration Section
main() Function Section
{
Declaration part
Executable part
}
Subprogram Section (User-defined functions)
Function 1
Function 2
---
Function n
Fig. 1.2: An overview of a C program
NOOR SUMAIYA, [Link]/DEPT OF CSE, TOCE Page 6
INTRODUCTION TO C PROGRAMMING 1BPLC105E
1. Documentation Section
• Contains comment lines giving details such as program title, author, date, and purpose.
• Helps in program readability and maintenance.
Example:
/* Program: Interest Calculation
Author: E. Balagurusamy
Date: 13 Oct 2025 */
2. Link Section
• Provides instructions to the compiler to link library functions.
• Achieved using #include preprocessor directives.
Example:
#include <stdio.h>
#include <math.h>
3. Definition Section
• Used for defining symbolic constants using #define.
• These constants remain unchanged during program execution.
Example:
#define PERIOD 10
#define RATE 0.11
4. Global Declaration Section
• Contains declarations of global variables and function prototypes.
• Global variables are accessible from all functions in the program.
Example:
int count;
float total;
int sum(int, int); /* function declaration */
5. Main() Function Section
• Every C program must have one main() function.
• Execution of a C program always begins with the first statement in main().
• It consists of two parts:
(a) Declaration Part
NOOR SUMAIYA, [Link]/DEPT OF CSE, TOCE Page 7
INTRODUCTION TO C PROGRAMMING 1BPLC105E
• All variables used in the program must be declared first.
• int i, j;
• float amount;
(b) Executable Part
• Contains the actual logic (statements) to perform the task.
• Each statement ends with a semicolon (;).
• Enclosed between braces { }.
Example:
{
amount = PRINCIPAL + (PRINCIPAL * RATE);
printf("%f", amount);
}
6. Subprogram Section
• Contains user-defined functions that are called from main() or other functions.
• They help divide the program into smaller, manageable parts (modular programming).
Example:
float interest(float principal, float rate, int time)
{
return (principal * rate * time);
}
Programming Style
1. Free-form Language
• Unlike some other programming languages (like COBOL or FORTRAN), C is a free-form
language.
• The C compiler does not care where on the line a statement begins — you can start typing
anywhere.
• While this provides flexibility, it can also lead to bad programming habits if not used
properly.
• Therefore, programmers should use this flexibility to make programs readable and well-
formatted, not cluttered.
NOOR SUMAIYA, [Link]/DEPT OF CSE, TOCE Page 8
INTRODUCTION TO C PROGRAMMING 1BPLC105E
• Although there are several possible styles, it is recommended to choose one style and use
it consistently.
2. Use of Lowercase Letters
• It is a good habit to write C programs in lowercase letters.
• C program statements and keywords are written in lowercase.
• Uppercase letters are reserved for symbolic constants only.
Example:
#define MAX 100
3. Use of Braces and Indentation
• Braces { } group statements together and mark the beginning and end of a function or
block.
• Proper indentation of braces and statements makes the program easier to read and debug.
Example showing properly aligned braces:
main()
{
printf("Hello C");
}
• In the above example, the opening brace is on a new line, and the statements inside are
indented.
4. Free-form Flexibility Example
• Since C is free-form, statements can be grouped together on a single line.
For example, these statements:
a = b;
x = y + 1;
z = a * x;
can be written in one line as:
a = b; x = y + 1; z = a * x;
Similarly, the program:
NOOR SUMAIYA, [Link]/DEPT OF CSE, TOCE Page 9
INTRODUCTION TO C PROGRAMMING 1BPLC105E
main()
{
printf("Hello C");
}
can be written as:
main() { printf("Hello C"); }
However, this reduces readability and should not be used.
5. Use of Comments
• Comments are an important part of good programming style.
• Judiciously inserted comments not only improve readability but also help others (and
yourself) understand the program’s logic.
• Comments are also useful for debugging and testing the program.
Example:
/* Program to print Hello C */
main()
{
printf("Hello C");
}
Compiling and Executing a ‘C’ Program.
A C program must go through several stages before it can be executed on the computer. When
we write a C program, it is just a source code (text file). To run it, we must compile, link, and
execute it.
Steps Involved
1. Creating the Program
• The program is first written using a text editor such as:
o Turbo C Editor
• The file is saved with the extension .c (for example, sample.c).
• This file is called the source program or source code.
Example:
#include <stdio.h>
NOOR SUMAIYA, [Link]/DEPT OF CSE, TOCE Page 10
INTRODUCTION TO C PROGRAMMING 1BPLC105E
main()
{
printf("Hello C");
}
2. Compiling the Program
• The compiler translates the source code into machine language (binary code).
• During compilation, the compiler:
o Checks for syntax errors (mistakes in C grammar).
o Generates an object file (usually with extension .obj or .o).
• If there are errors, the compiler lists them for correction.
• Once all errors are corrected and the code compiles successfully, an object file is created.
In Turbo C (Windows):
• Press Alt + F9 to compile the program.
3. Linking the Program
• The linker combines the object code with required library functions (like printf, scanf,
sqrt,etc.) which are stored in standard library files.
• It produces a single executable file (extension .exe or [Link]).
• If a required function is missing or incorrectly declared, the linker produces an
“unresolved reference” error.
In Turbo C:
• Use the Run → Link option or simply press Ctrl + F9, which automatically links and
runs.
4. Executing the Program
• The executable file is loaded into the computer’s memory and then executed.
• The CPU executes the binary instructions and displays the program output on the screen.
In Turbo C:
• After compilation, press Ctrl + F9 to run.
• Use Alt + F5 to view the output screen.
Example Output:
Hello C
NOOR SUMAIYA, [Link]/DEPT OF CSE, TOCE Page 11
INTRODUCTION TO C PROGRAMMING 1BPLC105E
An operating system (OS) is a program that manages and controls all the operations of a
computer. It acts as the interface between the user and the computer hardware.
When a C program is executed, the operating system loads the executable file into memory and
controls its execution.
Fig. 1.3: Process of compiling and running a C program
NOOR SUMAIYA, [Link]/DEPT OF CSE, TOCE Page 12
INTRODUCTION TO C PROGRAMMING 1BPLC105E
Fig. 1.4: Generation of executable code from source code
The compiler is specific to a programming language, and the object code is dependent upon the
platform on which it is meant to run
NOOR SUMAIYA, [Link]/DEPT OF CSE, TOCE Page 13