Lecture05 C WS
Lecture05 C WS
Chapter 02
C-Environment
Q. What is the translator?
Definition: The user’s instructions are always in English, which is called source code.
But the computer is not able to understand this source code and the computer’s
understandable code is binary / machine. To convert this source code into binary code,
we use the interface software translators.
Q. Explain any three types of Translators.
There are three types of Translators in C.
1. Compiler
2. Interpreter
3. Assembler
Compiler:
Definition: It converts high-level language (source code) to machine language
(assembly language). The compiler takes the whole program and converts it into
machine language.
Interpreter:
Definition: It is the same as the compiler but it does not take the whole program for
the conversion. It takes line-by-line code and converts it into machine language.
Assembler:
Definition: Assemblers convert the assembly language code to binary language code
as an output.
Logical Error:
Sometimes, we do not get the output we expected after the compilation and
execution of a program. Even though the code seems error-free, the output generated
is different from the expected one. These types of errors are called Logical Errors.
For example, instead of using the equal-to-comparison operator (==), we use the
assignment operator (=).
Semantic Error:
Errors that occur because the compiler is unable to understand the written code are
called Semantic Errors. A semantic error will be generated if the code makes no sense
to the compiler, even though it is syntactically correct. It is like using the wrong word
in the wrong place in the English language. For example, adding a string to an integer
will generate a semantic error.
Linker Error:
Linker errors are the errors encountered when the executable file of the code cannot be
generated even though the code gets compiled successfully. This error is generated
when a different object file is unable to link with the main object file.
Q. What is the function of printf() and scanf()?
printf() function:
• printf() function is defined in stdio.h header file.
• By using this function, we can print the data or user-defined message on the
monitor.
• In C Programming language, the printf() function is used for output.
scanf() function:
In C programming, scanf() is one of the commonly used functions to take input from
the user.
The scanf() function reads formatted(int,float etc) input from the standard
input such as keyboards.
Example:
#include<stdio.h>
int main()
{
int x;
scanf("%d",&x);
printf("The number is=%d",x);
return 0;
}
Q. Why do we use &?
• While scanning the input, scanf () needs to store that input data somewhere.
• To store this input data, scanf() needs to know a variable’s memory location.
And here comes the ampersand to rescue.
• & is also called as address of the operator.
• For example, &x is the address of x.
Q. What is an Algorithm? Write down the Properties of an Algorithm.
Algorithm
Definition: The algorithm is a step–by–step procedure that helps solve a problem.
Properties of an Algorithm
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 errors, and if the source code is error-
free, then it generates the object code.
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 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, passed to the compiler.
Compiler
It converts high-level language (source code) to machine language (assembly language).
Assembler
Assemblers convert the assembly language code to binary language code known as object code
as an output. The extension of the object file in DOS is '.obj.
Linker
Mainly, all the programs written in C use library functions. 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. For example,
if we are using printf() function in a program, then the linker adds its associated code in an output
file