C Language 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.
Importance of C programming
C programming is a powerful and versatile programming language that plays a significant role
in software development. With its simplicity, efficiency, and wide range of applications, C has stood the
test of time and continues to be a popular choice among programmers worldwide.
The importance of C programming cannot be overstated in the world of computer science and
programming. As one of the oldest and most widely used programming languages, C has become the
foundation for many modern programming languages.
In short, C programming holds immense importance for anyone interested in the world of
coding and software development.
History of C Language
The C programming language’s history can be traced to Dennis Ritchie, who developed it at
Bell Labs in the early 1970s. It evolved from an earlier language called B, which was developed by Ken
Thompson. B itself was based on the BCPL language.
C gained popularity due to its portability and efficiency. In 1978, Brian Kernighan and Dennis
Ritchie published “The C Programming Language,” which became the go-to reference for C programmers.
Since then, C has been widely used and has influenced the development of other programming
languages.
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.
Example:
#include <stdio.h>
int main()
int a = 10;
printf("%d", a);
return 0;
Structure of the C Program
Sections of the C Program
There are 6 basic sections responsible for the proper execution of a program. Sections are mentioned
below:
1. Documentation
2. Preprocessor Section
3. Definition
4. Global Declaration
5. Main() Function
6. Sub Programs
[Link] section
This section consists of the description of the program, the name of the program, and the
creation date and time of the program. It is specified at the start of the program in the form of
comments. Documentation can be represented as:
/*
description, name of the program, programmer name, date, time etc.
*/
2. Preprocessor Section
All the header files of the program will be declared in the preprocessor section of the program.
Header files help us to access other’s improved code into our code. A copy of these multiple files is
inserted into our program before the process of compilation.
Example:
#include<stdio.h>
#include<math.h>
3. Definition
Preprocessors are the programs that process our source code before the process of
compilation. There are multiple steps which are involved in the writing and execution of the program.
Preprocessor directives start with the ‘#’ symbol. The #define preprocessor is used to create a
constant throughout the program. Whenever this name is encountered by the compiler, it is replaced
by the actual piece of defined code.
Example:
#define long long ll
4. Global Declaration
The global declaration section contains global variables, function declaration, and static
variables. Variables and functions which are declared in this scope can be used anywhere in the
program.
Example:
int num = 18;
5. Main() Function
Every C program must have a main function. The main() function of the program is written in
this section. Operations like declaration and execution are performed inside the curly braces of the
main program. The return type of the main() function can be int as well as void too. void() main tells
the compiler that the program will not return any value. The int main() tells the compiler that the
program will return an integer value.
Example:
void main()
or
int main()
6. Sub Programs
User-defined functions are called in this section of the program. The control of the program is
shifted to the called function whenever they are called from the main or outside the main() function.
These are specified as per the requirements of the programmer.
Example:
int sum(int x, int y)
return x+y;
Execution of the C Program?
Whenever a C program file is compiled and executed, the compiler generates some files with
the same name as that of the C program file but with different extensions.
Every file that contains a C program must be saved with ‘.c’ extension. This is necessary for the
compiler to understand that this is a C program file. Suppose a program file is named, first.c. The file
first.c is called the source file which keeps the code of the program. Now, when we compile the file,
the C compiler looks for errors.
If the C compiler reports no error, then it stores the file as a .obj file of the same name, called
the object file. So, here it will create the [Link]. This .obj file is not executable. The process is
continued by the Linker which finally gives a .exe file which is executable.
Linker:
First of all, let us know that library functions are not a part of any C program but of the C
software. Thus, the compiler doesn’t know the operation of any function, whether it be printf or
scanf. The definitions of these functions are stored in their respective library which the compiler
should be able to link.
This is what the Linker does. So, when we write #include, it includes stdio.h library which gives
access to Standard Input and Output. The linker links the object files to the library functions and the
program becomes a .exe file. Here, [Link] will be created which is in an executable format.
Loader:
Whenever we give the command to execute a particular program, the loader comes into
work. The loader will load the .exe file in RAM and inform the CPU with the starting point of the
address where this program is loaded.
Character Set in C
As every language contains a set of characters used to construct words, statements, etc., C
language also has a set of characters which include alphabets, digits, and special symbols. C language
supports a total of 256 characters.
Every C program contains statements. These statements are constructed using words and these
words are constructed using characters from C character set. C language character set contains the
following set of characters...
1. Alphabets
2. Digits
3. Special Symbols
Alphabets
C language supports all the alphabets from the English language. Lower and upper
case letters together support 52 alphabets.
lower case letters - a to z
UPPER CASE LETTERS - A to Z
Digits
C language supports 10 digits which are used to construct numerical values in C language.
Digits - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Special Symbols
C language supports a rich set of special symbols that include symbols to perform
mathematical operations, to check conditions, white spaces, backspaces, and other special
symbols.
Special Symbols - ~ @ # $ % ^ & * ( ) _ - + = { } [ ] ; : ' " / ? . > , < \ | tab newline space
NULL bell backspace verticaltab etc.,
Tokens in C
A token in C can be defined as the smallest individual element of the C programming
language that is meaningful to the compiler. It is the basic component of a C program.
Types of Tokens in C
The tokens of C language can be classified into six types based on the functions
they are used to perform. The types of C tokens are as follows:
1. Keywords
2. Identifiers
3. Constants
4. Strings
5. Special Symbols
6. Operators
C Token – Keywords
The keywords are pre-defined or reserved words in a
programming language. Each keyword is meant to perform a
specific function in a program. Since keywords are referred names
for a compiler, they can’t be used as variable names because by
doing so, we are trying to assign a new meaning to the keyword
which is not allowed. You cannot redefine keywords. However, you
can specify the text to be substituted for keywords before
compilation by using C preprocessor directives. C language
supports 32 keywords which are given below:
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
C Token – Identifiers
Identifiers are used as the general terminology for the naming of variables, functions,
and arrays. These are user-defined names consisting of an arbitrarily long sequence of letters
and digits with either a letter or the underscore(_) as a first character.
Identifier names must differ in spelling and case from any keywords. You cannot use
keywords as identifiers; they are reserved for special use. Once declared, you can use the
identifier in later program statements to refer to the associated value. A special identifier called
a statement label can be used in goto statements.
Rules for Naming Identifiers
Certain rules should be followed while naming c identifiers which are as follows:
They must begin with a letter or underscore(_).
They must consist of only letters, digits, or underscore. No other special character is
allowed.
It should not be a keyword.
It must not contain white space.
It should be up to 31 characters long as only the first 31 characters are significant.
C Token – Constants
The constants in C are the read-only variables whose values cannot be modified once
they are declared in the C program. The type of constant can be an integer constant, a floating
pointer constant, a string constant, or a character constant. In C language, the const keyword is
used to define the constants.
What is a constant in C?
a constant in C is a variable that cannot be modified once it is declared in the program. We
can not make any change in the value of the constant variables after they are defined.
How to Define Constant in C?
We define a constant in C language 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.
Syntax to Define Constant
const data_type var_name = value;
C Variables
A variable in C language is the name associated with some memory location to store
data of different types. There are many types of variables in C depending on the scope,
storage class, lifetime, type of data they store, etc. A variable is the basic building block of a C
program that can be used in expressions as a substitute in place of the value it stores.
C Variable Syntax
The syntax to declare a variable in C specifies the name and the type of the variable.
data_type variable_name = value; // defining single variable
or
data_type variable_name1, variable_name2; // defining multiple variable
Here,
data_type: Type of data that a variable can store.
variable_name: Name of the variable given by the user.
value: value assigned to the variable by the user.
Example
int var; // integer variable
char a; // character variable
float fff; // float variables
There are 3 aspects of defining a variable:
1. Variable Declaration
2. Variable Definition
3. Variable Initialization
1. C Variable Declaration
Variable declaration in C tells the compiler about the existence of the
variable with the given name and data [Link] the variable is declared,
an entry in symbol table is created and memory will be allocated at the time
of initialization of the variable.
2. C Variable Definition
In the definition of a C variable, the compiler allocates some memory and
some value to it. A defined variable will contain some random garbage value
till it is not initialized.
Example
int var;
char var2;
C Variable Initialization
Initialization of a variable is the process where the user assigns some meaningful value to
the variable.
Example
int var; // variable definition
var = 10; // initialization
or
int var = 10; // variable declaration and definition
Rules for Naming Variables in C
You can assign any name to the variable as long as it follows the following rules:
1. A variable name must only contain alphabets, digits, and underscore.
2. A variable name must start with an alphabet or an underscore only. It cannot start with a
digit.
3. No whitespace is allowed within the variable name.
4. A variable name must not be any reserved word or keyword.
Data Types in C
Each variable in C has an associated data type. It specifies the type of data that the variable can
store like integer, character, floating, double, etc. Each data type requires different amounts of memory
and has some specific operations which can be performed over it. The data type is a collection of data
with values having fixed values, meaning as well as its characteristics.
Size Format
Data Type (bytes) Range Specifier
short int 2 -32,768 to 32,767 %hd
unsigned short
2 0 to 65,535 %hu
int
unsigned int 4 0 to 4,294,967,295 %u
-2,147,483,648 to
int 4 %d
2,147,483,647
-2,147,483,648 to
long int 4 %ld
2,147,483,647
unsigned long
4 0 to 4,294,967,295 %lu
int
Size Format
Data Type (bytes) Range Specifier
long long int 8 -(2^63) to (2^63)-1 %lld
unsigned long 0 to
8 %llu
long int 18,446,744,073,709,551,615
signed char 1 -128 to 127 %c
unsigned char 1 0 to 255 %c
float 4 %f
1.2E-38 to 3.4E+38
double 8 %lf
1.7E-308 to 1.7E+308
long double 16
3.4E-4932 to 1.1E+4932 %Lf
Integer Data Type
The integer datatype in C is used to store the integer numbers(any number including positive,
negative and zero without decimal part). Octal values, hexadecimal values, and decimal values
can be stored in int data type in C.
Range: -2,147,483,648 to 2,147,483,647
Size: 4 bytes
Format Specifier: %d
Syntax of Integer
We use int keyword to declare the integer variable:
int var_name;