Computer Program
A computer program is a set of instructions written in a programming language that tells a
computer how to perform a specific task. These instructions are executed by the computer's
processor (CPU) to produce the desired output. A program may consist of one or more
statements arranged in a logical sequence to solve a problem.
Characteristics
Written in a programming language.
Contains a sequence of instructions.
Stored in the computer's memory.
Produces the desired output after execution.
Can be modified and reused.
Types of Programming Languages
1. Low-Level Language
A low-level language is a programming language that is very close to the computer's hardware.
It provides direct control over the hardware and is difficult for humans to understand.
Examples
Machine Language
Assembly Language
Advantages
Fast execution
Efficient memory usage
Direct hardware control
Disadvantages
Difficult to write and understand
Machine dependent
Difficult to debug
2. High-Level Language
A high-level language uses English-like words and symbols, making it easier for programmers to
write, understand, and maintain programs.
Examples
C
C++
Java
Python
C#
Advantages
Easy to learn and write
Machine independent
Easier debugging
Faster program development
Disadvantages
Requires a translator (compiler or interpreter)
Slightly slower than machine language
Note: C is called a middle-level language because it supports both high-level and low-level
programming features.
Flowchart
A flowchart is a graphical representation of an algorithm or a process using standard symbols
connected by arrows. It illustrates the sequence of operations performed to solve a problem.
Common Flowchart Symbols
Symbol Purpose
Oval Start/Stop
Rectangle Process or Instruction
Symbol Purpose
Parallelogram Input/Output
Diamond Decision Making
Arrow Direction of Flow
Circle Connector
Advantages
Easy to understand.
Simplifies problem-solving.
Helps in debugging.
Improves communication among programmers.
Serves as documentation.
Disadvantages
Time-consuming to prepare.
Difficult to modify for large programs.
Becomes complex for lengthy algorithms.
Algorithm
An algorithm is a finite sequence of well-defined and ordered steps used to solve a particular
problem or perform a specific task.
Characteristics
Input: Accepts zero or more inputs.
Output: Produces at least one output.
Definiteness: Each step must be clear and unambiguous.
Finiteness: Must terminate after a finite number of steps.
Effectiveness: Every step should be simple and executable.
Advantages
Easy to understand.
Independent of programming language.
Helps in program development.
Simplifies debugging.
Saves development time.
Disadvantages
Time-consuming for complex problems.
Lengthy algorithms are difficult to manage.
Compiler and Interpreter
A translator is system software that converts a program written in a high-level language into
machine language.
Compiler
A compiler translates the entire source program into machine code at once before execution. If
errors are found, they are displayed after the complete compilation process.
Features
Translates the whole program.
Generates object/executable code.
Faster execution after compilation.
Displays all errors together.
Examples
GCC
Turbo C
Clang
Interpreter
An interpreter translates and executes the program one statement at a time. If an error occurs,
execution stops immediately.
Features
Translates line by line.
No object code is generated.
Easier to debug.
Slower execution.
Examples
Python Interpreter
JavaScript Engine
Difference Between Compiler and Interpreter
Compiler Interpreter
Translates the entire program Translates one statement at a time
Faster execution Slower execution
Generates object code Does not generate object code
Displays errors after compilation Displays errors immediately
Example: GCC Example: Python
Basic Data Types in C
A data type specifies the type of data that a variable can store and determines the memory
allocation and operations that can be performed on it.
Basic Data Types
Data Type Description Typical Size*
int Stores integer values 4 bytes
char Stores a single character 1 byte
float Stores single-precision decimal values 4 bytes
Data Type Description Typical Size*
double Stores double-precision decimal values 8 bytes
void Represents no value or no return type No storage
*The size may vary depending on the compiler and system architecture.
Example
int age = 20;
char grade = 'A';
float salary = 25000.50;
double pi = 3.1415926535;
Global Declaration
A global declaration refers to the declaration of variables, constants, function prototypes, or
user-defined data types outside all functions, usually before the main() function.
Global declarations are accessible throughout the program unless restricted by scope.
Advantages
Accessible from all functions.
Reduces repeated declarations.
Useful for sharing common data.
Example
#include <stdio.h>
int count = 100; // Global declaration
int main()
printf("%d", count);
return 0;
Constants
A constant is a fixed value that does not change during the execution of a program.
Characteristics
Value remains unchanged.
Improves program readability.
Makes programs easier to maintain.
Types of Constants
Numeric Constants
o Integer constants
o Floating-point constants
Character Constants
String Constants
Enumeration Constants
Symbolic Constants (#define, const)
Examples
100
25.75
'A'
"Hello"
#define PI 3.14
const int MAX = 100;
String Constant
A string constant is a sequence of characters enclosed within double quotation marks ("). A
string is automatically terminated by a null character ('\0').
Characteristics
Enclosed in double quotes.
Stored as a character array.
Ends with a null character ('\0').
Examples
"Computer"
"C Programming"
"Hello World"
Example Program
#include <stdio.h>
int main()
char name[] = "MVJ College";
printf("%s", name);
return 0;
Output
MVJ College
Variable
A variable is a named memory location used to store data whose value can change during
program execution.
Syntax
data_type variable_name;
or
data_type variable_name = value;
Examples
int age;
float salary = 25000.50;
char grade = 'A';
Rules for Naming Variables
Must begin with a letter or underscore (_).
Cannot begin with a digit.
Cannot contain spaces.
Cannot use C keywords.
Variable names are case-sensitive.
Operator
An operator is a special symbol that performs an operation on one or more operands to
produce a result.
Types of Operators in C
1. Arithmetic Operators
o +, -, *, /, %
2. Relational Operators
o ==, !=, >, <, >=, <=
3. Logical Operators
o &&, ||, !
4. Assignment Operators
o =, +=, -=, *=, /=, %=
5. Increment and Decrement Operators
o ++, --
6. Bitwise Operators
o &, |, ^, ~, <<, >>
7. Conditional (Ternary) Operator
o ?:
8. Special Operators
o sizeof, & (address-of), * (pointer), . (member access), -> (structure pointer
member access)
Example
int a = 10, b = 5;
printf("%d\n", a + b); // Addition
printf("%d\n", a > b); // Relational
printf("%d\n", a && b); // Logical
printf("%d\n", sizeof(a)); // Special operator
Importance of Operators
Perform mathematical calculations.
Compare values.
Make decisions.
Manipulate bits.
Access memory efficiently.
Simplify programming.
Structure of a C Program
A C program consists of several sections arranged in a specific order. Each section has a specific
purpose and helps in organizing the program. Although some sections are optional, the main()
function is mandatory because program execution always starts from it.
General Structure of a C Program
Documentation Section
Link Section
Definition Section
Global Declaration Section
main()
Local Declaration Section
Executable Statements
return 0;
User-defined Function Section
Sections of a C Program
1. Documentation Section
This section contains comments that describe the program.
Purpose
Explains the objective of the program.
Improves readability.
Helps other programmers understand the code.
Example
/* Program to find the sum of two numbers */
2. Link Section
This section includes the required header files using the #include directive.
Syntax
#include<stdio.h>
#include<math.h>
Purpose
Provides access to predefined library functions.
Enables the use of input/output, mathematical, and string functions.
3. Definition Section
This section defines symbolic constants using the #define directive.
Example
#define PI 3.14159
#define MAX 100
Advantages
Easy to modify values.
Improves readability.
Avoids repetition.
4. Global Declaration Section
Variables and function prototypes declared outside all functions are called global declarations.
Example
int count;
float salary;
void display();
Characteristics
Accessible throughout the program.
Memory is allocated only once.
Lifetime is the entire program execution.
5. main() Function
The main() function is the entry point of every C program.
Syntax
int main()
// statements
return 0;
Purpose
Execution begins from main().
Contains executable statements.
Returns control to the operating system.
6. Local Declaration Section
Variables declared inside a function are called local variables.
Example
int a, b;
float total;
7. Executable Statements
These are the instructions executed by the processor.
Examples
Assignment statements
Arithmetic operations
Input statements
Output statements
Decision-making statements
Looping statements
8. User-defined Function Section
Functions written by the programmer are placed after the main() function.
Example
void display()
printf("Welcome");
Complete Program Example
#include<stdio.h>
#define PI 3.14
int number;
void message();
int main()
int radius=5;
float area;
area=PI*radius*radius;
printf("Area = %.2f",area);
message();
return 0;
void message()
printf("\nProgram Completed");
Advantages of a Well-Structured C Program
Easy to understand
Easy to debug
Easy to modify
Promotes code reusability
Improves readability
Simplifies maintenance
Variables
A variable is a named memory location used to store data. Its value can change during program
execution.
Syntax
data_type variable_name;
or
data_type variable_name = value;
Examples
int age;
float salary = 25000.50;
char grade = 'A';
Rules for Naming Variables
Must start with a letter or underscore (_).
Cannot begin with a digit.
Cannot contain spaces.
Cannot use C keywords.
Variable names are case-sensitive.
Special characters are not allowed except _.
Types of Variables
Local Variable
Global Variable
Static Variable
External Variable
Example Program
#include<stdio.h>
int main()
int age=20;
float marks=89.5;
char grade='A';
printf("Age = %d\n",age);
printf("Marks = %.2f\n",marks);
printf("Grade = %c",grade);
return 0;
Output
Age = 20
Marks = 89.50
Grade = A
Compilation and Execution Process
A C program cannot be executed directly. It must be converted into machine language before
execution.
Steps in Compilation
1. Source Program
The program written by the programmer is called the source program.
Example:
sample.c
2. Preprocessor
The preprocessor processes all preprocessor directives.
Functions performed:
Expands macros
Includes header files
Removes comments
Output:
Expanded source code
3. Compiler
The compiler translates the source code into assembly language and checks for syntax errors.
Output:
Assembly Code
4. Assembler
The assembler converts assembly language into object code.
Output:
[Link]
or
sample.o
5. Linker
The linker combines object files and library files.
Output:
Executable File
Example:
[Link]
6. Loader
The loader loads the executable program into main memory and starts execution.
Flow of Compilation
Source Program (.c)
Preprocessor
Compiler
Assembly Code
Assembler
Object File (.obj)
▼
Linker
Executable File (.exe)
Loader
Program Output
Constants
A constant is a fixed value that cannot be changed during program execution.
Characteristics
Fixed value
Stored in memory
Improves readability
Makes programs easier to maintain
Types of Constants
1. Numeric Constants
Integer constants
Floating-point constants
Examples
10
250
3.14
-45
2. Character Constants
A single character enclosed within single quotes.
Examples
'A'
'7'
'#'
3. String Constants
A sequence of characters enclosed within double quotation marks.
Examples
"Computer"
"C Programming"
4. Symbolic Constants
Defined using #define or const.
Examples
#define PI 3.14
const int MAX=100;
5. Enumeration Constants
Defined using the enum keyword.
Example
enum day
{
MON,TUE,WED
};
Advantages
Prevent accidental modification.
Improves program readability.
Simplifies maintenance.
Reduces coding errors.
C Language
The C programming language is a general-purpose, procedural programming language
developed by Dennis Ritchie at AT&T Bell Laboratories in 1972. It is widely used for system
programming, application development, and embedded systems.
Because C combines the features of both high-level and low-level languages, it is often called a
middle-level language.
Features of C
Simple and easy to learn
Structured programming language
Portable
Fast execution
Efficient memory management
Rich set of operators
Rich library functions
Supports pointers
Supports recursion
Modular programming through functions
Advantages
Easy to understand
Portable across platforms
Faster execution
Efficient memory usage
Suitable for system programming
Large collection of library functions
Applications of C
Operating systems
Embedded systems
Device drivers
Compilers
Database systems
Networking software
Graphics applications
Scientific applications
Files Used in a C Program
Different types of files are created during the compilation and execution of a C program.
1. Source File (.c)
Contains the program written by the programmer.
Example:
program.c
2. Header File (.h)
Contains declarations of library functions, macros, and constants.
Examples:
stdio.h
math.h
string.h
3. Object File (.obj / .o)
Generated after the assembler converts assembly code into machine code.
Characteristics:
Contains machine code.
Cannot be executed directly.
Used by the linker.
4. Executable File (.exe)
Created by the linker after combining object and library files.
Characteristics:
Ready for execution.
Loaded into memory by the loader.
5. Library Files
Contain predefined functions used by C programs.
Examples:
Standard Input/Output Library
Math Library
String Library
Character Handling Library
Compilation Sequence
Source File (.c)
Preprocessor
Compiler
Assembly File
Assembler
Object File (.obj)
Linker + Library Files
Executable File (.exe)
High-Level Language and Types of Translators
High-Level Language
A high-level language is a programming language that uses English-like words and symbols,
making it easy for humans to read, write, and understand. Since computers cannot directly
understand high-level languages, they must be translated into machine language using
translators.
Characteristics
Easy to learn and use.
Machine independent.
Portable across different platforms.
Supports structured programming.
Easier to debug and maintain.
Examples
C
C++
Java
Python
C#
Types of Translators
1. Compiler
A compiler translates the entire source program into machine code before execution.
Features
Translates the whole program at once.
Generates object/executable code.
Faster execution after compilation.
Displays all errors after compilation.
Examples
GCC
Turbo C
Clang
2. Interpreter
An interpreter translates and executes the program one statement at a time.
Features
Translates line by line.
Stops execution when an error is found.
Does not generate object code.
Easier to debug.
Examples
Python Interpreter
JavaScript Engine
3. Assembler
An assembler converts assembly language into machine language.
Features
Converts assembly instructions into machine code.
Produces object files.
Used in low-level programming.
Comparison of Compiler and Interpreter
Compiler Interpreter
Translates entire program Translates one statement at a time
Faster execution Slower execution
Generates object code No object code generated
Reports errors after compilation Reports errors immediately
Numeric Constants
A numeric constant is a constant that represents a numeric value. Its value remains fixed
throughout the execution of the program.
Types of Numeric Constants
1. Integer Constants
Integer constants are whole numbers without a decimal point.
Examples
10
250
-45
Rules for Integer Constants
Must not contain a decimal point.
May be positive or negative.
Can be written in decimal, octal, or hexadecimal form.
2. Floating-Point Constants
Floating-point constants contain a decimal point or are written in exponential notation.
Examples
3.14
25.75
-0.45
1.5e3
Characteristics
Used to represent real numbers.
Stored in float or double variables.
Can contain a fractional part.
Example Program
#include <stdio.h>
int main()
int age = 20;
float percentage = 89.5;
printf("Age = %d\n", age);
printf("Percentage = %.1f", percentage);
return 0;
Output
Age = 20
Percentage = 89.5
Advantages of Numeric Constants
Represent fixed numeric values.
Improve code readability.
Reduce programming errors.
Make programs easier to maintain.
String Constant
A string constant is a sequence of one or more characters enclosed within double quotation
marks (" "). In C, a string is automatically terminated by a special null character ('\0'), which
indicates the end of the string.
Characteristics of String Constants
Enclosed within double quotation marks (" ").
Stored as an array of characters.
Automatically ends with a null character ('\0').
Used to store words, names, and sentences.
Can contain alphabets, digits, symbols, and spaces.
Syntax
"string"
Examples
"Hello"
"C Programming"
"MVJ College"
"Computer Science"
Example Program
#include <stdio.h>
int main()
char name[] = "MVJ College";
printf("%s", name);
return 0;
Output
MVJ College
Advantages
Easy to store multiple characters.
Improves readability of programs.
Widely used for displaying messages.
Can be manipulated using string library functions.
Input Devices of a Computer
An input device is a hardware device used to enter data and instructions into a computer. These
devices convert user input into a form that the computer can process.
Common Input Devices
1. Keyboard
A keyboard is the most common input device used to enter text, numbers, and special
characters.
Uses
Typing programs
Entering data
Executing commands
2. Mouse
A mouse is a pointing device used to control the cursor on the screen.
Uses
Selecting objects
Opening files
Dragging and dropping items
3. Scanner
A scanner converts printed documents and images into digital form.
Uses
Document scanning
Image scanning
OCR applications
4. Microphone
A microphone is used to input voice and audio into a computer.
Applications
Voice recording
Video conferencing
Speech recognition
5. Webcam
A webcam captures images and videos.
Applications
Online meetings
Video calls
Image capture
6. Joystick
A joystick is mainly used for controlling games and simulations.
7. Light Pen
A light pen is used to draw or select objects directly on the computer screen.
8. Touch Screen
A touch screen allows users to interact directly by touching the display.
Examples
Smartphones
ATMs
Tablets
Advantages of Input Devices
Easy data entry
Faster communication with computers
Improved user interaction
Increased accuracy and efficiency
Input and Output Statements in C
Input and output statements are used for communication between the user and the computer.
Input statements accept data from the user.
Output statements display data on the screen.
Input Functions
1. scanf()
The scanf() function is used to read formatted input from the keyboard.
Syntax
scanf("format_specifier",&variable);
Example
int age;
scanf("%d",&age);
2. getchar()
The getchar() function reads a single character from the keyboard.
Syntax
character = getchar();
Example
char ch;
ch = getchar();
Output Functions
1. printf()
The printf() function displays formatted output on the screen.
Syntax
printf("format string",variables);
Example
printf("Age = %d",age);
2. putchar()
The putchar() function displays a single character on the screen.
Syntax
putchar(character);
Example
putchar(ch);
Common Format Specifiers
Format Specifier Data Type
%d Integer
%f Float
Format Specifier Data Type
%lf Double
%c Character
%s String
Advantages
Easy interaction between user and computer.
Supports different data types.
Simple to use.
Improves program flexibility.
Designing an Efficient Program in C
An efficient C program is one that produces correct results while using minimum memory,
execution time, and programming effort. Good program design makes software easier to
understand, test, and maintain.
Characteristics of an Efficient Program
1. Proper Problem Analysis
Understand the problem completely before writing the program.
2. Algorithm Design
Prepare a clear algorithm that describes the solution step by step.
3. Flowchart Preparation
Draw a flowchart to represent the logic of the program graphically.
4. Modular Programming
Divide a large program into smaller functions.
Advantages
Easier testing
Code reuse
Better readability
5. Meaningful Variable Names
Use descriptive names.
Example
totalMarks
studentName
Avoid
6. Proper Indentation
Indent statements properly to improve readability.
7. Comments
Use comments to explain important sections of the program.
Example
/* Calculate total marks */
8. Minimize Memory Usage
Declare only the required variables and use appropriate data types.
9. Error Checking
Validate user input and handle possible errors.
10. Program Testing
Test the program using different inputs before finalizing it.
Advantages
Faster execution
Less memory usage
Easy debugging
Easy maintenance
Improved readability
Global Variable
A global variable is a variable declared outside all functions, usually before the main() function.
It can be accessed by every function in the program.
Characteristics
Declared outside functions.
Accessible throughout the program.
Memory is allocated only once.
Exists until program execution ends.
Syntax
data_type variable_name;
Example Program
#include <stdio.h>
int count = 100;
void display()
printf("Count = %d\n", count);
int main()
printf("Count = %d\n", count);
display();
return 0;
Output
Count = 100
Count = 100
Advantages
Accessible by all functions.
Reduces repeated declarations.
Useful for sharing common data.
Static Variable
A static variable is a local variable declared using the static keyword. It retains its value between
function calls.
Characteristics
Declared using static.
Initialized only once.
Lifetime is the entire program.
Scope is limited to the function.
Syntax
static data_type variable_name;
Example Program
#include <stdio.h>
void demo()
static int count = 0;
count++;
printf("%d\n", count);
int main()
demo();
demo();
demo();
return 0;
Output
Advantages
Retains values between function calls.
Reduces unnecessary initialization.
Saves memory.
Local Variable
A local variable is a variable declared inside a function or block. It can only be accessed within
that function or block.
Characteristics
Declared inside a function.
Accessible only within the function.
Created when the function starts.
Destroyed when the function ends.
Example Program
#include <stdio.h>
int main()
int age = 20;
printf("Age = %d", age);
return 0;
Output
Age = 20
Advantages
Prevents accidental modification.
Reduces memory usage.
Improves program security.
External Variable
An external variable is a global variable accessed in another function or source file using the
extern keyword.
Characteristics
Declared using extern.
Shares global variables across functions or files.
Memory is allocated only once.
Syntax
extern int number;
Example Program
#include <stdio.h>
int number = 100;
void display()
extern int number;
printf("%d\n", number);
int main()
display();
return 0;
Output
100
Advantages
Allows sharing of global variables.
Reduces duplicate variable declarations.
Useful in multi-file programs.
Program Using Input and Output Statements
The following program demonstrates the use of printf(), scanf(), getchar(), and putchar().
#include <stdio.h>
int main()
{
int age;
char ch;
printf("Enter your age: ");
scanf("%d", &age);
getchar(); // Clears the newline character left by scanf
printf("Enter a character: ");
ch = getchar();
printf("\nAge = %d\n", age);
printf("Character entered: ");
putchar(ch);
return 0;
Sample Output
Enter your age: 20
Enter a character: A
Age = 20
Character entered: A
Explanation
printf() displays messages or values on the screen.
scanf() reads formatted input from the keyboard.
getchar() reads a single character from the keyboard.
putchar() displays a single character on the screen.
These functions are defined in the stdio.h header file and are the most commonly used
input/output functions in C programming.