Unit 2 L1
Unit 2 L1
which users and computers can communicate. Using this series of tutorials, one can learn C
Programming Language from the basics. Every topic in these tutorials is explained with clear
information and good examples. The content of all the topics is prepared by very
What is C?
C is a computer programming language used to design computer software and applications.
Why do we use C?
We use the C programming language to design computer software and applications.
Who invented C?
C Programming Language was invented in the year 1972 by Dennis Ritchie (Dennis
researcher along with Ken Thompson. He was born on 9th September 1941 and lived till
1. Turbo C
2. Turbo C++
3. GNU C
4. Code Blocks
5. Net Beans
stored in its memory. A computer can take data from the user through input devices
(Input), process the user given data (Processing), produces the result to the user through
output devices (Output)and stores data (Information) for future use. A Computer can be
defined as follows...
The Computer is an electronic device which operates under the control of instructions
stored in its memory and it takes the data from the user, a process that data gives
1. Hardware
2. Software
3. User
Here the user interacts with the software, and the software makes the computer hardware
touch and feel every hardware of the computer. All hardware components perform any task
1. Input Devices - These are the parts through which a user can give the data to the
computer.
2. Output Devices - These are the physical components of a computer through which
3. Storage Devices - These are the physical components of a computer in which the
4. Devices Drives - Using drives, user can read and write data on to the storage
Input Devices
Computer input devices are the physical components of the computer which are used to
give the data given by the user to the computer. Using input devices the user can give the
Example
Output Devices
Computer output devices are the physical components of the computer which are used to
give the computer result to the User. Using output devices, the user can see the computer-
generated result.
Example
Storage Devices
Computer storage devices are the physical components of the computer which are used to
Example
Device Drives
Computer Device drives are the physical components of the computer which are used to
Example
Computer Cables
In a Computer, various cables are used to make connections among the various hardware
Example
Other Devices
Other devices of the computer are shown below...
CPU (Processor)
SMPS
Motherboard
How does Computer work?
When a user wants to communicate with the computer, the user interacts with an
application. The application interacts with the operating system, and the operating system
makes hardware components to work according to the user given instructions. The
hardware components send the result back to the operating system, then the operating
system forwards the same to the application and the application shows the result to the
user.
By using input devices, the user interacts with the application and the application uses
output devices to show the result. All input and output devices work according to the
persons. That means when we want to make communication between two persons we need
a language through which persons can express their feelings. Similarly, when we want to
make communication between user and computer or between two or more computers we
need a language through which user can give information to the computer and vice versa.
When a user wants to give any instruction to the computer the user needs a specific
The user interacts with the computer using programs and that programs are created using
Computer languages are the languages through which the user can communicate
Every computer programming language contains a set of predefined words and a set of
Languages. In the earliest days of computers, only Binary Language was used to write
as Machine Language. The binary language contains only two symbols 1 & 0. All the
instructions of binary language are written in the form of binary numbers 1's & 0's. A
computer can directly understand the binary language. Machine language is also known as
As the CPU directly understands the binary language instructions, it does not require any
translator. CPU directly starts executing the binary language instructions and takes very less
time to execute the instructions as it does not require any translation. Low-level language is
Advantages
Low-level language instructions require very less time for their execution.
Disadvantages
In low-level language, there is more chance for errors and it is very difficult to find
symbols such as letters, digits and special characters. Assembly language is an example of
Binary code instructions in low-level language are replaced with mnemonics and operands
translator which takes assembly code as input and produces machine code as output. That
Advantages
low-level language.
Disadvantages
machine-dependent.
High-Level Language
A high-level language is a computer language which can be understood by the users. The
high-level language is very similar to human languages and has a set of grammar rules that
are used to make instructions more easily. Every high-level language has a set of predefined
words known as Keywords and a set of rules known as Syntax to create instructions. The
high-level language is easier to understand for the users but the computer can not
understand it. High-level language needs to be converted into the low-level language to
Languages like COBOL, FORTRAN, BASIC, C, C++, JAVA, etc., are examples of high-level
Advantages
The programs created using high-level language runs on different machines with
Disadvantages
The programming languages like C, C++, Java, etc., are written in High-level language
Middle-level language is not closer to both user and computer. We can consider it as
written using a high-level language like English. But, the computer cannot understand the
high-level language. It can understand only low-level language. So, the program written in
the high-level language needs to be converted into the low-level language to make it
understandable for the computer. This conversion is performed using either Interpreter or
Compiler.
Popular programming languages like C, C++, Java, etc., use the compiler to convert high-level
converts high-level language instructions into low-level language instructions. Generally, the
compiler performs two things, first it verifies the program errors, if errors are found, it
returns a list of errors otherwise it converts the complete code into the low-level language.
To create and execute C programs in the Windows Operating System, we need to install
Turbo C software. We use the following steps to create and execute C programs in Windows
OS…
code. Every c program source file is saved with .c extension, for example, Sample.c.
Whenever we press Alt + F9 the source file is submitted to the compiler. Compiler checks for
the errors, if there are any errors, it returns a list of errors, otherwise generates object code
in a file with name Sample.obj and submit it to the linker. The linker combines the code
from specified header file into an object file and generates executable file as Sample.exe.
Now, we need to run the executable file (Sample.exe). To run a program we press Ctrl + F9.
When we press Ctrl + F9 the executable file is submitted to the CPU. Then CPU performs the
task according to the instructions written in that program and place the result into
UserScreen.
Then we press Alt + F5 to open UserScreen and check the result of the program.
Important Points
The compiler converts complete program at a time from high-level language to low-
level language.
Input to the compiler is .c file and output from the compiler is .exe file, but it also
Overall Process
Type the program in C editor and save with .c extension (Press F2 to save).
If there are errors, correct the errors and recompile the program.
C Program Basics
C is a structured programming language. Every c program and its statements must be in a
This section is used to provide a small description of the program. The comment lines are
simply ignored by the compiler, that means they are not executed. In C, there are two types
of comments.
1. Single Line Comments: Single line comment begins with // symbol. We can write
2. Multiple Lines Comments: Multiple lines comment begins with /* symbol and ends
with */. We can write any number of multiple lines comments in a program.
In a C program, the comment lines are optional. Based on the requirement, we write
comments. All the comment lines in a C program just provide the guidelines to understand
Preprocessing commands are used to include header files and to define constants. We use
the #include statement to include the header file into our program. We use
a #define statement to define a constant. The preprocessing statements are used according
to the requirements. If we don't need any header file, then no need to write #include
statement. If we don't need any constant, then no need to write a #define statement.
The global declaration is used to define the global variables, which are common for all the
functions after its declaration. We also use the global declaration to declare functions. This
Every C program must write this statement. This statement (main) specifies the starting
point of the C program execution. Here, main is a user-defined method which tells the
compiler that this is the starting point of the program execution. Here, int is a data type of a
value that is going to return to the Operating System after completing the main method
The open brace indicates the beginning of the block which belongs to the main method. In C
In this section, we declare the variables and functions that are local to the function or block
in which they are declared. The variables which are declared in this section are valid only
In this section, we write the statements which perform tasks like reading data, displaying the
result, calculations, etc., All the statements in this section are written according to the
requirements.
The close brace indicates the end of the block which belongs to the main method. In C
This is the place where we implement the user-defined functions. The user-defined function
implementation can also be performed before the main method. In this case, the user-
defined function need not be declared. Directly it can be implemented, but it must be
before the main method. In a program, we can define as many user-defined functions as we
want. Every user-defined function needs a function call to execute its statements.
2. Every C program must contain exactly one main method (Starting point of the
program execution).
5. For every open brace ({), there must be respective closing brace (}).
#include <stdio.h>
// macro definition
#define LIMIT 5
int main()
{
for (int i = 0; i < LIMIT; i++) {
printf("%d \n",i);
}
return 0;
}
n the above program, when the compiler executes the word LIMIT it replaces
it with 5. The word ‘LIMIT’ in the macro definition is called a macro template
and ‘5’ is macro expansion.
#include <stdio.h>
return 0;
}
File Inclusion:
This type of preprocessor directive tells the compiler to include a file in
the source code program. There are two types of files which can be
included by the user in the program:
Header File or Standard files: These files contains definition of pre-
defined functions like printf(), scanf() etc. These files must be included for
working with these functions. Different function are declared in different
header files. For example standard I/O functions are in ‘iostream’ file
whereas functions which perform string operations are in ‘string’ file.
Syntax:
#include< file_name >
where file_name is the name of file to be included. The ‘<‘ and ‘>’ brackets
tells the compiler to look for the file in standard directory.
Other directives: Apart from the above directives there are two
more directives which are not commonly used.