UNIT I BASICS OF C PROGRAMMING
Introduction to programming paradigms – Applications of C Language -
Structure of C program– C programming: Data Types - Constants –
Enumeration Constants - Keywords – Operators: Precedence and Associativity -
Expressions - Input/Output statements, Assignment statements –Decision
making statements - Switch statement - Looping statements
INTRODUCTION TO PROGRAMMING PARADIGMS:
1. Introduction
A programming paradigm refers to a style, approach, or methodology of
programming that provides a structured way of thinking about problem-solving and
program design. It defines how programs are written, organized, and executed.
Programming paradigms influence the structure of programs, the flow of control, and
the way data is handled.
Different programming paradigms have evolved over time to overcome the limitations
of earlier approaches and to make software development more efficient, reliable, and
maintainable.
2. Need for Programming Paradigms
Programming paradigms are required because:
They provide a systematic approach to problem solving.
They improve program clarity and readability.
They help in modular programming, making programs easier to debug and
maintain.
They support code reusability.
They enable programmers to choose the best approach based on problem
requirements.
3. Classification of Programming
Paradigms
Programming paradigms can be broadly classified into the following types:
Unstructured Programming
Structured Programming
Procedural Programming
Object-Oriented Programming
Functional Programming
Logic Programming
4. Unstructured Programming Paradigm
Definition
Unstructured programming is an early programming approach where programs are
written as a sequence of instructions without a clear structure.
Characteristics
Uses GOTO statements extensively
Program flow is difficult to understand
No modularity
Code is hard to debug and maintain
Disadvantages
Leads to spaghetti code
Difficult to modify or extend
Poor readability
Example Languages
Early versions of BASIC
Assembly language (partially)
5. Structured Programming Paradigm
Definition
Structured programming organizes programs into well-defined control structures
such as sequence, selection, and iteration.
Key Control Structures
Sequence – Statements executed in order
Selection – Decision making using if, if-else, switch
Iteration – Looping using for, while, do-while
Features
Avoids GOTO statements
Improves readability
Easier debugging
Supports modular programming
Example Languages
C
Pascal
ALGOL
6. Procedural Programming Paradigm
Definition
Procedural programming is an extension of structured programming where a program
is divided into functions or procedures.
Key Features
Program is organized into functions
Functions perform specific tasks
Data is passed between functions
Emphasizes procedure over data
Advantages
Code reusability
Easier testing and debugging
Reduced complexity
Disadvantages
Poor data security
Difficult to manage very large programs
Example Languages
C
FORTRAN
COBOL
C language primarily follows the Procedural
Programming Paradigm.
7. Object-Oriented Programming
Paradigm (OOP)
Definition
Object-oriented programming organizes programs using objects, which contain both
data and functions.
Core Concepts
Class – Blueprint for objects
Object – Instance of a class
Encapsulation – Binding data and functions together
Inheritance – Reusing existing classes
Polymorphism – One interface, multiple implementations
Advantages
Data security
Reusability
Easy maintenance
Real-world modeling
Disadvantages
More complex
Requires more memory
Slower execution compared to C
Example Languages
C++
Java
Python
8. Functional Programming Paradigm
Definition
Functional programming is based on mathematical functions and avoids changing
data or states.
Characteristics
Uses pure functions
No global variables
Emphasizes immutability
No side effects
Advantages
Easier parallel processing
Less error-prone
High reliability
Example Languages
Haskell
Lisp
Scala
9. Logic Programming Paradigm
Definition
Logic programming is based on formal logic, where programs consist of facts and
rules.
Characteristics
Uses logical statements
Execution is rule-based
Programmer defines what to do, not how to do
Example Language
Prolog
Applications
Artificial Intelligence
Expert systems
Home Work
Compare all programming paradigms.
APPLICATIONS OF C LANGUAGE
Operating Systems
One of the most important applications of C language is in the development of
operating systems.
Explanation
C provides direct access to memory using pointers.
It supports low-level hardware interaction.
Programs written in C are fast and efficient.
Examples
UNIX operating system
Linux kernel
Windows components
UNIX is almost entirely written in C language.
Embedded Systems
Explanation
Embedded systems are systems designed to perform a specific function within a larger
system.
Role of C in Embedded Systems
Efficient memory usage
High execution speed
Close interaction with hardware
Easy to write device-specific programs
Examples
Microcontrollers
Washing machines
Microwave ovens
Automotive control systems
System Programming
Explanation
System programming involves writing programs that control system resources such as
memory, processes, and hardware.
Uses of C
Writing system utilities
Developing system-level software
Creating system calls
Examples
Command-line tools
File management utilities
Compiler and Interpreter Development
Explanation
C is widely used for developing compilers and interpreters because of its speed and
control over memory.
Reasons
Efficient execution
Structured programming support
Low-level access
Examples
C compiler
Python interpreter
Java Virtual Machine (partially)
Database Systems
Explanation
C language is used to develop database management systems due to its performance
and reliability.
Examples
MySQL
Oracle (core components)
SQLite
Advantages
Fast data processing
Efficient memory management
Game Development
Explanation
C is used in game programming, especially for performance-critical components.
Uses
Game engines
Graphics rendering
Physics engines
Advantages
High execution speed
Direct hardware acces
Network Programming
Explanation
C language is widely used in network programming.
Applications
Socket programming
Network protocols
Web servers
Examples
TCP/IP implementation
Network utilities
Desktop Applications
Explanation
C is also used to create desktop applications.
Examples
Text editors
Media players
Graphics applications
STRUCTURE OF C PROGRAM
Documentation Section
Link Section
Definition Section
Global Declaration Section
main() Function
Subprogram (User-defined Function) Section
Documentation → Explains program
Link → Connects libraries
Definition → Defines constants
Global → Declares global data
main() → Program starts
Declaration → Declares variables
Executable → Runs logic
Subprogram → Extra functions
/* -------------------------------------------------
Program Name : Add Two Numbers
Author : Student
Description : This program demonstrates
all sections of a C program
---------------------------------------------------*/
/* -------- LINK SECTION -------- */
#include <stdio.h>
/* -------- DEFINITION SECTION -------- */
#define PI 3.14
/* -------- GLOBAL DECLARATION SECTION -------- */
int a, b; // Global variables
int add(int, int); // Function declaration
/* -------- MAIN FUNCTION -------- */
int main()
{
/* Declaration part (local variables) */
int sum;
/* Executable part */
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum = add(a, b);
printf("Sum = %d\n", sum);
printf("Value of PI = %.2f\n", PI);
return 0;
}
/* -------- SUBPROGRAM SECTION -------- */
int add(int x, int y)
{
return x + y;
}
HOMEWORK
Write your own program which have all sections mentioned in the structure of c
program
1. Basic Data Types
Basic data types are the fundamental building blocks of C. They are used to store
simple values.
1.1 int
Used to store integer (whole) numbers
Can store positive and negative values
Example:
int a = 10;
Size: 2 or 4 bytes (depends on compiler)
1.2 float
Used to store decimal (fractional) values
Single precision floating-point
Example:
float pi = 3.14;
Size: 4 bytes
1.3 double
Used to store large decimal values
Double precision, more accurate than float
Example:
double value = 123.456789;
Size: 8 bytes
1.4 char
Used to store a single character
Written inside single quotes
Example:
char grade = 'A';
Size: 1 byte
1.5 bool
Used to store true or false values
In C, bool is available using <stdbool.h>
true = 1, false = 0
Example:
#include <stdbool.h>bool flag = true;
1.6 void
Represents no value
Used when a function returns nothing
Also used with pointers
Example:
void display()
{
printf("Hello");
}
2. Derived Data Types
Derived data types are formed using basic data types. They help in storing multiple
or complex data.
2.1 Array
Collection of elements of the same data type
Stored in continuous memory locations
Example:
int a[5] = {1, 2, 3, 4, 5};
Use: Storing multiple values of same type
2.2 Pointer
Stores the address of another variable
Used for dynamic memory and efficient programs
Example:
int x = 10;int *p = &x;
2.3 Function
A block of code that performs a specific task
Helps in modular programming
Example:
int add(int a, int b)
{
return a + b;
}
3. User Defined Data Types
User-defined data types allow programmers to create their own data types according
to program needs.
3.1 Structure
Collection of different data types under one name
Each member has separate memory
Example:
struct student
{
int roll;
char name[20];
float marks;
};
3.2 Union
Similar to structure
All members share the same memory
Only one member is used at a time
Example:
union data
{
int i;
float f;
};
3.3 enum (Enumeration)
User-defined data type with named integer constants
Improves code readability
Example:
enum day {Mon, Tue, Wed, Thu, Fri};
CONSTATNTS
· Integer constants (e.g., 10, -5)
· Floating-point constants (e.g., 3.14, 2.5e2)
· Character constants (e.g., 'A', '9')
· String constants (e.g., "Hello")
· Symbolic constants (defined using #define)
· Enumeration constants (defined using enum)
ENUMERATION CONSTANTS
Purpose of enum
To represent a collection of related constants under one type
To improve the readability of code instead of using magic numbers
To make the program easier to understand and maintain
3. Syntax
enum enum_name { constant1, constant2, constant3, ... };
enum_name is the name of the enumeration type (optional)
constant1, constant2, ... are the enumeration constants (identifiers)
Each constant is assigned an integer value automatically starting from 0 by
default
4. How enum works
By default, the first constant has the value 0, the next 1, and so on.
You can manually assign specific integer values to constants.
Subsequent constants without assigned values continue incrementing from the
previous value.
5. Example
enum days { Sunday, Monday, Tuesday, Wednesday, Thursday,
Friday, Saturday };
Sunday = 0
Monday = 1
Tuesday = 2, and so on.
You can also assign specific values:
enum months { Jan = 1, Feb, Mar, Apr, May };
Jan = 1
Feb = 2 (automatically next integer)
Mar = 3, etc
6. Using enum variables
enum days today;
today = Wednesday;
You can print or use the enum values as integers:
printf("%d", today); // Output will be 3
7. Advantages of enum
Makes code more readable by using names instead of numbers
Reduces errors caused by using incorrect numeric constants
Helps group related constants logically
Improves code maintainability
8. Important Notes
Enum constants are of type int.
Enum variables can be assigned any integer, not just the constants.
Usually used in switch-case statements for clarity.
9. Complete example program
#include <stdio.h>
enum color { RED, GREEN, BLUE };
int main() {
enum color favorite;
favorite = GREEN;
printf("Favorite color code is %d\n", favorite); // Output: 1
return 0;
}
Keywords
Keywords are reserved words in the C programming language that have special
meaning. These words cannot be used as identifiers (like variable names, function
names, etc.) because they are part of the syntax of the language.
2. Characteristics of Keywords
Reserved by the compiler
Always written in lowercase (by convention)
Have predefined meanings and functionalities
Cannot be redefined or used for any other purpose
3. Purpose of Keywords
Keywords are used to:
Define data types (e.g., int, float)
Control program flow (e.g., if, while, switch)
Define storage classes (e.g., static, extern)
Handle functions (e.g., return)
Handle constants (e.g., const)
4. List of Important Keywords in C
(Total 32 in Standard C)
Keywor
Use/Meaning
d
auto Automatic storage class
break Exit from loop or switch
case Defines a case in switch
char Character data type
const Defines constant value
continu
Skip rest of loop iteration
e
default Default case in switch
do Do-while loop
double Double precision float
else Alternative branch in if
enum Enumeration data type
extern External variable declaration
float Floating point data type
for For loop
goto Jump to label
if Conditional statement
int Integer data type
long Long integer data type
register Register storage class
return Return from function
short Short integer data type
signed Signed integer modifier
sizeof Get size of variable/type
Keywor
Use/Meaning
d
static Static storage class
struct Structure data type
switch Switch statement
typedef Create new data type name
union Union data type
unsigne
Unsigned integer modifier
d
void No data / no return type
Variable may change
volatile
unexpectedly
while While loop
5. Example Showing Keywords
int main() {
int a = 10; // int is a keyword
if (a > 5) { // if is a keyword
printf("Hello\n");
}
return 0; // return is a keyword
}
6. Why Keywords Cannot Be Used as
Identifiers
Using keywords as variable or function names will cause compile-time errors
because the compiler recognizes these words as part of the language syntax.
OPERATORS
1. Increment/Decrement Operators (++, --)
Type: Unary Operator (operates on one operand)
Purpose: Increase (++) or decrease (--) an integer value by 1.
#include <stdio.h>
int main() {
int a = 5;
a++; // Increment a by 1 (a = 6)
printf("a after increment: %d\n", a);
a--; // Decrement a by 1 (a = 5)
printf("a after decrement: %d\n", a);
return 0;
}
Arithmetic Operators (+, -, *, /, %)
Type: Binary Operator (operates on two operands)
Purpose: Perform basic arithmetic operations: addition, subtraction,
multiplication, division, modulus.
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a %% b = %d\n", a % b); // % is modulus operator
return 0;
Relational Operators (<, <= , >, >= , ==, !=)
Type: Binary Operator
Purpose: Compare two values and return true (1) or false (0).
#include <stdio.h>
int main() {
int x = 5, y = 10;
printf("x < y: %d\n", x < y);
printf("x <= y: %d\n", x <= y);
printf("x == y: %d\n", x == y);
printf("x != y: %d\n", x != y);
return 0;
Logical Operators (&&, ||, !)
PRECEEDENCE OF OPERATORS
Expressions, Input/Output
Statements, and Assignment
Statements in C
1. Expressions in C
Definition
An expression in C is a combination of variables, constants, and operators that
produces a single value after evaluation.
Example
a+b
x * y + 10
Types of Expressions
Arithmetic Expression
Uses arithmetic operators (+, -, *, /, %).
Example
sum = a + b;
area = l * b;
Relational Expression
Uses relational operators and returns true (1) or false (0).
Example
a>b
x == y
Logical Expression
Uses logical operators (&&, ||, !).
Example
(a > b) && (b > c)
Assignment Expression
Uses assignment operator (=).
Example
x = 10;
Conditional Expression
Uses ternary operator ?:.
Example
max = (a > b) ? a : b;
Input Statements in C
Purpose
Input statements are used to accept data from the user.
scanf() Function
Used to read input from the keyboard
Requires address operator (&)
Syntax
scanf("format_specifier", &variable);
Example
int a;scanf("%d", &a);
ypes of Decision Making Statements in C
C provides the following decision making statements:
1.
if statement
2.
3.
if–else statement
4.
5.
else-if ladder
6.
7.
Nested if statement
8.
9.
switch statement
10.
3. if Statement
Explanation
The if statement executes a block of code only when the condition is true.
Syntax
if(condition)
{
statements;
}
Example
int a = 10;
if(a > 5)
{
printf("a is greater than 5");
}
4. if–else Statement
Explanation
The if–else statement executes one block of code when the condition is true and
another block when it is false.
Syntax
if(condition)
{
statements;
}else
{
statements;
}
Example
int a = 3;
if(a > 5)
{
printf("a is greater than 5");
}else
{
printf("a is less than or equal to 5");
}
5. else-if Ladder
Explanation
The else-if ladder is used to check multiple conditions one by one.
Syntax
if(condition1)
{
}else if(condition2)
{
}else if(condition3)
{
}else
{
}
Example
int marks = 75;
if(marks >= 90)
printf("Grade A");else if(marks >= 75)
printf("Grade B");else if(marks >= 50)
printf("Grade C");else
printf("Fail");
6. Nested if Statement
Explanation
An if statement inside another if statement is called a nested if.
Syntax
if(condition1)
{
if(condition2)
{
statements;
}
}
Example
int a = 10, b = 5;
if(a > b)
{
if(b > 0)
{
printf("a is greater and b is positive");
}
}
7. switch Statement
Explanation
The switch statement is used when a variable is compared with multiple constant
values.
Syntax
switch(expression)
{
case value1:
statements;
break;
case value2:
statements;
break;
default:
statements;
}
Example
int choice = 2;
switch(choice)
{
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
case 3:
printf("Three");
break;
default:
printf("Invalid choice");
}
Types of Looping Statements in C
C provides three types of loops:
1.
for loop
2.
3.
while loop
4.
5.
do–while loop
6.
3. for Loop
Explanation
The for loop is used when the number of iterations is known in advance.
Syntax
for(initialization; condition; increment/decrement)
{
statements;
}
Example
int i;for(i = 1; i <= 5; i++)
{
printf("%d ", i);
}
Output: 1 2 3 4 5
4. while Loop
Explanation
The while loop executes the block of code as long as the condition is true.
The condition is checked before execution.
Syntax
while(condition)
{
statements;
}
Example
int i = 1;while(i <= 5)
{
printf("%d ", i);
i++;
}
5. do–while Loop
Explanation
The do–while loop executes the block of code at least once, even if the condition is
false.
The condition is checked after execution.
Syntax
do
{
statements;
}while(condition);
Example
int i = 1;do
{
printf("%d ", i);
i++;
}while(i <= 5);
6. Difference Between while and do–
while
while loop do–while loop
Condition checked Condition checked
first last
Executes at least
May not execute at all
once
Entry-controlled loop Exit-controlled loop
7. Nested Loops
Explanation
A loop inside another loop is called a nested loop.
Example
int i, j;for(i = 1; i <= 3; i++)
{
for(j = 1; j <= 3; j++)
{
printf("* ");
}
printf("\n");
}
8. Loop Control Statements
8.1 break Statement
Used to terminate the loop immediately
int i;for(i = 1; i <= 5; i++)
{
if(i == 3)
break;
printf("%d ", i);
}
Output: 1 2
8.2 continue Statement
Skips the current iteration and continues with the next iteration
int i;for(i = 1; i <= 5; i++)
{
if(i == 3)
continue;
printf("%d ", i);
}
Output: 1 2 4 5
9. Applications of Loops
Printing patterns
Calculating sum or factorial
Searching and sorting
Repeating tasks