Compilation process in c
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:
Pre-processor
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 pre-processor, and then
the pre-processor expands this code. After expanding the code, the expanded code is
passed to the compiler.
Removal of Comments
Expansion of the included files.
Conditional compilation
The pre-processed output is stored in the filename.i.
Compiler
The code, which is expanded by the pre-processor, 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.
Similarly, if your program code is too big and you break it into two files, then both the
source code files will be converted into object code separately and then the Linker will
link them and make the code ready for execution. This is also called Separate
Compilation.
Programming Errors in C
Errors are the problems or the faults that occur in the program, which makes the
behaviour of the program abnormal, and experienced developers can also make these
faults. Programming errors are also known as the bugs or faults, and the process of
removing these bugs is known as debugging.
These errors are detected either during the time of compilation or execution. Thus, the
errors must be removed from the program for the successful execution of the program.
There are mainly five types of errors exist in C programming:
o Syntax error
o Semantic error
Syntax error
Syntax errors are also known as the compilation errors as they occurred at the
compilation time, or we can say that the syntax errors are thrown by the compilers.
These errors are mainly occurred due to the mistakes while typing or do not follow the
syntax of the specified programming language. These mistakes are generally made by
beginners only because they are new to the language. These errors can be easily
debugged or corrected.
For example:
1. If we want to declare the variable of type integer,
2. int a; // this is the correct form
3. Int a; // this is an incorrect form.
Commonly occurred syntax errors are:
o If we miss the parenthesis (}) while writing the code.
o Displaying the value of a variable without its declaration.
o If we miss the semicolon (;) at the end of the statement.
Semantic error
Semantic errors are the errors that occurred when the statements are not
understandable by the compiler.
The following can be the cases for the semantic error:
o Type compatibility
int b = "javatpoint";
o Errors in expressions
int a, b, c;
a+b = c;
o Array index out of bound
int a[10];
a[10] = 34;
Let's understand through an example.
1. #include <stdio.h>
2. int main()
3. {
4. int a,b,c;
5. a=2;
6. b=3;
7. c=1;
8. a+b=c; // semantic error
9. return 0;
10. }
In the above code, we use the statement a+b =c, which is incorrect as we cannot use the
two operands on the left-side.
Output
ASCII value in C
What is ASCII code?
ASCII
The full form of ASCII is the American Standard Code for information interchange. It
is a character-encoding scheme used for electronics communication. Each character or a
special character is represented by some ASCII code, and each ascii code occupies 7 bits
in memory.
In C programming language
, a character variable does not contain a character value itself rather the ascii value of
the character variable. The ascii value represents the character variable in numbers,
and each character variable is assigned with some number range from 0 to 127. For
example, the ascii value of 'A' is 65.
In the above example, we assign 'A' to the character variable whose ascii value is 65, so
65 will be stored in the character variable rather than 'A'.