Unit 1
Introduction to C Language
C is a general-purpose procedural programming language initially developed
by Dennis Ritchie in 1972 at Bell Laboratories of AT&T Labs. It was mainly
created as a system programming language to write the UNIX operating
system. But its versatility and efficiency have made it one of the most widely
used programming languages in the world.
Key Features of C Language
1. Simple and Efficient: C is easy to learn and provides high performance.
2. Portability: Programs written in C can run on different machines with
minimal or no modification.
3. Low-Level Access: It allows direct manipulation of hardware and
memory, making it ideal for system programming.
4. Structured Programming: C supports modular programming, making
code easier to debug and maintain.
5. Rich Library Support: It provides a wide range of built-in functions for
various tasks.
Applications of C
Operating Systems: Most operating systems, including UNIX and Linux,
are written in C.
Embedded Systems: C is widely used in programming microcontrollers
and embedded systems due to its efficiency and low-level capabilities
System Software: C is used to create system software like compilers,
assemblers, and interpreters.
Databases: C is used in the development of database management
systems like MySQL and Oracle.
Networking: It is used for developing network drivers and protocols.
Networking: It is used for developing network drivers and protocols.
Games: C is also used to developed games.
Structures of ‘C’ Programming
A C program is organized into several sections to ensure clarity,
maintainability, and proper execution.
1. Documentation Section
This section contains comments to describe the program, its purpose,
and any other relevant information.
Comments are written using:
o Single-line comments: // This is a comment
o Multi-line comments: /* This is a multi-line comment */
2. Preprocessor Directives
These include header files and macros that are required for the program.
Commonly used directives:
#include <stdio.h> // Standard Input/Output functions
#include <stdlib.h> // Standard library functions
3. Global Declarations
Variables, constants, or functions that need to be accessed throughout
the program are declared here.
Example:
int globalVar = 10; // Global variable
4. main() Function
The main() function is the entry point of every C program.
It contains the core logic of the program and is mandatory.
Example:
int main() {
// Code execution starts here
return 0; // Indicates successful execution
}
5. Subprograms/Functions
User-defined functions are written here to modularize the code.
Example:
void greet()
{
printf("Hello, World!");
}
6. Statements and Expressions
Inside the main() function or other functions, you write the actual logic
using statements and expressions.
Example:
int a = 5, b = 10;
int sum = a + b;
printf("Sum: %d", sum);
Example :
// A program to print value of global variable x
#include <stdio.h>
int x = 10;
int main()
{
printf("Value of x is : %d\n", x);
return 0;
}
Functions as Building Blocks
Function: A block of code that performs a specific task.
main() is the starting point.
C programs are divided into user-defined functions and library
functions.
Advantages of Using Functions:
Modularity
Code reuse
Easy debugging
Improved readability
Language Fundamentals
1. Character Set: C uses characters from:
Alphabets: A–Z, a–z
Digits: 0–9
Special Symbols: ~ ! @ # $ % ^ & * ( ) _ + - = { } [ ] : ; ' " < > , . ? / \ |
White Space Characters: Space, Tab, Newline
2. Tokens: a token is the smallest individual unit (or building block) of a
program that is meaningful to the compiler.
There are 6 main types of tokens in C:
Token Type Description Example
1. Keywords Reserved words with special meaning in C int, return, if
2. Identifiers Names given to variables, functions, arrays, etc. sum, total, main
3. Constants Fixed values that do not change during execution 10, 3.14, 'A'
Symbols that perform operations on variables
4. Operators +, -, *, ==
and values
5. Special
Characters used for structure or syntax in C (), {}, ;, ,
Symbols
Sequence of characters enclosed in double "Hello", "C
6. Strings
quotes Language"
3. Keywords: Keywords in C are reserved words that have special meaning to
the C compiler. In C language there is 32 keyword.
They are predefined by the C language.
We cannot use them as names for variables, functions, or identifiers.
They are used to perform specific operations or define the structure of
a program.
auto break case char
const continue default do
double else enum extern
float for goto if
int long register return
short signed sizeof static
struct switch typedef union
unsigned void volatile while
4. Identifier: An identifier is the name used to identify:
Variables
Functions
Arrays
Structures
User-defined data types, etc.
Identifiers are names that we create in our program.
Ex. int roll,age;
Char name[];
Void greet();
Identifier Rules :-
Identifiers must begin with a letter (A–Z, a–z) or an underscore _
After the first character, you can use letters, digits (0–9), or underscores
Cannot use keywords as identifiers (int, float, if, etc.)
Cannot contain special characters like @, #, $, %, space, etc.
C is case-sensitive — Total and total are different identifiers
Length can vary (though traditionally, only the first 31 characters are
significant in older compilers)
5 Variable: A variable in C is a named storage location in the computer's
memory used to store data values during the execution of a program.
The value of a variable can change during program execution.
You must declare a variable before using it.
Syntax:-
Datatype variablename;
Ex: - int a; //declaration
A=10; //Assignment
Datatype variablename = value;
Ex:- int a = 10; //declaration+assignment
6. Constant: constants are fixed values that do not change during the
execution of a program. They are categorized into the following types:
1. Numeric Constants
a. Integer Constants
Represent whole numbers without decimals.
Can be written in:
o Decimal (base 10) e g. :- ( 45, -9, 0, 10)
o Octal (base 8, prefixed with 0) e g. :- ( 045, 07, 010)
o Hexadecimal (base 16, prefixed with 0x or 0X) e g. :- (
0x45, 0xA)
May include suffixes like U (unsigned), L (long), or LL (long
long).
b. Floating-Point (Real) Constants
Represent numbers with fractional parts or exponents.
Can be written using:
Decimal notation (e.g., 3.14)
Exponential notation (e.g., 1.5e3)
May include suffixes like F/f (float), L/l (long double).
2. Character Constants
Single Character Constants: A single character enclosed in single quotes
(e.g., 'A', '9', '#').
Escape Sequence Constants: Special characters represented using a
backslash (e.g., '\n' for newline, '\t' for tab).
3. String Constants
A sequence of characters enclosed in double quotes (e.g., "Hello", "123",
"C Programming").
Internally represented as an array of characters terminated with a null
character (\0).
4. Symbolic Constants
Defined using the #define preprocessor directive or the const keyword
e.g.,
#define PI 3.14 or
const int MAX = 100;
Constants using const Constants using #define
They are the variables that are They are the macros that are
immutable replaced by their value.
They are handled by the
They are handled by the compiler.
preprocessor.
Syntax: const type name = value; Syntax: #define name value
Enumeration Constants
User-defined set of named integer constants using the enum keyword.
Improves readability and organization of code by assigning meaningful
names to integer values. (e.g., enum Color {RED, GREEN, BLUE};).
Data Types: - A data type defines the type of data a variable can hold. It
specifies the size, range, and operations that can be performed on the data. C
is a statically-typed language, meaning the data type of a variable must be
declared at the time of its definition and cannot be changed later.
Integer Data Type :- 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 also be stored in int data
type.
Character Data Type: - Character data type allows its variable to store only a
single character. The size of the character is 1 byte.
Float Data Type :- float data type is used to store single precision floating-
point values. These values are decimal and exponential numbers.
Double Data Type:- The double data type in C is used to store decimal
numbers (numbers with floating point values) with double precision. It can
easily accommodate about 16 to 17 digits after or before a decimal point.
Void Data Type: - The void data type in C is used to indicate the absence of a
value. Variables of void data type are not allowed. It can only be used for
pointers and function return type and parameters.
Data Type Size Range Format
(bytes) Specifier
short int 2 -32,768 to 32,767 %hd
unsigned short int 2 0 to 65,535 %hu
unsigned int 4 0 to 4,294,967,295 %u
int 4 -2,147,483,648 to %d
2,147,483,647
long int 4 -2,147,483,648 to %ld
2,147,483,647
unsigned long int 4 0 to 4,294,967,295 %lu
long long int 8 -(2^63) to (2^63)-1 %lld
unsigned long long 8 0 to %llu
int 18,446,744,073,709,551,615
signed char 1 -128 to 127 %c
unsigned char 1 0 to 255 %c
float 4 1.2E-38 to 3.4E+38 %f
double 8 1.7E-308 to 1.7E+308 %lf
long double 16 3.4E-4932 to 1.1E+4932 %Lf
Note: The long, short, signed and unsigned are datatype modifier that can be used with some
primitive data types to change the size or length of the datatype.
Note: The size of the data types in C is dependent on the size of the architecture.
Type Conversion: - type conversion is the process of changing one data type
into another. This can happen automatically by the compiler or manually by
the programmer. Type conversion is only performed between data types
where such a conversion is possible.
Format specifiers: - format specifiers are the symbols that are used for printing
and scanning values of given data types.