Advanced c programming chapter 1 questions and answers
Advanced c programming chapter 1 questions and answers
QA Review Questions
1.write a short note on c preprocessor.
=>A preprocessor is a program that process or analyse the source code file before it is given to the compiler . it perform sum action according to special instruction called preprocessor directives(which begin with #) in the source code. A preprocessor or directive begin with hash and does not end with semicon.
2. Explain macro substitution in brief with example.
=> The macro substitution directive is #define. it defines the symbol which represent a value . Whenever the macro name occurs in a program the preprocessor substitutes the code of the macro at that position. it defines symbolic constants with occur frequently in a program.
Syntax
#define macro _name value
Examples
1 #define PI 3.142
2#define TRUE 1
3 #define AND &&
4 # define LESSTHAN <
5 #define GREET prints ("Hello");
6#define MESSAGE "welcome to c"
7#define INRANGE(a>=60&&a<70)
Example
Int a=65;
If(INRANGE)
Printf ("first class");
Parameterized Macro
Just like a function a macro can be defined with parameters. Such a macro is called as function macro because it looks like a function .It represent a small executable code. Each time the macro name encountered, it's s are replaced by the actual armaments which are used in the micro call.
Example
#define HALFOF(x) X/2
#define SQR(x) x*x
#define MAX (x,y) ((x)>(y)?(x):(y))
Whenever the macro occurs in the program, it's arguments( example X ) is replaced by the variable or value used in the program.
For Example:
result =HALFOF (10); //replace by result=10/2;
ans=SQR(a); //replaced by and=a*a
Nested Macro
Macro can be contained within another macro. This is is called nesting of macros.
Example
#define CUBE (x) SQR(x)* (x)
3. When a Parameterized macro is defined, Why should each argument be eclosed in parenthesis?
=> Because it is advisable to enclose the argument in parenthesis if the arguments are not enclosed in parenthesis it may yield wrong results.
4. can one macro used inside another macro?
=> Yes ,using nested macro we can use one macro in another macro.
5. List the difference between macros and function .
=> Macro
1. macro is small in size it usually does contain more than one line.
2.a macro is preprocessed.
3. macro is expanded in the code. i.e , its code replace the macro name.
4. The program size increases.
5. Execution of macros is faster since the code is replaced.
6. No type checking is done for macros . hence their can be side-effects or unforeseen errors.
7. Usually used for small code which appear many times.
Function
1. Function can contains several lines of code.
2. A function is compiled.
3. Function code is not inserted into the program when a function is called.
4. The program size does not increase.
5. Functions execute slower since control has to be passed to the function and then returned back to the program.
6. Strict type checking is done for functions.
7. Usually used for large code that needs to be executed many times.
6.List the conditional compilation directives.
=> This directive allow specific parts of the program to be included or discarded based on a certain condition. 6 directive are available to control conditional compilation.This are #if ,#ifdef, #ifndef ,#else ,#elif , and #endif.
The program text within the blocks may consist of preprocessor directive ,c statement and so on the beginning of the block consists of one of the following three directives.
#if
#ifdef
#ifndef
Optionally and alternative block of text can be indicated by the following two directives.
#else
#elif
The end of the block or alternative block is marked by the #endif directive.
7. What is difference between#else and #elif?
=> #else
Syntax
#else
This directive is used with #if , #ifdef, #ifndef, to delimit alternative source text to be compiled if the condition is false. it is optional.
And
#elif
Syntax
#elif constant-expression
The #elif directive performs a task similar to the combined use of the else- if statement in c. This directive is used with #if,#ifdef , #ifndef , or another #elif to indicate alternative code if the condition is false. it is optional. The difference between #else and #elif is that elif must be followed by a condition.Also #elif can have the following #elif or #else cannot be followed by an #else or #elif.
Comments
Post a Comment