Module 1-2
Module 1-2
Module 1
Introduction to C
Syllabus: Introduction to C:
Introduction to computers,
input and output devices,
designing efficient programs.
Introduction to C,
Structure of C program,
Files used in a C program,
Compilers,
Compiling and executing C programs,
variables, constants,
Input/output statements in C,
----------------------------------------------------------------------------------------------------------------
Introduction to Computers
A computer is an electronic device which accepts data as input, performs operations on the data based
on the instructions stored in the memory and produces output. A computer is made up of two
components: i) Hardware and ii) software.
Characteristics of Computers
1. Speed: Computers are capable of executing billions of instructions per second, providing fast
processing and data manipulation.
2. Accuracy: Computers perform calculations and operations with high accuracy, minimizing human
errors and providing precise results
3. Automation: Computers can automate repetitive tasks and processes, improving efficiency and
productivity in various industries and sectors.
4. Diligence: Unlike humans, computers never get tired of repetitive task. It can continually work for
hours without creating errors with same accuracy.
1
2022 Scheme Module 1 Introduction to C
5. Versatile: Computers have the quality of being flexible that can handle a wide range of tasks and
applications, including word processing, data analysis, multimedia editing, internet browsing,
gaming, and much more.
6. Storage Capacity: Modern computers can store vast amounts of data, ranging from gigabytes to
terabytes or even petabytes, in various storage devices such as hard drives, solid-state drives
(SSDs), and cloud storage.
7. Multitasking: Computers have the ability to perform multiple tasks simultaneously, thanks to
multitasking operating systems. This allows users to run several applications and processes
concurrently.
8. Connectivity: Computers can connect to networks, enabling communication, data sharing, and
access to online resources such as the internet, intranets, and cloud services.
9. Programmability: Computers are programmable machines, allowing users to write and execute
software instructions that define desired operations and tasks.
10. Scalability: Computers can be scaled up or down based on computing needs, ranging from
personal computers to supercomputers that offer immense processing power.
11. Reliability: Computers are designed to operate reliably, with components and systems built to
withstand environmental conditions and maintain consistent performance over time.
12. Compactness: Computers have evolved from large mainframe systems to smaller, more compact
form factors such as laptops, tablets, and smartphones, providing portability and convenience.
13. User Interface: Computers have user interfaces that allow users to interact with the system,
including graphical user interfaces (GUIs), touchscreens, keyboards, mice, and voice recognition.
14. Security: Computers incorporate various security measures, such as encryption, firewalls,
antivirus software, and user authentication, to protect data and systems from unauthorized access
and threats.
The stored program concept refers to the idea that instructions and data can be stored in computer
memory and treated equally as data, allowing the computer to execute a sequence of instructions stored
in memory.
Q: What are the key characteristics features of stored program concept? 4 to 6 marks
2
2022 Scheme Module 1 Introduction to C
Stored program computers can be broadly classified into two main types based on their architecture:
Harvard architecture
In this type of architecture there was separate memory to store data and instructions as shown in Fig
1.2b. Since there are separate memories execution becomes faster.
3
2022 Scheme Module 1 Introduction to C
History of Computers
Abacus (300 BC): The abacus, a simple mechanical device for performing arithmetic calculations, is
invented in ancient Mesopotamia and China.
Slide Rule (1600s): The slide rule, a mechanical tool for mathematical calculations, is invented,
allowing for more advanced computations.
Mechanical Calculators (17th-19th century): Inventors like Blaise Pascal and Gottfried Wilhelm
Leibniz develop mechanical calculators capable of performing arithmetic operations.
Analytical Engine (1837): Charles Babbage conceives the concept of the Analytical Engine, a
programmable mechanical computer that laid the foundation for modern computing.
Punched Card System (1890s): Herman Hollerith develops a punched card system for processing and
tabulating data, which finds use in early computing machines.
Vacuum Tube Computers (1940s): Electronic computers like ENIAC and UNIVAC use vacuum tubes
for computation, marking a shift from mechanical to electronic computing.
Transistor Computers (1950s): The invention of transistors replaces vacuum tubes, leading to smaller,
more reliable, and faster computers.
Integrated Circuits (1960s): Integrated circuits, or microchips, are developed, enabling the integration
of multiple transistors onto a single chip and further miniaturization of computers.
Personal Computers (1970s-1980s): Companies like Apple, IBM, and Microsoft introduce personal
computers, making computing accessible to individuals and businesses.
Internet and World Wide Web (1990s): The internet and the World Wide Web revolutionize
communication and information sharing, transforming computers into powerful tools for connectivity
and access to vast amounts of data.
Mobile Computing (2000s): The proliferation of smartphones and tablets brings computing power to
people's fingertips, enabling mobile computing and access to a wide range of applications and services.
4
2022 Scheme Module 1 Introduction to C
Cloud Computing and AI (2010s): The emergence of cloud computing allows for the storage,
processing, and sharing of data and applications over the internet. Artificial intelligence and machine
learning technologies become prominent, powering advancements in areas such as voice recognition,
image processing, and data analysis.
Q: List the classifications of computer and explain each of them briefly. 6 marks
Classification of Computers
Computers can be classified into several categories based on their size, purpose, and architecture. Here
are the commonly recognized classifications of computers:
Very expensive and need very large clean room with air conditioning
Similar to supercomputers mainframes can also support multiple processors
Mainframes are commonly used in enterprise-level applications such as financial
transactions, airline reservations, and database management.
Use two types of terminals namely dumb terminals and intelligent terminals
Dumb terminal: consists only monitor and a keyboard/mouse and do not have their own
CPU and memory.
Intelligent terminal: have their own processor and can perform processing operations but
do not have own storage space like dumb terminal.
Minicomputers: Intermediate-level computers that offer more processing power and storage
capacity than microcomputers (personal computers) but are smaller and less powerful than
mainframes introduced in 1960s.
Also known as midrange computers
Capabilities fall between mainframe and personal computers
They find applications in scientific research, small businesses, and as servers.
They can be used as servers in networked environment
Personal Computers (PCs): Also known as microcomputers, personal computers are designed for
individual use.
They come in desktop and laptop form factors and are widely used for general computing
tasks, gaming, internet browsing, and office applications.
5
2022 Scheme Module 1 Introduction to C
Applications of Computers
Communication: Computers facilitate communication through email, instant messaging, and
social media.
Education: Computers are used for e-learning, online courses, and accessing educational
resources.
Business and Finance: Computers manage business operations, accounting, and financial
transactions.
Entertainment and Media: Computers power video games, streaming services, and digital
content creation.
Research and Science: Computers aid in scientific research, simulations, data analysis, and
modeling.
Healthcare: Computers support electronic medical records and medical imaging.
Transportation: Computers control systems in vehicles, traffic management, and navigation.
Personal Productivity: Computers enhance productivity through word processing,
spreadsheets, and organizational tools.
Manufacturing and Automation: Computers control manufacturing processes, robotics, and
automation systems.
Security: Computers are used for cybersecurity, encryption, and surveillance systems.
Q: Explain the organization of a computer with the help of a neat block 7 marks
diagram.
The basic organization of a computer involves various components that work together to perform tasks
and execute instructions. Fig 1.3 shows the interaction between the different units of a computer system.
6
2022 Scheme Module 1 Introduction to C
1. Input: It is a process of entering data and instructions (program) into the computer.
These data and programs are entered to computer by various input devices such as
keyboard, mouse, scanner, and trackball.
The input devices are responsible to convert the input data into binary codes, as the
computer only understands binary language.
2. Storage: It is the process of saving the data and programs permanently in computer for processing
purpose.
The storage space of computer not only stores the data and instructions but also the
intermediate results and final results of processing.
A computer has 2 types of storage areas: i) Primary storage and ii) secondary storage
Primary storage:
Primary storage refers to the computer's internal memory that is directly accessible by the CPU.
It is also known as main memory or primary memory.
Primary storage is used to temporarily store data and instructions that are actively being
processed by the CPU.
It is much faster than secondary storage (e.g., hard drives or solid-state drives) and allows for
quick retrieval and execution of data.
The two main types of primary storage are Random Access Memory (RAM) and Cache
memory.
RAM provides temporary storage for data and instructions during program execution.
Cache memory is a smaller and faster memory used to store frequently accessed data for quicker
access by the CPU.
Primary storage is volatile, meaning that its contents are lost when the computer is powered off
or restarted.
Secondary storage:
7
2022 Scheme Module 1 Introduction to C
Secondary memory is non-volatile, meaning it retains data even when the computer is powered
off or restarted.
3. Processing: it is process of performing operations on the data as per the instructions of program is
called processing.
Data and instructions are taken from main memory and transferred to ALU, that
performs all the calculations
The intermediate results may be stored in the main memory as they are required again.
After the process completion, the result is stored in main memory
Hence this way the data move from main memory to ALU multiple times before the
processing is over.
4. Output: it is process of giving the data processed to outside world. It refers to the result or
information produced by a program
The results are given through output devices such as monitor, and printer.
The output data is also in binary format and hence it is responsibility of output devices to
convert binary data to human readable language.
5. Control: The control unit is a component of the central processing unit (CPU) in a computer
system.
It coordinates and controls the operations of the CPU and other hardware components.
It fetches instructions from memory and decodes them to determine the required operation.
It executes instructions by activating the necessary hardware components, such as the ALU
and registers.
It manages the timing and control signals to synchronize activities within the CPU and the
entire system.
It handles interrupt requests and transfers control to the appropriate interrupt service
routine.
It manages the flow of data within the CPU and between the CPU and other components.
It interfaces with external devices, such as input/output devices and storage devices.
It ensures proper data exchange, synchronization, and communication with external devices
Motherboard
The motherboard, also known as the mainboard or the parent board, is the primary component of a
computer. It is used to connect all the components of the computer. The motherboard is a printed circuit
that has connectors for expansion cards, memory modules, the processor, etc.
Characteristics of Motherboard
A motherboard can be classified depending on the following characteristics:
Form factor: refers to the motherboard‘s geometry, dimensions, arrangement, and electrical
requirements.
8
2022 Scheme Module 1 Introduction to C
Chipset:
Input–Output connectors
An input device is a peripheral or hardware component that allows users to provide data,
commands, or instructions to a computer. Fig 1.4 shows the classifications of input devices.
1. Keyboard: A keyboard is a common input device that allows users to enter text,
numbers, and commands by pressing keys. It typically consists of alphanumeric keys,
function keys, and special keys.
Advantages: Easy to use, good for typing text, and widely available.
Disadvantages: Not suitable for graphical input, can cause hand strain with prolonged
use.
2. Mouse: A mouse is a pointing device that enables users to control the cursor on the
screen. It typically has buttons and a scrolling wheel, allowing users to click, double-
click, right-click, and scroll through content.
Advantages: Makes it easy to point and click, good for navigating and selecting items on
the screen.
Disadvantages: Needs a flat surface to work on, not great for drawing or precise
movements.
9
2022 Scheme Module 1 Introduction to C
5. Graphics Tablet: Also known as a digitizing tablet or pen tablet, a graphics tablet
consists of a flat surface and a stylus or pen. Artists and designers use it to draw, sketch,
or write directly on the tablet, with the input being transferred to the computer screen.
Advantages: Offers precise drawing and pressure sensitivity for digital art and design.
Disadvantages: Requires additional space, may take time to get used to for new users.
6. Joystick: A joystick is a lever-like input device primarily used for gaming or controlling
machinery. It allows users to move in multiple directions and control objects on the
screen by manipulating the joystick's position.
Advantages: Precise control, versatility, immersive experience, ergonomic design.
Disadvantages: Limited functionality, learning curve, space requirements, not ideal for
text input.
7. Scanner: A scanner is used to convert physical documents or images into digital form.
It captures the content optically and sends it to a computer for further processing or
storage.
Advantages: Helps to digitize physical documents for easy storage and sharing.
Disadvantages: Requires a separate device, not suitable for scanning large or delicate
items
10
2022 Scheme Module 1 Introduction to C
9. Webcam: A webcam is a camera device that captures video and audio input. It is
commonly used for video conferencing, live streaming, video chatting, or capturing
video footage.
Advantages: Allows visual communication, good for video chats and creating video
content.
Disadvantages: Affected by lighting conditions, video quality may not be very high
10. Barcode Reader: Barcode readers, such as barcode scanners or barcode readers, are used
to read barcodes found on products or documents. They quickly scan and translate the
barcode information into digital data that can be processed by a computer.
Advantages: Quick and accurate scanning for inventory management and data entry.
Disadvantages: Limited to reading barcodes, requires specific barcode types.
Output devices are hardware components that display or present information or results from a computer
in various forms such as text, images, sound, or physical output. The fig 1.5 shows the classification of
output devices.
Output devices accept digital data from the computer and convert them into human understandable
language.
Soft copy refers specifically to digital or electronic representations of information. Therefore, in the
context of soft copy devices, we can focus on the monitor and projector.
Monitor/Display: A monitor or display screen is a soft copy device that presents visual information
in digital form.
It allows users to view and interact with digital content such as text, images, videos, and
graphical interfaces.
Monitors come in various sizes, resolutions, and technologies (e.g., LCD, LED, OLED),
providing clear and vibrant visual output.
11
2022 Scheme Module 1 Introduction to C
Projector: A projector is another soft copy device that displays digital content onto a larger screen or
surface.
It works by projecting images, videos, or presentations onto a wall, screen, or other suitable
surfaces.
Projectors are commonly used in classrooms, conference rooms, and theatres to share and
present digital information to a larger audience.
hard copy devices are used to create physical copies or prints of digital documents or images. The two
primary types of hard copy devices are printers and plotters:
Printers:
Inkjet Printer: Sprays tiny droplets of ink onto paper to create text and images. It is commonly used
for everyday printing tasks and can produce high-quality colour prints.
Laser Printer: Uses a laser beam and toner to create prints. Laser printers are known for their fast-
printing speeds and high-quality text and graphics.
Dot Matrix Printer: Utilizes a matrix of pins striking an inked ribbon to produce characters and images
on paper. Dot matrix printers are commonly used for printing receipts, invoices, and multipart forms.
Thermal Printer: Uses heat to create prints on specially coated thermal paper. They are commonly
used in applications such as receipts, tickets, and labels.
Plotters:
Pen Plotter: Utilizes a pen or multiple pens to draw continuous lines on paper. Pen plotters are
commonly used in engineering, architecture, and design for creating technical drawings and
large-scale illustrations.
Electrostatic Plotter: Uses an electrostatic process to draw lines on paper. These plotters can
produce high-quality prints and are used for professional graphical applications
Q: With the neat diagram, explain the working of CRT and LCD monitor 8 marks
CRT monitors looks much like traditional televisions and are very huge in size. They are
cheaper but occupy more space on desk. Fig 1.6 shows the internal structure of CRT, the
working of CRT is as follows:
12
2022 Scheme Module 1 Introduction to C
Advantages: Excellent picture quality, fast response times, wide viewing angles, durability,
affordability.
Disadvantages: Bulky and heavy, high-power consumption, electromagnetic radiation,
limited resolutions, flickering and eye strain.
LCD (Liquid Crystal Display) monitors use liquid crystals, a unique state of matter with
properties of both liquids and solids.
The monitor has a backlight system (fluorescent lamps or LEDs) that provides
illumination to the display.
The LCD panel consists of a grid of tiny pixels, each containing red, green, and blue
(RGB) sub-pixels.
Two polarizing filters sandwich the liquid crystal molecules, controlling the passage of
light.
When no electric current is applied, the liquid crystal molecules twist and block light,
resulting in a dark pixel.
Thin-film transistors (TFTs) address individual pixels, applying an electric current to
control the liquid crystal orientation.
By adjusting the electric current to RGB sub-pixels, the intensity of light passing through
creates a wide range of colors and shades.
13
2022 Scheme Module 1 Introduction to C
The liquid crystal molecules act as a light valve, selectively allowing or blocking the
backlight to form the desired image.
The Software Development Life Cycle (SDLC) is a process followed in software development
to plan, design, build, test, and deploy software systems. It consists of several phases that guide
the development process from start to finish. The typical phases of the SDLC cycle are as
follows:
Requirement Analysis:
In this phase, the requirements for the software system are gathered and analyzed.
The project scope, goals, and constraints are defined.
Business needs and user requirements are identified.
System Design:
In this phase, the system architecture and design are defined based on the requirements.
The software components, modules, and interfaces are designed.
System flowcharts, data flow diagrams, and other design artifacts are created.
Implementation:
In this phase, the actual coding and development of the software system take place.
The design is translated into programming code using the chosen programming
language and tools.
Developers follow coding standards and best practices to ensure quality and
maintainability.
Testing:
In this phase, the software is thoroughly tested to ensure it meets the requirements and
functions as intended.
Different types of testing, such as unit testing, integration testing, and system testing,
are performed.
Bugs, errors, and issues are identified, reported, and fixed.
Deployment:
14
2022 Scheme Module 1 Introduction to C
User training and documentation may also be part of the deployment process.
Maintenance:
ALGORITHMS
An algorithm can be defined as ―a step-by-step procedure that provides solution to a given problem‖.
3. Input. An algorithm has zero or more inputs, i.e., quantities which are given to it
initially before the algorithm begins.
4. Output. An algorithm has one or more outputs i.e., quantities which have a specified
relation to the inputs.
5. Effectiveness. An algorithm is also generally expected to be effective. This means that
all the operations to be performed in the algorithm must be sufficiently basic that they
can in principle be done exactly and in a finite length of time.
Example 1:
Algorithm for Addition of Two Numbers
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
15
2022 Scheme Module 1 Introduction to C
Step 6: Stop
Example 2:
Algorithm to check if a number is even or odd
Step 1. Start
Step 2. Declare an integer variable "num" to store the input number
Step 3. Read the input number from the user and store it in "num"
Step 4. Check if the remainder of "num" divided by 2 is equal to 0
- If the remainder is 0, go to step 5 (number is even)
- If the remainder is not 0, go to step 6 (number is odd)
Step 5. Print "num is even"
Step 6. Print "num is odd"
Step 7. Stop
Example 3:
Algorithm to find the sum and average of three numbers
Step 1. Start
Step 2. Declare three variables "num1", "num2", "num3" to store the three numbers
Declare variables "sum" and "avg" to store the sum and average respectively
Step 3. Read the three numbers from the user and store them in "num1", "num2", and "num3"
Step 4. Calculate the sum of the three numbers using the formula:
Step 5. Calculate the average of the three numbers using the formula:
avg = sum / 3
Step 7. Stop
Advantages of Algorithms:
16
2022 Scheme Module 1 Introduction to C
Disadvantages of Algorithms
Algorithms are Time consuming.
Difficult to show Branching and Looping in Algorithms.
FLOW CHART
They help us visualize complex processes, or make explicit the structure of problems
and tasks. A flowchart can also be used to define a process or project to be implemented.
17
2022 Scheme Module 1 Introduction to C
Example 1:
Flowchart for Addition of Two Numbers
18
2022 Scheme Module 1 Introduction to C
Example 2:
Flowchart to find if the number is even or odd
Example 3:
Algorithm and Flowchart to find sum of 5 numbers
19
2022 Scheme Module 1 Introduction to C
Write a program the converts the input Celsius degree into its equivalent
Fahrenheit degree. Use the formula: F = (9/5) *C+32.
20
2022 Scheme Module 1 Introduction to C
Write a program that exchanges the value of two variables: x and y. The
output must be: the value of variable y will become the value of variable x,
and vice versa.
21
2022 Scheme Module 1 Introduction to C
Q6. Design a program to find the circumference of a circle. Use the formula:
C=2πr, where π is approximately equivalent 3.1416.
Algorithm Flowchart
Algorithm is step by step procedure to Flowchart is a diagram created by different shapes
solve the problem. to show the flow of data.
Algorithm does not follow any rules. Flowchart follows rules to be constructed.
Algorithm is the pseudo code for the Flowchart is just graphical representation of that
program. logic.
Pseudo code
22
2022 Scheme Module 1 Introduction to C
Benefits: Pseudocode allows algorithm designers to focus on logic and structure, separate from
language-specific details. It aids understanding, validation, and translation into different programming
languages.
Example
Start
Read num1
Read num2
Set sum = num1 + num2
Print sum
Stop
Types of Errors
Q: What are the various types of errors in C program? Explain briefly 4 to 6 marks
Runtime Errors:
Compile-time Errors:
Compile-time errors refer to errors that occur during the compilation process.
These errors are detected by the compiler and prevent the program from being successfully
compiled.
Example: syntax errors, using undeclared variables, or referencing undefined functions.
Linker Errors:
Linker errors occur during the linking phase of the compilation process.
These errors are related to unresolved symbols or references to functions or variables that are
not found in the program or libraries being linked.
Example: referencing a function that has not been defined or including a library that is missing.
23
2022 Scheme Module 1 Introduction to C
Logical Errors:
Introduction to C
Programming Language: A language which is used for communication between a human
being and a computer. Human beings communicate with the computer by writing program using a
programming language. Example: C, C++, Java etc.
Program: a program is a set of instructions for solving a particular problem or let the computer perform
operations for completing a task. Programs are written using programming language.
Programming: The process of writing program to instruct the computer do something is known as
programming. Programming is done using programming language.
Programmer: a person who can write programs by using any one of the programming languages is
known as a programmer. A programmer has the knowledge of using at least one programming language
for writing programs.
What is C Program?
Features of C Program
1. Middle level Language: C language provides the benefits of high level and low-level languages
both. C allows manipulation of hardware just like low level language and allows high user end
functionalities like high-level languages.
2. Simple: C language is simple and easy to learn. The syntax of C is simple and gives flexibility
to the programmer with its wide variety of in-built functions and data types.
3. Portable: C is a machine independent language, which means a C program written one machine
can run on another machine without requiring a code change.
4. Fast: C is a compiler-based language. C is power packed with several features, but it is not
bloated like some other programming languages, it supports only useful features which makes
the compilation of C file fast.
24
2022 Scheme Module 1 Introduction to C
5. Extensible: C program supports code modifications and addition of new code to the already
existing programs; this makes C language extensible. It becomes easier to add new
functionalities to the existing C programs.
6. Rich libraries: C libraries are full of useful in-built functions. These functions can be used
frequently to perform some repeated tasks. As a C programmer, you won‘t need to write same
code again and again for some trivial repeated tasks, rather you can use in-built functions to
perform the task.
7. Dynamic memory allocation: Dynamic memory allocation means, optimizing and allocating
the memory during runtime. C language supports dynamic memory allocation, which supports
function like free() to free the un-utilized memory space and other memory management
functions such as malloc(), calloc() etc.
8. Reusability: Function is a block of code that is used to perform a specific task. Along with the
in-built predefined functions, C language allows you to create you own functions that you can
create for a reoccurring task. This improves the reusability of the code and avoids the unnecessary
writing of the same code again and again.
9. Supports pointers: C language supports pointers. The pointer in C allows you to directly interact
with the memory.
10. Recursion: C supports recursion. Recursion means a function calls itself. Recursion provides the
features of code reusability and backtracking.
Structure of C program
Each C Program is consists of 6 main sections as shown in Fig , these sections are named as:
1. Documentation Section
2. Preprocessor Directives section
3. Definition Section
4. Global Declaration Section
5. Main Function Section
6. Subprogram Section.
25
2022 Scheme Module 1 Introduction to C
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. The documentation section helps
anyone to get an overview of the program. Comments may appear anywhere within a program. The text
between /* And */ appears as a comment in C as shown in the example.
for Example: /* This is a comment */
Preprocessor Directive section/Link Section: The Link section provides instructions to the compiler
to link functions from the system library such as using the #include directive.
Example: #directive
Definition Section: All the symbolic constants are written in the definition section. Macros are known
as symbolic constant (macro is a process where an identifier is replaced by a predefined string or value
in a program) such as using the #define directive.
Example: #define a=10
Global declaration section: Global Declaration Section contains the global declaration of user-defined
functions and Variables. 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.
26
2022 Scheme Module 1 Introduction to C
Main() Function Section: It is necessary to have one main() function section in every c program. This
section contains two parts, the Declaration Part And Executable Part.
The declaration part declares all the variables that are used in the executable part.
Example: main()
OR
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 ordeí.
Example:
27
2022 Scheme Module 1 Introduction to C
Some of the most common files used in C program as shown in Fig are listed as follows:
1. Source file
2. Header file
3. Object file
4. Executable file
Header Files
1. At times the programmer may want to use the same subroutines for different programs. To do this,
he would just compile the code of the subroutine once and link to the resulting object file in any
file in which the functionalities of this subroutine are required.
2. At times the programmer may want to change or add the subroutines and reflect those changes in
all the programs. For doing this, he will have to only change the source file for the subroutines,
recompile the source code and then recompile and re-link the program.
This tells us that including a header file will make it easier at all levels of the program. If we need
to modify anything, then changes are made only in the subroutines after which all the changes will
be reflected.
28
2022 Scheme Module 1 Introduction to C
The header files are added at the start of the source code so that they can be used by more than one
function of the same file.
Object files
They have an extension of ‗.o‘
They are the files that are generated by the compiler as the source code file is processed.
These files generally contain the binary code of the function definitions.
The object file is used by the linker for producing an executable file for combining the object files
together.
Executable file
They have an '.exe' extension.
This file is generated by the linker.
Various object files are linked by the linker for producing a binary file which will be executed
directly.
Compilers
29
2022 Scheme Module 1 Introduction to C
Pre-processing: All the statements starting with the # symbol in a C program is processed by the pre-
processor, and it converts our program file into an intermediate file with no # statements. In this section
our source code is attached to the preprocessor file. Different types of header files are used like the
studio.h, math.h, etc. Our C source code is attached to these types of files and the final C Source
generates. (some of the preprocessor directives are #include, #define). After generating the
preprocessed C source code, the C source code is sent to the compiler section.
Compiler: The preprocessed source code moves to the compiler, and an assembly-level code is
generated by the compiler after the compilation of the whole C source code program. All the different
files which have the C program must be saved with the .c extension. For the compiler to understand
whether the program file is a C program file or not, it is necessary to have the '.c' extension. Suppose
there is a program file named as first.c, The file first.c will be the source file which will consist of the
C source code of the program. Now, when the file is compiled, the first thing the C compiler does is to
search for any error. If there is no error, the C compiler will report for no error, after that the compiler
will store the file as a .obj file of the same name, which is termed as the object file. So, by this process,
the compiler will create the first.obj. Although this .obj file will not be executable. After the
compilation, the process is continued by the assembler section.
Assembler: This part usually generates the Object code, after taking the assembly-level code from the
compiler. This object code is quite similar to the machine code or the set of binary digits. After this
assembler part, The Linker continues the process, producing an executable.exe file at the end.
Linker: Before getting started with this, we should know that the library functions are a part of the C
software but not of any C program. Hence, the compiler has no idea about the working of the function,
whether it is a printf function or scanf function. The information for each of these functions is kept in
the corresponding library, which the compiler ought to be able to connect. The linker does this task. So,
when the #include is written, it includes the studio.h library, which is basically used for giving access
to the Standard Output and Input. The basic goal of the linker is to link the object file to the library
functions so that the programme may be run as an executable file (.exe). In this Linker process, the
first.exe file will be created, and this file is in an executable format. After this process, the next step is
the loader process.
30
2022 Scheme Module 1 Introduction to C
variables, constants,
Variables in C
Q: What are variables? How are they declared and initialized with an 4 to 6 marks
example.
In the programs, generally we need to store values in the memory and perform operations on those
values. This can be achieved in C, by the concept of variables.
A variable is a placeholder for holding a value in the main memory (RAM). As the name implies, the
value in the variable can change at any point of execution of the program. For using variables in our
programs, there are essentially two steps:
31
2022 Scheme Module 1 Introduction to C
Before using a variable in the program, we have to declare the variable. The syntax for declaring a
variable in a program is as shown below:
There are certain rules that must be followed while writing the variable name. They are as follows:
After declaring the variable, we can assign a value to the variable. This process of assigning a value to
the variable is known as initialization. Syntax for initializing a variable is as shown below:
The value we assign to the variable depends on the data type of the
Example:
The declaration and initialization can be combined into a single line as shown below:
Example:
A data type specifies the type of value that we use in our programs. A data type is generally
specified when declaring variables, arrays, functions etc.
In ANSI C, the data types are divided into three categories. They are:
1) Primitive or Fundamental data types
The most fundamental data types that every C compiler supports are:
32
2022 Scheme Module 1 Introduction to C
Other compilers support the extended versions of these fundamental data types like:
The signed data types are used for storing both positive and negative values whereas the unsigned data
types are used to store only positive values. The memory capacity of these data types are based on the
hardware.
The memory capacity of all the fundamental data types on a 16-bit machine is as shown
below
Constants
Q: What are constants? How are constants categorized? Explain briefly 4 to 6 marks
33
2022 Scheme Module 1 Introduction to C
Integer Constants
A hexadecimal integer consists of numbers from 0 to 9 and 10, 11, 12, 13, 14 and 15 are
represented as A, B, C, D, E and F.
Hexadecimal numbers are always preceded by 0x or 0X.
Some valid examples of hexadecimal integer constants are: 0x54, 0X23, +0x1F, -0X56 etc.
Real Constants
Integer numbers are not sufficient to represent quantities that vary continuously, such as
distances, height, prices etc.
These quantities are represented by numbers containing fractional parts like 25.234.
Such numbers are called as real or floating point constants.
Some valid examples of real constants are: 0.067, -12.5, +4.67, .87, 121. Etc.
A real number may also be expressed in exponential (scientific) notation.
For example, 45.2344 can be written 0.452344e2. (102)
The exponential notation is: mantissa e exponent. The mantissa is either a real number
expressed in decimal notation or an integer with an optional + or – sign.
The exponent is an integer number with an optional + or – sign.
Some valid examples of real constants in exponential notation are: 0.34e2 (102), 13e-
2 -1
2 (10- ), -1.23e-1(10 ) etc.
34
2022 Scheme Module 1 Introduction to C
A sequence of characters that are enclosed between double quotes is known as a string literal
or string constant.
The characters in a string literal can be either letters, digits or special symbols or white spaces.
The string literal does not have an associated integer value like the character constants.
Some valid examples of string constants are: ―hai‖, ―hEllO‖, ―hi5‖, ―Wel come‖ etc.
Input/output statements in C
The printf and scanf statements are fundamental input/output functions in C that allow you to interact
with the user, display formatted output, and read input according to specified formats.
printf Statement: The printf statement is used to display formatted output on the console or terminal.
"format string": A string that specifies the layout and type of the output to be displayed.
argument1, argument2, ... (optional): Values or variables to be inserted into the format string.
Explanation: The printf function formats and prints text, variables, and expressions based on the
specified format string. The format string can include placeholders marked by format specifiers (such
as %d for integers or %f for floating-point numbers), which will be replaced by the corresponding
values or variables.
Example:
35
2022 Scheme Module 1 Introduction to C
scanf Statement: The scanf statement is used to read input from the user or input stream based on
specified format specifiers.
&variable1, &variable2, ... (optional): Memory addresses of variables where the entered values will be
stored.
Explanation: The scanf function reads and assigns values entered by the user to the specified variables
based on the format string. The format string contains format specifiers (such as %d for integers or %f
for floating-point numbers) that match the types of the variables to be read.
Example:
Output
OUTPUT:
36