Module 1
Introduction
● BCPL (Basic Combined Programming Language)
a. Developed by Martin Richards in 1966.
b. It was a simple, low-level language designed for system programming,
especially writing compilers.
● B Language
○ Created by Ken Thompson at Bell Labs in 1969.
○ B was derived from BCPL and used for early versions of the UNIX
operating system on the DEC PDP-7 computer.
● Dennis Ritchie at Bell Labs started developing C around 1972 as an
improvement to B.
● K&R C (1978):
○ The book "The C Programming Language" by Brian Kernighan and
Dennis Ritchie documented the language.
● ANSI C (1989):
○ In 1983, the ANSI (American National Standards Institute) formed a
committee to standardize C.
Compiler
● In C, a compiler is a software program that translates
human-readable C source code into machine code, a series of
0s and 1s that a computer's processor can directly execute.
● How a C compiler works:
○ Preprocessing: The compiler first processes directives,
such as #include and #define, to prepare the source
code.
○ Compilation: The preprocessed code is translated into
an intermediate form, often assembly language.
○ Assembly: The assembly code is then converted into
machine code, which is processor-specific.
○ Linking: The compiler links together necessary library
files and object code to produce the final executable file
that the computer can run.
Interpreter
● An Interpreter directly executes instructions written in a
programming or scripting language without previously
converting them to an object code or machine code.
● Interpreter transforms high level language code into
machine-understandable code or an intermediate
language that can be executed well.
● An interpreter reads every line of code, translates it into
machine code and then immediately executes it. The
interpreter executes every source statement line by line
during execution.
● An interpreter provides detailed error messages and
supports interactive debugging.
Basic structure of a C program
Structure of C program
● Header Files Inclusion: 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.
Ex: stdio.h, conio.h,stdlib.h, math.h, string.h
● Main Method Declaration: The next part of a C program is to declare the main() function.
● Variable Declaration: It refers to the variables that are to be used in the function, the
variables are to be declared before any operation in the function.
● Body: Body of a function in C program, refers to the operations that are performed in the
functions. It can be anything like manipulations, searching, sorting, printing, etc.
● Return Statement: The return statement refers to the returning of the values from a
function. This return statement and return value depend upon the return type of the function
Example program
#include <stdio.h> // Required for printf()
int main() {
printf("My Name is Merrin\n");
return 0; // Indicates successful program execution
}
Data types in C
● int: Stores whole numbers (integers) without decimal points. Modifiers
like short, long, signed, and unsigned can be used to specify the
range and storage size.
● char: Stores single characters (letters, numbers, symbols) enclosed in
single quotes.
● float: Stores floating-point numbers (numbers with decimal points) with
single precision.
● double: Stores floating-point numbers with double precision, offering
more accuracy and a larger range than float.
Type Modifiers
Type modifiers are keywords used to alter the default properties of basic data types, primarily affecting their range or memory
allocation. The main type modifiers in C are:
● signed:
● This modifier allows a data type (like int or char) to store both positive and negative values.
● It is the default for int and char if no other sign modifier is specified.
● unsigned:
● This modifier restricts a data type to store only non-negative (zero and positive) values.
● It effectively doubles the positive range of a data type by reallocating the bit used for the sign to represent magnitude.
● short:
● This modifier is used with int to indicate a smaller range and typically less memory allocation than a standard int.
● For example, short int or simply short.
● long:
● This modifier is used with int and double to indicate a larger range and typically more memory allocation than their
standard counterparts.
● For example, long int (or long), long double.
● long long:
● This modifier is an extension to long for int and provides an even larger range and memory allocation.
● For example, long long int or simply long long.
Format specifiers
● Format specifiers in C are special
characters used within functions like
printf() and scanf() to indicate
the data type of the variable being
processed and how it should be
formatted for input or output.
● They act as placeholders within a
format string, telling the compiler how
to interpret or display the
corresponding data.
Write a program to Add two numbers
How to Design an Algorithm?
To write an algorithm, the following things are needed as a prerequisite:
1. The problem that is to be solved by this algorithm i.e. clear problem definition.
2. The constraints of the problem must be considered while solving the problem.
3. The input to be taken to solve the problem.
4. The output is to be expected when the problem is solved.
5. The solution to this problem is within the given constraints.
Algorithm to add 2 numbers and print their sum:
1. START
2. Declare 2 integer variables num1 and num2
3. Take the two numbers, to be added, as inputs in variables num1 and num2 respectively.
4. Declare an integer variable sum to store the resultant sum of the 2 numbers.
5. Add the 2 numbers and store the result in the variable sum.
6. Print the value of the variable sum
7. END
Flowchart
A flowchart is a diagram that depicts the stages of a process, workflow, computer program or
system.
Addition of two numbers
Identifiers
Identifiers are the names used to identify variables, functions, arrays, structures, or any
other user-defined items. It is a name that uniquely identifies a program element and can
be used to refer to it later in the program.
Rules for Naming Identifiers in C
A programmer must follow a set of rules to create an identifier in C:
● Identifier can contain following characters:
○ Uppercase (A-Z) and lowercase (a-z) alphabets.
○ Numeric digits (0-9).
○ Underscore (_).
● The first character of an identifier must be a letter or an underscore.
● Identifiers are case-sensitive.
● Identifiers cannot be keywords in C (such as int, return, if, while etc.).
Variables in C
A variable in C is a named piece of memory which is used to store data and access it whenever required. It
allows us to use the memory without having to memorize the exact memory address.
To create a variable in C, we have to specify a name and the type of data it is going to store in the syntax.
Variable Initialization:
Once the variable is declared, we can store useful values in it. The first value we store is called initial value
and the process is called Initialization. It is done using assignment operator (=).
Eg: int num;
num = 3;
It is important to initialize a variable because a C variable only contains garbage value when it is declared.
We can also initialize a variable along with declaration.
int num = 3;
Constants in C
● In C programming, const is a keyword used to declare a
variable as constant, meaning its value cannot be changed
after it is initialized.
● It is mainly used to protect variables from being
accidentally modified, making the program safer and
easier to understand.
● These constants can be of various types, such as integer,
floating-point, string, or character constants.
● We define a constant in C using the const keyword. Also
known as a const type qualifier, the const keyword is
placed at the start of the variable declaration to declare
that variable as a constant.
Naming Conventions
In C programming, naming conventions are not strict rules but are commonly followed suggestions by the
programming community for identifiers to improve readability and understanding of code. Below are some
conventions that are commonly used:
For Variables:
● Use camelCase for variable names (e.g., frequencyCount, personName).
● Constants can use UPPER_SNAKE_CASE (e.g., MAX_SIZE, PI).
● Start variable names with a lowercase letter.
● Use descriptive and meaningful names.
For Functions:
● Use camelCase for function names (e.g., getName(), countFrequency()).
● Function names should generally be verbs or verb phrases that describe the action.
printf() and scanf()
In C programming, printf() and scanf() are standard library functions used for console input and output operations,
respectively. They are defined in the <stdio.h> header file.
1. printf() Function (Formatted Output):
The printf() function is used to display formatted output on the console. It allows printing literal text, values of variables,
and formatted data using format specifiers and escape sequences.
printf("format string", argument_list);
2. scanf() Function (Formatted Input):
The scanf() function is used to read formatted input from the user via the console. It allows the program to pause and
wait for the user to enter data, which is then stored in specified variables.
scanf("format string", address_list);
Example
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
printf("You entered: %d and %d\n", num1, num2);
return 0;
}
Memory mapping in C
● The memory layout of a program refers to how the program’s data is stored in the computer
memory during its execution.
● A C program's memory is organized into specific regions (segments) as shown in the below image,
each serving distinct purposes for program execution.
Text Segment: The text segment (also known as code segment) is where the executable code of the program
is stored. It contains the compiled machine code of the program's functions and instructions. This segment is
usually read-only and stored in the lower parts of the memory to prevent accidental modification of the code while
the program is running.
Data Segment: The data segment stores global and static variables that are created by the programmer. It is
present just above the code segment of the program. It can be further divided into two parts:
● Initialized Data Segment: As the name suggests, it is the part of the data segment that contains global
and static variables that have been initialized by the programmer.
● Uninitialized Data Segment (BSS): Uninitialized data segment often called the "bss" segment, named
after an ancient assembler operator, that stood for "Block Started by Symbol" contains global and static
variables that are not initialized by the programmer. These variables are automatically initialized to zero at
runtime by the operating system.
Heap Segment: Heap segment is where dynamic memory allocation usually takes place. The heap area begins
at the end of the BSS segment and grows towards the larger addresses from there. It is managed by functions
such as malloc(), realloc(), and free() which in turn may use the brk and sbrk system calls to adjust its size.
Stack Segment: The stack is a region of memory used for local variables and function call management. Each
time a function is called, a stack frame is created to store local variables, function parameters, and return
addresses. This stack frame is stored in this segment.