Introduction to C Programming
C Programming Language is a very popular computer programming language through
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
experienced and expert faculty.
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
MacAlistair Ritchie). He was an American Computer Scientist worked at Bell Labs as a
researcher along with Ken Thompson. He was born on 9th September 1941 and lived till
12th October 2011. He is said to be the Father of C.
Software used to create and execute a C Program
Following are the applications and software used to create and execute C programs.
1. Turbo C
2. Turbo C++
3. GNU C
4. Code Blocks
5. Net Beans
Introduction to Computer Systems
What is a Computer?
The computer is an electronic device which operates under the control of instructions
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
the result and stores the result for future use.
What does a computer consist of?
Every computer mainly consists of three things and those are...
1. Hardware
2. Software
3. User
Here the user interacts with the software, and the software makes the computer hardware
parts to work for the user.
What is Computer Hardware?
All physical components of the computer are called as computer hardware. A user can see,
touch and feel every hardware of the computer. All hardware components perform any task
based on the instructions given by the computer software.
The computer hardware is the physical part of a computer.
The computer hardware components are as follows...
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
the computer gives the result to the user.
3. Storage Devices - These are the physical components of a computer in which the
data can be stored.
4. Devices Drives - Using drives, user can read and write data on to the storage
devices like CD, floppy, etc.,
5. Cables - Various cables (Wires) are used to make connections in a computer
6. Other Devices - Other than the above hardware components, a computer also
contains components like Motherboard, CPU (Processor), SMPS, Fans, etc.,
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
data to the computer.
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
store data internally or externally.
Example
Device Drives
Computer Device drives are the physical components of the computer which are used to
read and write data on the storage devices.
Example
Computer Cables
In a Computer, various cables are used to make connections among the various hardware
components of the computer.
Example
Other Devices
Other devices of the computer are shown below...
CPU (Processor)
SMPS
CPU Fan (Heat Sink)
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
instructions given by the operating system.
The working process of a computer is shown in the following figure.
Computer Languages
What is Computer Language?
Generally, we use languages like English, Hindi, etc., to make communication between two
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
language and that language is known as a computer language.
The user interacts with the computer using programs and that programs are created using
computer programming languages like C, C++, Java, etc.,
Computer languages are the languages through which the user can communicate
with the computer by writing program instructions.
Every computer programming language contains a set of predefined words and a set of
rules (syntax) that are used to create instructions of a program.
Computer Languages Classification
Over the years, computer languages have been evolved from Low-Level to High-Level
Languages. In the earliest days of computers, only Binary Language was used to write
programs. The computer languages are classified as follows...
Low-Level Language (Machine Language)
Low-Level language is the only language which can be understood by the computer. Binary
Language is an example of a low-level language. Low-level language is also known
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
the Machine Code.
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
considered as the First Generation Language (1GL).
Advantages
A computer can easily understand the low-level language.
Low-level language instructions are executed directly without any translation.
Low-level language instructions require very less time for their execution.
Disadvantages
Low-level language instructions are very difficult to use and understand.
Low-level language instructions are machine-dependent, that means a program
written for a particular machine does not execute on another machine.
In low-level language, there is more chance for errors and it is very difficult to find
errors, debug and modify.
Middle-Level Language (Assembly Language)
Middle-level language is a computer language in which the instructions are created using
symbols such as letters, digits and special characters. Assembly language is an example of
middle-level language. In assembly language, we use predefined words called mnemonics.
Binary code instructions in low-level language are replaced with mnemonics and operands
in middle-level language. But the computer cannot understand mnemonics, so we use a
translator called Assembler to translate mnemonics into binary language. Assembler is a
translator which takes assembly code as input and produces machine code as output. That
means, the computer cannot understand middle-level language, so it needs to be translated
into a low-level language to make it understandable by the computer. Assembler is used to
translate middle-level language into low-level language.
Advantages
Writing instructions in a middle-level language is easier than writing instructions in a
low-level language.
Middle-level language is more readable compared to low-level language.
Easy to understand, find errors and modify.
Disadvantages
Middle-level language is specific to a particular machine architecture, that means it is
machine-dependent.
Middle-level language needs to be translated into low-level language.
Middle-level language executes slower compared to low-level language.
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
make it understandable by the computer. We use Compiler or interpreter to convert high-
level language to low-level language.
Languages like COBOL, FORTRAN, BASIC, C, C++, JAVA, etc., are examples of high-level
languages. All these programming languages use human-understandable language like
English to write program instructions. These instructions are converted to low-level
language by the compiler so that it can be understood by the computer.
Advantages
Writing instructions in a high-level language is easier.
A high-level language is more readable and understandable.
The programs created using high-level language runs on different machines with
little change or no change.
Easy to understand, create programs, find errors and modify.
Disadvantages
High-level language needs to be translated into low-level language.
High-level language executes slower compared to middle and low-level languages.
Understanding Computer Languages
The following figure provides a few key points related to computer languages.
From the above figure, we can observe the following key points...
The programming languages like C, C++, Java, etc., are written in High-level language
which is more comfortable for the developers.
A high-level language is closer to the users.
Low-level language is closer to the computer. Computer hardware can understand
only the low-level language (Machine Language).
The program written in the high-level language needs to be converted to low-level
language to make communication between the user and the computer.
Middle-level language is not closer to both user and computer. We can consider it as
a combination of both high-level language and low-level language.
Creating and Running C Program
Generally, the programs created using programming languages like C, C++, Java, etc., are
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
language instructions into low-level language instructions. A compiler is a program that
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…
Execution Process of a C Program
When we execute a C program it undergoes with the following process…
The file which contains c program instructions in a high-level language is said to be source
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.
With this compilation process completes.
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
C program file (Source file) must save with .c extension.
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
generates .obj file in this process.
The compiler converts the file only if there are no errors in the source code.
CPU places the result in User Screen window.
Overall Process
Type the program in C editor and save with .c extension (Press F2 to save).
Press Alt + F9 to compile the program.
If there are errors, correct the errors and recompile the program.
If there are no errors, then press Ctrl + F9 to execute/run the program.
Press Alt + F5 to open User Screen and check the result.
Unit 2
C Program Basics
C is a structured programming language. Every c program and its statements must be in a
particular structure. Every c program has the following general structure...
Line 1: Comments - They are ignored by the compiler
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
any number of single line comments.
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
the program and its code.
Line 2: Pre-processing Commands
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.
Line 3: Global Declaration
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
global declaration is used based on the requirement.
Line 4: int main()
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
execution. If we don't want to return any value, we can use it as void.
Line 5: Open Brace ( { )
The open brace indicates the beginning of the block which belongs to the main method. In C
program, every block begins with a '{' symbol.
Line 6: Local Declaration
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
within the function or block in which they are declared.
Line 7: Executable statements
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.
Line 9: Closing Brace ( } )
The close brace indicates the end of the block which belongs to the main method. In C
program every block ends with a '}' symbol.
Line 10, 11, 12, ...: User-defined function()
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.
General rules for any C program
1. Every executable statement must end with a semicolon symbol (;).
2. Every C program must contain exactly one main method (Starting point of the
program execution).
3. All the system-defined words (keywords) must be used in lowercase letters.
4. Keywords can not be used as user-defined names(identifiers).
5. For every open brace ({), there must be respective closing brace (}).
6. Every variable must be declared before it is used.
Preprocessor programs provide preprocessors directives which tell the
compiler to preprocess the source code before compiling. All of these
preprocessor directives begin with a ‘#’ (hash) symbol. The ‘#’ symbol
indicates that, whatever statement starts with #, is going to the preprocessor
program, and preprocessor program will execute this statement. Examples of
some preprocessor directives are: #include, #define, #ifndef etc. Remember
that # symbol only provides a path that it will go to the preprocessor, and
command such as include is processed by preprocessor program. For
example, include will include extra code to your program. We can place
these pre-processor directives anywhere in our program.
There are 4 main types of pre-processor directives:
1. Macros
2. File Inclusion
3. Conditional Compilation
4. Other directives
Let us now learn about each of these directives in detail.
Macros: Macros are a piece of code in a program which is given
some name. Whenever this name is encountered by the compiler
the compiler replaces the name with the actual piece of code. The
‘#define’ directive is used to define a macro.
#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.
Note: There is no semi-colon(‘;’) at the end of macro definition. Macro
definitions do not need a semi-colon to end.
Macros with arguments: We can also pass arguments to macros. Macros
defined with arguments works similarly as functions. Let us understand this
with a program:
#include <stdio.h>
// macro with parameter
#define AREA(l, b) (l * b)
int main()
{
int l1 = 10, l2 = 5, area;
area = AREA(l1, l2);
printf("Area of rectangle is: %d", area);
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.
user defined files: When a program becomes very large, it is good
practice to divide it into smaller files and include whenever needed.
These types of files are user defined files.
These files can be included as:
#include"filename"
Conditional Compilation: Conditional Compilation directives are type of
directives which helps to compile a specific portion of the program or to
skip compilation of some specific part of the program based on some
conditions. This can be done with the help of two preprocessing
commands ‘ifdef‘ and ‘endif‘.
Syntax:
#ifdef macro_name
statement1;
statement2;
statement3;
.
.
.
statementN;
#endif
Other directives: Apart from the above directives there are two
more directives which are not commonly used.
These are: #undef Directive: The #undef directive is used to
undefine an existing macro.
This directive works as:
#undef LIMIT