Programming in C Basics Unit-1
Programming in C Basics Unit-1
A computer system consists of hardware components that have been carefully chosen so that
they work well together and software components or programs that run in the computer. The
main software component is itself an operating system (OS) that manages and provides services
to other programs that can be run in the computer.
In its most basic form, a computer system is a programmable electronic device that can accept
input; store data; and retrieve, process and output information.
Computer systems consist of three components as shown in below image: Central Processing
Unit, Input devices and Output devices. Input devices provide data input to processor, which
processes data and generates useful information that’s displayed to the user through output
devices. This is stored in computer’s memory.
Memory Unit
This is unit in which data and instructions given to computer as well as results given by
computer are stored. Unit of memory is "Byte".
1 Byte = 8 Bits
Computer Language:
A computer language is a formal language used to communicate with a computer. Types of
computer languages include:
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.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.
Step 1: Creating a Source Code
Source code is a file with C programming instructions in a high-level language. To create source
code, we use any text editor to write the program instructions. The instructions written in the
source code must follow the C programming language rules. The following steps are used to
create a source code file in Windows OS…
The compiler first checks for the Errors. If there are any Errors then compiler returns List of
Errors, if there are no errors then the source code is converted into object code and stores it as a
file with .obj extension. Then the object code is given to the Linker. The Linker combines both
the object code and specified header file code and generates an Executable file with
a .exe extension.
Process:
Type the program in C editor and save with .c extension (Press F2 to save).
Compile the program.
If there are errors, correct the errors and recompile the program.
If there are no errors, then run the program.
Preprocessor:
The C preprocessor is a macro preprocessor (allows you to define macros) that transforms your
program before it is compiled. These transformations can be the inclusion of header files, macro
expansions, etc.
#define PI 3.14
The #include preprocessor is used to include header files to C programs. For example,
#include <stdio.h>
Here, stdio.h is a header file. The #include preprocessor directive replaces the above line with the
contents of stdio.h header file. That's the reason why you need to use #include <stdio.h> before
you can use functions like scanf() and printf().
You can also create your own header file containing function declaration and include it in your
program using this preprocessor directive.
A macro is a fragment of code that is given a name. You can define a macro in C using
the #define preprocessor directive. Here's an example.
What is a compilation?
The compilation is a process of converting the source code into object code. It is done with the
help of the compiler. The compiler checks the source code for the syntactical or structural errors,
and if the source code is error-free, then it generates the object code.
The c compilation process converts the source code taken as input into the object code or
machine code. The compilation process can be divided into four steps, i.e., Pre-processing,
Compiling, Assembling, and Linking.
The preprocessor takes the source code as an input, and it removes all the comments from the
source code. The preprocessor takes the preprocessor directive and interprets it. For example,
if <stdio.h>, the directive is available in the program, then the preprocessor interprets the
directive and replace this directive with the content of the 'stdio.h' file.
The following are the phases through which our program passes before being transformed into an
executable form:
o Preprocessor
o Compiler
o Assembler
o Linker
Preprocessor
The source code is the code which is written in a text editor and the source code file is given an
extension ".c". This source code is first passed to the preprocessor, and then the preprocessor
expands this code. After expanding the code, the expanded code is passed to the compiler.
Compiler
The code which is expanded by the preprocessor is passed to the compiler. The compiler
converts this code into assembly code. Or we can say that the C compiler converts the pre-
processed code into assembly code.
Assembler
The assembly code is converted into object code by using an assembler. The name of the object
file generated by the assembler is the same as the source file. The extension of the object file in
DOS is '.obj,' and in UNIX, the extension is 'o'. If the name of the source file is 'hello.c', then the
name of the object file would be 'hello.obj'.
Linker
Mainly, all the programs written in C use library functions. These library functions are pre-
compiled, and the object code of these library files is stored with '.lib' (or '.a') extension.
The main working of the linker is to combine the object code of library files with the object
code of our program. Sometimes the situation arises when our program refers to the functions
defined in other files; then linker plays a very important role in this. It links the object code of
these files to our program.
Therefore, we conclude that the job of the linker is to link the object code of our program with
the object code of the library files and other files.
The output of the linker is the executable file. The name of the executable file is the same as the
source file but differs only in their extensions. In DOS, the extension of the executable file is
'.exe', and in UNIX, the executable file can be named as 'a.out'. For example, if we are using
printf() function in a program, then the linker adds its associated code in an output file.