Lesson 2
Lesson 2
LECTURE 2
Introduction to C
Presented by:
Pratibha Ku. Khandual
Department of CSE,
VSSUT, Burla
Brief History of C
• The C programming language is a structure oriented
programming language, developed at Bell Laboratories in
1972 by Dennis Ritchie.
• C programming language features were derived from an
earlier language called “B” (Basic Combined
Programming Language – BCPL)
• As many of C language ideas and principles were derived from
the earlier language B, thus, this new language was named as
“C”.
• C language was invented for implementing UNIX
operating system.
2
Brief History of C
• In 1978, Dennis Ritchie and Brian Kernighan
published the first edition of “The C Programming
Language” and is commonly known as K&R C.
• In 1983, the American National Standards
Institute (ANSI) established a committee to
provide a modern, comprehensive definition of C.
The resulting definition, the ANSI standard, or
“ANSI C”, was completed late 1988.
3
Why is C popular?
• C is a reliable, simple and easy to use programming
language.
• It is a small, block-structured (modular) programming
language that allows the programmer to code
a program by dividing the whole program into
smaller units or modules.
• This helps in improving the readability of the code
and also makes the program simple to code, debug
and maintain.
4
Why is C popular?
• C is a portable language, which means that C
programs written in one platform can be run on
other platforms with little or no modification.
– Eg. Suppose you are a Windows user but for some
reasons you want to switch to Linux.
– The programs that you have written and executed in
Windows will work and give the same output when
copied and pasted in a LINUX terminal.
5
Why is C popular?
• C has one of the largest collection of operators those
are used for calculations and data comparisons.
• The C standard library provides a large range of
inbuilt functions that makes the program
development quite easy for the programmers.
• The C programming language helps to manage
memory efficiently as it supports the feature of
dynamic memory allocation.
6
Why to study C?
• C serves as the base language or a prerequisite language
before learning other high level programming languages.
• Most of the popularly used high level programming languages
came after the invent of C.
• By the early 1980s, C was already a dominant language
in UNIX systems (minicomputers). Since then, it has
spread to personal computers (microcomputers) and to
mainframes.
• C language is the most widely used language in operating
systems development today.
• Many software companies still use C for producing various
types of software as programs written in C are efficient and
fast.
7
Characteristics of a C Program
• Structured language – C is a general purpose, flexible, and
structured programming language. C is known as a structured
programming language as the entire program is divided into
several blocks/modules that follows top-down design
approach.
• Small size – C language has only 32 keywords.
• Extensive use of function calls – It enables the end user to
add their own user-defined functions to the C programs.
• Rich set of Library Functions – It is a robust language with a
rich set of built-in functions and operators that can be utilized
to write any complex program.
8
Characteristics of a C Program
• Low level (Bitwise) programming – C supports low
level programming features which enable the programmer to
carry out bit-wise operations. These features are normally
provided in assembly language and machine language.
• High level language constructs ̶ In addition to the low level
language constructs, high level constructs (data types,
operators, pointers, dynamic memory allocation, etc.) are
available in C.
• Middle level language – C is usually called a middle-level
language as it stands between high-level languages and low-
level languages. It does not provide all the features found in
high level languages, but provides all building blocks that we
need to write programs and produce results.
9
Characteristics of a C Program
• Pointer implementation ̶ Extensive use of pointers
for memory allocation dynamically, array, structures
and functions.
• Efficient and fast ̶ programs written in C are efficient
and fast due to its variety of data types and powerful
operators.
• Portable – It can be compiled on a variety of computers.
• Supports loose typing – C is considered to be
weakly typed, because we can convert any data type to
any other data type through a cast operator.
10
Uses of C
• Database systems
• Graphics packages
• Word processors
• Spreadsheets
• Operating system development
• Compilers and Assemblers
• Network drivers
• Interpreters
11
Structure of a C Program
• A C program is divided into different sections.
There are six main sections in a basic c
program. The six sections are:
1. Documentation Section
2. Link Section
3. Definition Section
4. Global Declarations Section
5. Main functions Section
6. Subprograms Section
12
Structure of a C Program
13
Structure of a C Program
• Documentation Section
– The Documentation Section consists of a set of
comment lines giving the name of the programmer,
date and other details about the program.
– Comments may appear anywhere within a program.
Text between /* and */ appears as a comment in C.
For Example: /* This is a Comment */
– Documentation section helps anyone to get an
overview of the program.
14
Structure of a C Program
• Link Section
̶ The link section provides instructions to the compiler
to link functions from the system library using
the #include directive.
̶ This leads to the compiler to link the header files from
the system libraries.
̶ Example:
#include<stdio.h>
̶ It tells the C compiler to include the contents of the
system file called stdio.h (Standard Input Output
header file).
15
Structure of a C Program
• Definition Section
– In this section, we define different symbolic constants
using the #define directive.
– The keyword define is used in this part.
– Example:
#define PI = 3.14
– This statement defines a symbolic constant PI with
value 3.14.
16
Structure of a C Program
• Global Declaration Section:
̶ There are some variables that are used in more than
one function. Such variables are called global variables
and are declared in the global declaration section that
is outside of all the functions.
̶ This section also declares all the user-defined
functions.
̶ Example:
float area(float r);
int a=7;
17
Structure of a C Program
• main() function Section:
– Every C program must have one main function section
which is the starting point of the program execution.
– This section contains two parts: declaration part and
executable part.
• Declaration part: The declaration part declares all
the variables used in the executable part.
• Executable part: There is at least one statement in the
executable part.
– These two parts must appear between the opening
and closing braces.
18
Structure of a C Program
• main() function Section:
– The program execution begins at the opening brace and
ends at the closing brace.
– The closing brace of the main function is the logical end of
the program.
– All statements in the declaration and executable part end
with a semicolon.
– Example:
int main(void)
{
int a=10;
printf(" %d", a);
return 0;
}
19
Structure of a C Program
• Subprogram Section:
– The subprogram section contains all the user defined
functions that are used to perform a specific task.
– These user defined functions are called in the main
function.
– User defined functions are generally placed just after
the main() function, although they may appear in any
order.
– Example:
int add(int a, int b)
{
return a+b;
}
20
Sample Program
21
Things to know in the Sample Program
• #include <stdio.h>:
– #include is a pre-processor directive. It is an instruction to
the C compiler to include the contents of the file stdio.h.
– stdio.h is the name of the standard library definition
file for all STanDard Input and Output functions.
• void:
– Many programming languages need a data type to define
the lack of return value to indicate that nothing is being
returned.
– The void data type is typically used in the definition and
prototyping of functions to indicate that either nothing is
being passed and/or nothing is being returned from the
called function.
22
Things to know in the Sample Program
• scanf() and printf()
– The standard input/output library contains a
number of functions for formatted data input
and formatted data output.
– The two functions that are used are scanf()
(scan formatted) and printf() (print formatted)
for reading input from keyboard and displaying
output on screen respectively.
23
Sample Program
/*File Name: areaofcircle.c, Author: Manthan Naik, Date:
09/08/2019, Description: a program to calculate area of circle*/
#include<stdio.h> //link section
#define pi 3.14; //definition section
float area(float r); //global declaration
int main() //main function
{
float r; //Declaration part
printf(" Enter the radius:"); //Executable part
scanf("%f", &r);
printf(“The circle area is: %f", area(r));
return 0;
}
24
Sample Program
float area(float r) //Subprogram Section
{
return (pi * r * r);
}
• Output:
Enter the radius:
7
The circle area is: 153.860001
25
Files Used in a C Program
• Source File:
– This file contains the source code of the program.
– The file extension of any c file is .c.
– The file contains C source code that defines the main
function and may be other functions.
• Header File:
– A header file is a file that contains the set of predefined
standard library functions.
– The "#include" preprocessor directive is used to include
the header files with ".h" extension in the program.
26
Files Used in a C Program
• Object File:
– An object file is a file containing object code, with an
extension .o, which are produced by an assembler,
compiler, or other language translator.
– This is then used as input to the linker, which in turn
generates an executable file by combining the
object files.
• Executable File:
– The binary executable file is generated by the linker.
The linker links the various object files to produce a
binary file that can be directly executed.
27
Compilation and Execution of a C Program
•The compilation process in C involves four
steps: pre-processing, compiling,
assembling, and linking.
•Then, we run the obtained executable file
to get an output on the screen.
28
ANY DOUBTS?
1/23/2025 29
THANK YOU!
1/23/2025 30