SlideShare ist ein Scribd-Unternehmen logo
1 von 23
UNIT-V
PART – A
1. Define storage class specifier.
Storage classes are used to define things like storage location (whether RAM or REGISTER), scope,
lifetime and the default value of a variable.
2. Mention different type of storage classes.
1. auto storage class
2. extern storage class
3. static storage class
4. register storage class
3. Distinguish betweenauto and register
Storage
Class Keyword
Memory
Location
Default
Value Scope Life Time
Automatic auto Computer
Memory
(RAM)
Garbage
Value
Local to the
block in
which the
variable has
defined
Till the control remains
within the block in which
variable is defined
Register register CPU
Register
Garbage
Value
Local to the
block in
which the
variable has
defined
Till the control remains
within the block in which
variable is defined
4. What is meant by extern? Give an example.
The default storage class of all global variables (variables declared outside function) is
external storage class.
Example
#include<stdio.h>
#include<conio.h>
int i; //By default it is extern variable
int main()
{
printf("%d",i);
return 0;
}
5. What is storage class for variable A in below code?Justify.
int main()
{
int A;
A = 10;
printf("%d", A);
return 0;
}
The default storage class of all local variables (variables declared inside block or
function) is auto storage class.
6. What will the SWAP macro in the following program be expanded to on preprocessing?
will the codecompile?
#include<stdio.h>
#define SWAP(a, b, c)(c t; t=a, a=b, b=t)
int main()
{
int x=10, y=20;
SWAP(x, y, int);
printf("%d %dn", x, y);
return 0;
}
7. How are preprocessordirectives written in C?
C program is compiled in a compiler, source codeis processedbya program
called preprocessor. This process is called preprocessing. Commands used
in preprocessor are called preprocessordirectives and they begin with “#”symbol.
8. How can you avoid including a header more than once?
One easy technique to avoid multiple inclusions of the same header is to use the #ifndef and #define
preprocessor directives. When you create a header for your program, you can #define a symbolic name
that is unique to that header.
9. How to pragma directive works?
These directives helps us to specify the functions that are needed to run before program
startup( before the controlpasses to main()) and just before program exit (just before
the control returns from main()).
10.Compare the pragma and conditional directive.
pragma directive conditional directive
Syntax: #pragma #ifdef, #endif, #if,
#else, #ifndef
Definition #Pragma is used to call a function before
and after main function in a C program.
Set of commands are
included or excluded in
sourceprogram before
compilation with
respect to the
condition.
11.Examine the six pragma directives.
#pragma startup, #pragma exit, #pragma warn, #pragma GCC dependency, #pragma
GCC system_header and #pragma GCC poison
12.Write the syntax of pragma directive.
#pragma token_name
13.Is there any difference that arises if double quotes , instead of angular brackets are used
for including the standard header file?
When include file in <> then it searches the specified file system directory.Ex.
#include<stdio.h>It will search stdio. h file in system directory.
When including file in “” Then it search this specified file in currently working
directory.
14.Identify the use of pragma directive in c.
The preprocessordirective #pragma is used to provide the additional information to the
compiler in C language.
15.List out the seven conditional directives in c.
#if, #ifdef, #ifndef, #else, #elif, #endif, and defined
16.What is the use of #if directive?
“#ifdef” directive checks whether particular macro is defined or not. If it is defined, “If”
clause statements are included in source file.
Otherwise, “else” clause statements are included in source file for compilation and
execution.
17.Write a note on define macro.
This macro defines constant value and can be any of the basic data types.
There are two types of macros:
(i) Object-like Macros
(ii) Function-like Macros
18.Evaluate the advantages of a macro over a function.
When writing macros for functions, they saves a lot of time that is spent by the
compiler for invoking / calling the functions. Hence, The advantage of a macro
over an actual function, is speed. No time is taken up in passing control to a
new function, becausecontrol never leaves the home function
19.Develop an example for conditional compilation.
#include <stdio.h>
#define X 45.25
int main()
{
#if !X
printf("Hello");
#else
printf("World");
#endif
return 0;
}
Output:
main.c: In function ‘main’:
main.c:2:11: error: floating constant in preprocessorexpression
#define X 45.25
^
main.c:5:10: note: in expansion of macro ‘X’
#if !
20.What does #undef, #pragma indicate in c?
The #pragma preprocessordirective is used to provide additional information to the
compiler. The #pragma directive is used by the compiler to offer machine or operating-
system feature
PART – B
1. Describe about the various storage classes with example program.(13)
C provides four storage classes as:
1. auto storage class
2. register storage
3. extern storage class
4. static storage class
 Storage class determines its storage duration, scope and linkage.
 Storage duration is the period during which that identifier exists in the memory.
 Scope is where the identifier can be referenced in a program.
 Linkage determines for a multiple-source file.
Summarised all storage classes in C as following as:
Features
Automatic Storage
Class
Register Storage
Class
Static Storage Class
External Storage
Class
Keyword auto register static extern
Initial Value Garbage Garbage Zero Zero
Storage Memory CPU register Memory Memory
Scope
scope limited,local to
block
scope limited,local to
block
scope limited,local to block Global
Life
limited life of
block,where defined
limited life of
block,where defined
value of variable persist
between different function calls
Global,till the
program
execution
2. Distinguish between the following macro.
(i)object like macro(7)
The preprocessor (i.e. translator that processes the source code before it is given to the compiler) is
controlled by directives known as preprocessor directives. A preprocessor directive consists of
various
preprocessing tokens (i.e. smallest indivisible elements of C language) and begins with a # (pound)
symbol. The various preprocessor directives available in C language are:
Macro Replacement Directive (#define, #undef): A macro is a facility provided by the C
preprocessor, by which a token can be replaced by the user-defined sequence of characters. The
types of macros are:
Object-like Macros: An object-like macro is also known as a symbolic constant. It is defined as:
#define macro-name replacement-list
Example: #define PI 3.142
(ii)function like macro(6).
Function-like Macros: A macro with arguments is called a function-like macro. It is defined as:
#define macro-name(parameter-list) replacement-list
Memory
location
Stack Register memory Segment Segment
Example
void main()
{
auto int i;
printf("%d",i);
}
OUTPUT
124
void main()
{
register int i;
for(i=1; i<=5 ;
i++);
printf("%d ",i);
}
OUTPUT
1 2 3 4 5
void add();
void main()
{
add();
add();
}
void add()
{
static int i=1;
printf("n%d",i);
i=i+1;
}
OUTPUT
1
2
void main()
{
extern int i;
printf("%d",i);
int i=5
}
OUTPUT
5
Example: #define SQR(x) (x*x)
During the preprocessing stage, the macro names are expanded and are replaced by their
replacement lists. This process is known as macro expansion.
Predefined Macros: The macros that are already defined in C language are known as predefined
macros. Some of the predefined macros are:
a. __FILE__: This macro expands to the name of the current file in the form of a string constant.
b. __LINE__: The __LINE__ macro expands to the current line number in the source file.
c. __DATE__: The __DATE__ macro expands to the compilation date of the source file in the form
of a string constant of the form “Mmm dd yyyy”. Example: Nov 27 2014.
d. __TIME__: This macro expands to the time at which the C preprocessor is being invoked in the
form of a string constant of the form “hh:mm:ss”. Example: 19:57:05.
undef Directive: The undef directive causes the specified identifier to be no longer defined as a
macro name. The general form is:
#undef identifer
Example: #undef PI
3. Illustrate and explain about unconditional preprocessor directive.(13)
The unconditional directives are:
#include - Inserts a particular header from another file
– used to include the header files (e.g. stdio.h, math.h, string.h, etc.) that contain
predefined library functions to carry out specific tasks. Example: #include<stdio.h>
#include<stdio.h> // Preprocessor directive to include the standard input / output header
file.
main ( ) // Mandatory function
{
printf(“Welcome to C Programming…”); // Function body containing only one
executable statement.
}
#define - Defines a preprocessor macro
– used to define symbolic constants.
Example: #define PI 3.1429
#undef - Undefines a preprocessor macro
The undef directive causes the specified identifier to be no longer defined as a
macro name. The general form is:
#undef identifer
Example: #undef PI
4. Explain about #if, #else ,#elif directive with an example program.(13)
These directives are used for conditional compilation. Conditional compilation means that a part of a
program is compiled only if a condition evaluates to true.
EXAMPLE PROGRAM FOR #IF & #ELSE IN C:
#include <stdio.h>
#define a 100
int main()
{
#if (a==100)
printf("This line will be added in this C file since " 
"a = 100n");
#else
printf("This line will be added in this C file since " 
"a is not equal to 100n");
#endif
return 0;
}
#elifdirective
The preprocessor will include the C source code that follows the #elif statement when the condition of
the preceding #if, #ifdef or #ifndef directive evaluates to false and the #elif condition evaluates to
true.
Syntax
The syntax for the #elif directive in the C language is:
#elif conditional_expression
conditional_expression
Expression that must evaluate to true for the preprocessor to include the C source code into the
compiled application.
The #elif directive must be closed by an #endif directive.
Example
The following example shows how to use the #elif directive in the C language:
/* Example using #elif directive by TechOnTheNet.com */
#include <stdio.h>
#define YEARS_OLD 12
int main()
{
#if YEARS_OLD <= 10
printf("TechOnTheNet is a great resource.n");
#elif YEARS_OLD > 10
printf("TechOnTheNet is over %d years old.n", YEARS_OLD);
#endif
return 0;
}
In this example, YEARS_OLD has a value of 12 so the statement #if YEARS_OLD <=10 evaluates
to false. As a result, processing is passed to the #elif YEARS_OLD > 10 statement which evaluates to
true. The C source code following the #elif statement is then compiled into the application.
Here is the output of the executable program:
TechOnTheNet is over 12 years old.
5. Describe the defined operator and #error directive .(13)
The defined Operator
Another way to verify that a macro is defined is to use the defined unary operator. The defined
operator has one of the following forms:
defined name
defined (name)
An expression of this form evaluates to 1 if name is defined and to 0 if it is not.
The defined operator is especially useful for checking many macros with just a single use of the #if
directive. In this way, you can check for macro definitions in one concise line without having to use
many #ifdef or #ifndef directives.
For example, consider the following macro checks:
#ifdef macro1
printf( "Hello!n" );
#endif
#ifndef macro2
printf( "Hello!n" );
#endif
#ifdef macro3
printf( "Hello!n" );
#endif
Another use of the defined operator is in a single #if directive to perform similar macro checks:
#if defined (macro1) || !defined (macro2) || defined (macro3)
printf( "Hello!n" );
#endif
Error Directive (#error):
The error directive causes the preprocessor to generate the customized diagnostic messages and causes
the compilation to fail.
The #error directive causes preprocessing to stop at the location where the directive is encountered.
Information following the #error directive is output as a message prior to stopping preprocessing.
Syntax
The syntax for the #error directive in the C language is:
#error message
message
Message to output prior to stopping preprocessing.
Example
Let's look at how to use #error directives in your C program.
The following example shows the output of the #error directive:
/* Example using #error directive by TechOnTheNet.com */
#include <stdio.h>
#include <limits.h>
/*
* Calculate the number of milliseconds for the provided age in years
* Milliseconds = age in years * 365 days/year * 24 hours/day, 60 minutes/hour, 60 seconds/min, 1000
millisconds/sec
*/
#define MILLISECONDS(age) (age * 365 * 24 * 60 * 60 * 1000)
int main()
{
/* The age of TechOnTheNet in milliseconds */
int age;
#if INT_MAX < MILLISECONDS(12)
#error Integer size cannot hold our age in milliseconds
#endif
/* Calculate the number of milliseconds in 12 years */
age = MILLISECONDS(12);
printf("TechOnTheNet is %d milliseconds oldn", age);
return 0;
}
6. Define a macro in C to check whether a given three-digit number is an Armstrong number or
odd. Illustrate the use of this macro in a program. (13)
#include <stdio.h>
#define arms(x) (x*x*x)
int main() {
int num, originalNum, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
originalNum = num;
while (originalNum != 0) {
remainder = originalNum % 10;
result += arms(remainder);
originalNum /= 10;
}
if (result == num)
printf("%d is an Armstrong number.", num);
else
if (num%2!=0)
printf("%d is odd number.", num);
return 0;
}
7. Write about conditional preprocessor directive with an example.(13)
The conditional directives are:
#ifdef - If this macro is defined
#ifndef - If this macro is not defined
#if - Test if a compile time condition is true
#else - The alternative for #if
#elif - #else an #if in one statement
#endif - End preprocessor conditional
These directives are used for conditional compilation. Conditional compilation means that a part of a
program is compiled only if a condition evaluates to true.
EXAMPLE PROGRAM FOR #IF & #ELSE IN C:
#include <stdio.h>
#define a 100
int main()
{
#if (a==100)
printf("This line will be added in this C file since " 
"a = 100n");
#else
printf("This line will be added in this C file since " 
"a is not equal to 100n");
#endif
return 0;
}
#elifdirective
The preprocessor will include the C source code that follows the #elif statement when the condition of
the preceding #if, #ifdef or #ifndef directive evaluates to false and the #elif condition evaluates to
true.
Syntax
The syntax for the #elif directive in the C language is:
#elif conditional_expression
conditional_expression
Expression that must evaluate to true for the preprocessor to include the C source code into the
compiled application.
The #elif directive must be closed by an #endif directive.
Example
The following example shows how to use the #elif directive in the C language:
/* Example using #elif directive by TechOnTheNet.com */
#include <stdio.h>
#define YEARS_OLD 12
int main()
{
#if YEARS_OLD <= 10
printf("TechOnTheNet is a great resource.n");
#elif YEARS_OLD > 10
printf("TechOnTheNet is over %d years old.n", YEARS_OLD);
#endif
return 0;
}
For example, consider the following macro checks:
#ifdef macro1
printf( "Hello!n" );
#endif
#ifndef macro2
printf( "Hello!n" );
#endif
#ifdef macro3
printf( "Hello!n" );
#endif
Another use of the defined operator is in a single #if directive to perform similar macro checks:
#if defined (macro1) || !defined (macro2) || defined (macro3)
printf( "Hello!n" );
#endif
8. Define a macro in C to check whether a given number is evenor odd. Illustrate the use of this
macro in a program. (13)
#include <stdio.h>
#define ISEVEN(n) ((n%2 == 0) ? 1 : 0)
#define ISODD(n) ((n%2 != 0) ? 1 : 0)
int main(void)
{
int number;
printf("Enter an integer number: ");
scanf("%d",&number);
if(ISEVEN(number))
printf("%d is an EVEN numbern",number);
else if(ISODD(number))
printf("%d is an ODD numbern",number);
else
printf("An Invalid Inputn");
return 0;
}
9. Write about all the pragma directive and explain in detail.(13)
The #pragma preprocessor directive is used to provide additional information to the compiler. The
#pragma directive is used by the compiler to offer machine or operating-system feature.
The pragma directive configures some of the compiler options that can otherwise be configured from
the command line. An unrecognized pragma directive is ignored without an error or a warning
message. The pragma directive is written as:
#pragma token-sequence
The commonly used forms of the pragma directive are:
a. #pragma option: It is written as #pragma option [options…].
Example: #pragma option –C Allows nested comments.
#pragma option –C– Does not allow the nesting of comments.
b. #pragma warn: It can be used to turn on, off or toggle the state of warnings. It can be written as:
#pragma warn +www (Turns on the warning with character code www)
#pragma warn –www (Turns off the warning with character code www)
#pragma warn .www (Toggles the state of warning with character code www)
Example:#pragma warn –dup Suppresses the warning ‘Redefinition of macro is not identifal’.
#pragma warn –aus Suppresses warning ‘Identifier is assigned value that is never used’.
c. #pragma startup and #pragma exit: These directives can be used to execute a function before
and after the execution of the function main. These directives can be written as:
Example:
#pragma startup function-name
#pragma exit function-name
#include<stdio.h>
#include<conio.h>
void School();
void College() ;
#pragma startup School 105
#pragma startup College
#pragma exit College
#pragma exit School 105
void main(){
printf("nI am in main");
getch();
}
void School(){
printf("nI am in School");
getch();
}
void College(){
printf("nI am in College");
getch();
}
Output :
I am in College
I am in School
I am in main
I am in School
I am in College
10. Write the C coding for finding the average of number using any of the storage class
declarations.(13)
#include<stdio.h>
int main()
{
printf("nnttStudytonight - Best place to learnnnn");
int n, i; /* auto */
float sum = 0, x;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("nnnEnter %d elementsnn", n);
for(i = 0; i < n; i++)
{
scanf("%f", &x);
sum += x;
}
printf("nnnAverage of the entered numbers is = %f", (sum/n));
printf("nnnntttCoding is Fun !nnn");
return 0;
}
11. Explain the various ways in which a source file inclusion directive can be written.(13)
The source file inclusion directive tells the preprocessor to replace the directive with the content of
the file specified in the directive. This directive is generally used to include the header files, which
contain the prototypes of the library functions and the definitions of the predefined constants.
The include directive can be written as:
a. #include <name-of-file> : Searches the prespecified list of directories for the source file whose
name is given within angular brackets, and text embeds the entire
content of the source file in place of itself. If the file is not found there, it
will show the error “Unable to include ‘name-of-file’”.
b. #include “name-of-file” : First searches the file in the current working directory. If the search fails,
the search will be carried out in the prespecified list of directories. If the
search still fails, it will show the error “Unable to include ‘name-of-file’”.
Example: #include <stdio.h>
#include<stdio.h> // Preprocessor directive to include the standard input / output header file.
main ( ) // Mandatory function
{
printf(“Welcome to C Programming…”); // Function body containing only one executable statement.
}
12. Examine the various stages a program undergoes before execution.(13)
Pre-processing, Compiling, Linking, Loading
Execution of a C/C++ program involves four stages using different compiling/execution tool, these
tools are set of programs which help to complete the C/C++ program's execution process.
1. Preprocessor
2. Compiler
3. Linker
4. Loader
These tools make the program running.
1) Preprocessor
This is the first stage of any C/C++ program execution process; in this stage Preprocessor processes
the program before compilation. Preprocessor include header files, expand the Macros.
2) Complier
This is the second stage of any C/C++ program execution process, in this stage generated output file
after preprocessing ( with source code) will be passed to the compiler for compilation. Complier will
compile the program, checks the errors and generates the object file (this object file contains assembly
code).
3) Linker
This is the third stage of any C/C++ program execution process, in this stage Linker links the more
than one object files or libraries and generates the executable file.
4) Loader
This is the fourth or final stage of any C/C++ program execution process, in this stage Loader loads
the executable file into the main/primary memory. And program run.
Different files during the process of execution
Suppose, you save a C program with prg1.c – here .c is the extension of C code, prg1.c file contains
the program (source code of a C program). Preprocessor reads the file and generates the prg1.i
(prg1.ii – for c++ source code) file, this file contains the preprocessed code.
Compiler reads the prg1.i file and further converts into assembly code and generates prg1.s and then
finally generates object code in prg1.o file.
Linker reads prg1.o file and links the other assembly/object code or library files and generates
executable file named prg1.exe.
Loader loads the prg1.exe file into the main/primary memory and finally program run.
One more file is created that contains the source code named prg1.bak; it’s a backup file of the
program files.
13. Write a C Program to calculate the factorial of a number by using the keyword static.(13)
#include<stdio.h>
void main()
{
int num,i,f=1;
clrscr();
printf("Enter the a Num : ");
scanf("%d",&num);
printf("Factorial of %d = %d", n, fact(num));
getch();
}
fact(static int n)
{
int f=1;
if(n<=1) return 1;
else
{
f=n*fact(n-1);
return f;
}
}
14. Write a C Program to generate Fibonacci series by using keyword auto.(13)
#include<stdio.h>
int f(int);
int main()
{
int n, i = 0, c;
scanf("%d", &n);
printf("Fibonacci series terms are:n");
for (c = 1; c <= n; c++)
{
printf("%dn", f(i));
i++;
}
return 0;
}
int f( auto int n)
{
if (n == 0 || n == 1)
return n;
else
return (f(n-1) + f(n-2));
}
PART C
1. Write any two common macro pitfalls with example program.
(i) Misnesting
When a macro is called with arguments, the arguments are substituted into the macro body and the
result is checked, together with the rest of the input file, for more macro calls. It is possible to piece
together a macro call coming partially from the macro body and partially from the arguments. For
example,
#define twice(x) (2*(x))
#define call_with_1(x) x(1)
call_with_1 (twice)
==> twice(1)
==> (2*(1))
Macro definitions do not have to have balanced parentheses. By writing an unbalanced open
parenthesis in a macro body, it is possible to create a macro call that begins inside the macro body but
ends outside of it. For example,
#define strange(file) fprintf (file, "%s %d",
…
strange(stderr) p, 35)
==> fprintf (stderr, "%s %d", p, 35)
The ability to piece together a macro call can be useful, but the use of unbalanced open parentheses in a
macro body is just confusing, and should be avoided.
(ii) Operator Precedence Problems
You may have noticed that in most of the macro definition examples shown above, each occurrence of
a macro argument name had parentheses around it. In addition, another pair of parentheses usually
surround the entire macro definition. Here is why it is best to write macros that way.
Suppose you define a macro as follows,
#define ceil_div(x, y) (x + y - 1) / y
whose purpose is to divide, rounding up. (One use for this operation is to compute how
many int objects are needed to hold a certain number of char objects.) Then suppose it is used as
follows:
a = ceil_div (b & c, sizeof (int));
==> a = (b & c + sizeof (int) - 1) / sizeof (int);
his does not do what is intended. The operator-precedence rules of C make it equivalent to this:
a = (b & (c + sizeof (int) - 1)) / sizeof (int);
2. Write preprocessor directives code in C for roots of a quadratic equation.
3. Develop a C Program based on conditional directive to display the Distinction, First class and
Second class based on the student mark is above 70, between 60 to 70 and between 40 to 60
respectively otherwise Fail.
4. Summarize of storage classes with respect to various parameters storage location, initial value,
lifetime and linkage
2. Write preprocessor directives code in C for roots of a quadratic equation. (15)
#include <math.h>
#include <stdio.h>
int main() {
double a, b, c, discriminant, root1, root2, realPart, imagPart;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
// condition for real and different roots
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf", root1, root2);
}
// condition for real and equal roots
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2lf;", root1);
}
// if roots are not real
else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart,
imagPart);
}
return 0;
}
3. Developa C Program based on conditional directive to display the Distinction, First class and
Second class based on the student mark is above 70, between 60 to 70 and between 40 to 60
respectively otherwise Fail. (15)
#include<stdio.h>
#define marks 80
void main()
{
#if(marks<0 || marks>100)
{
printf("Wrong Entry");
}
#elif (marks>70)
{
printf("Distinction");
}
#elif (marks<=70 && marks>60)
{
printf("First class ");
}
#elif (marks<=60 && marks>=40)
{
printf("Second class ");
}
#elif(marks<40)
{
printf("Fail");
}
#endif
}
4. Summarize of storage classes with respect to various parameters storage location, initial
value, lifetime and linkage .(15)
Features
Automatic Storage
Class
Register Storage
Class
Static Storage Class
External Storage
Class
Keyword auto register static extern
Initial Value Garbage Garbage Zero Zero
Storage Memory CPU register Memory Memory
Scope
scope limited,local to
block
scope limited,local to
block
scope limited,local to block Global
Life
limited life of
block,where defined
limited life of
block,where defined
value of variable persist
between different function calls
Global,till the
program
execution
Memory
location
Stack Register memory Segment Segment
Example
void main()
{
auto int i;
printf("%d",i);
}
OUTPUT
124
void main()
{
register int i;
for(i=1; i<=5 ;
i++);
printf("%d ",i);
}
OUTPUT
1 2 3 4 5
void add();
void main()
{
add();
add();
}
void add()
{
static int i=1;
printf("n%d",i);
i=i+1;
}
OUTPUT
1
2
void main()
{
extern int i;
printf("%d",i);
int i=5
}
OUTPUT
5
Unit 5 quesn b ans5

Weitere ähnliche Inhalte

Was ist angesagt?

Module 05 Preprocessor and Macros in C
Module 05 Preprocessor and Macros in CModule 05 Preprocessor and Macros in C
Module 05 Preprocessor and Macros in CTushar B Kute
 
PHP 7X New Features
PHP 7X New FeaturesPHP 7X New Features
PHP 7X New FeaturesThanh Tai
 
20 cool features that is in PHP 7, we missed in PHP 5. Let walkthrough with t...
20 cool features that is in PHP 7, we missed in PHP 5. Let walkthrough with t...20 cool features that is in PHP 7, we missed in PHP 5. Let walkthrough with t...
20 cool features that is in PHP 7, we missed in PHP 5. Let walkthrough with t...DrupalMumbai
 
Programming Fundamentals lecture 5
Programming Fundamentals lecture 5Programming Fundamentals lecture 5
Programming Fundamentals lecture 5REHAN IJAZ
 
Basics of c Nisarg Patel
Basics of c Nisarg PatelBasics of c Nisarg Patel
Basics of c Nisarg PatelTechNGyan
 
introduction of c langauge(I unit)
introduction of c langauge(I unit)introduction of c langauge(I unit)
introduction of c langauge(I unit)Prashant Sharma
 
Solid C++ by Example
Solid C++ by ExampleSolid C++ by Example
Solid C++ by ExampleOlve Maudal
 
Part II: LLVM Intermediate Representation
Part II: LLVM Intermediate RepresentationPart II: LLVM Intermediate Representation
Part II: LLVM Intermediate RepresentationWei-Ren Chen
 
Dynamic website
Dynamic websiteDynamic website
Dynamic websitesalissal
 

Was ist angesagt? (20)

Module 05 Preprocessor and Macros in C
Module 05 Preprocessor and Macros in CModule 05 Preprocessor and Macros in C
Module 05 Preprocessor and Macros in C
 
Syntutic
SyntuticSyntutic
Syntutic
 
GCC RTL and Machine Description
GCC RTL and Machine DescriptionGCC RTL and Machine Description
GCC RTL and Machine Description
 
PHP 7X New Features
PHP 7X New FeaturesPHP 7X New Features
PHP 7X New Features
 
Deep C
Deep CDeep C
Deep C
 
Pre processor directives in c
Pre processor directives in cPre processor directives in c
Pre processor directives in c
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 
Preprocessors
Preprocessors Preprocessors
Preprocessors
 
C programming session7
C programming  session7C programming  session7
C programming session7
 
20 cool features that is in PHP 7, we missed in PHP 5. Let walkthrough with t...
20 cool features that is in PHP 7, we missed in PHP 5. Let walkthrough with t...20 cool features that is in PHP 7, we missed in PHP 5. Let walkthrough with t...
20 cool features that is in PHP 7, we missed in PHP 5. Let walkthrough with t...
 
Programming Fundamentals lecture 5
Programming Fundamentals lecture 5Programming Fundamentals lecture 5
Programming Fundamentals lecture 5
 
First session quiz
First session quizFirst session quiz
First session quiz
 
pre processor directives in C
pre processor directives in Cpre processor directives in C
pre processor directives in C
 
Basics of c Nisarg Patel
Basics of c Nisarg PatelBasics of c Nisarg Patel
Basics of c Nisarg Patel
 
Perl intro
Perl introPerl intro
Perl intro
 
introduction of c langauge(I unit)
introduction of c langauge(I unit)introduction of c langauge(I unit)
introduction of c langauge(I unit)
 
Solid C++ by Example
Solid C++ by ExampleSolid C++ by Example
Solid C++ by Example
 
Part II: LLVM Intermediate Representation
Part II: LLVM Intermediate RepresentationPart II: LLVM Intermediate Representation
Part II: LLVM Intermediate Representation
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
Dynamic website
Dynamic websiteDynamic website
Dynamic website
 

Ähnlich wie Unit 5 quesn b ans5

Preprocessor directives in c laguage
Preprocessor directives in c laguagePreprocessor directives in c laguage
Preprocessor directives in c laguageTanmay Modi
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programmingTejaswiB4
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programmingTejaswiB4
 
Data structure week 1
Data structure week 1Data structure week 1
Data structure week 1karmuhtam
 
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptxC-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptxSKUP1
 
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptxC-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptxLECO9
 
1 CMPS 12M Introduction to Data Structures Lab La.docx
1 CMPS 12M Introduction to Data Structures Lab La.docx1 CMPS 12M Introduction to Data Structures Lab La.docx
1 CMPS 12M Introduction to Data Structures Lab La.docxtarifarmarie
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpuDhaval Jalalpara
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in CSyed Mustafa
 

Ähnlich wie Unit 5 quesn b ans5 (20)

Preprocessor directives in c laguage
Preprocessor directives in c laguagePreprocessor directives in c laguage
Preprocessor directives in c laguage
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
C programming session6
C programming  session6C programming  session6
C programming session6
 
ANSI C Macros
ANSI C MacrosANSI C Macros
ANSI C Macros
 
C programming session9 -
C programming  session9 -C programming  session9 -
C programming session9 -
 
Data structure week 1
Data structure week 1Data structure week 1
Data structure week 1
 
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptxC-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
 
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptxC-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
 
1 CMPS 12M Introduction to Data Structures Lab La.docx
1 CMPS 12M Introduction to Data Structures Lab La.docx1 CMPS 12M Introduction to Data Structures Lab La.docx
1 CMPS 12M Introduction to Data Structures Lab La.docx
 
C Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.comC Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.com
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpu
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
 
08 -functions
08  -functions08  -functions
08 -functions
 
CP Handout#2
CP Handout#2CP Handout#2
CP Handout#2
 
C++ Constructs.pptx
C++ Constructs.pptxC++ Constructs.pptx
C++ Constructs.pptx
 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 
FUNCTIONS.pptx
FUNCTIONS.pptxFUNCTIONS.pptx
FUNCTIONS.pptx
 
C structure
C structureC structure
C structure
 

Mehr von Sowri Rajan

Mehr von Sowri Rajan (10)

Unit 5 dwqb ans
Unit 5 dwqb ansUnit 5 dwqb ans
Unit 5 dwqb ans
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
Unit 2
Unit 2Unit 2
Unit 2
 
Unit 1
Unit 1Unit 1
Unit 1
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
 
Unitii string
Unitii stringUnitii string
Unitii string
 
Unitii classnotes
Unitii classnotesUnitii classnotes
Unitii classnotes
 
Uniti classnotes
Uniti classnotesUniti classnotes
Uniti classnotes
 
Program flowchart
Program flowchartProgram flowchart
Program flowchart
 

Kürzlich hochgeladen

Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxleah joy valeriano
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsManeerUddin
 

Kürzlich hochgeladen (20)

Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture hons
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 

Unit 5 quesn b ans5

  • 1. UNIT-V PART – A 1. Define storage class specifier. Storage classes are used to define things like storage location (whether RAM or REGISTER), scope, lifetime and the default value of a variable. 2. Mention different type of storage classes. 1. auto storage class 2. extern storage class 3. static storage class 4. register storage class 3. Distinguish betweenauto and register Storage Class Keyword Memory Location Default Value Scope Life Time Automatic auto Computer Memory (RAM) Garbage Value Local to the block in which the variable has defined Till the control remains within the block in which variable is defined Register register CPU Register Garbage Value Local to the block in which the variable has defined Till the control remains within the block in which variable is defined 4. What is meant by extern? Give an example. The default storage class of all global variables (variables declared outside function) is external storage class. Example #include<stdio.h> #include<conio.h>
  • 2. int i; //By default it is extern variable int main() { printf("%d",i); return 0; } 5. What is storage class for variable A in below code?Justify. int main() { int A; A = 10; printf("%d", A); return 0; } The default storage class of all local variables (variables declared inside block or function) is auto storage class. 6. What will the SWAP macro in the following program be expanded to on preprocessing? will the codecompile? #include<stdio.h> #define SWAP(a, b, c)(c t; t=a, a=b, b=t) int main() { int x=10, y=20; SWAP(x, y, int); printf("%d %dn", x, y); return 0; } 7. How are preprocessordirectives written in C? C program is compiled in a compiler, source codeis processedbya program called preprocessor. This process is called preprocessing. Commands used in preprocessor are called preprocessordirectives and they begin with “#”symbol.
  • 3. 8. How can you avoid including a header more than once? One easy technique to avoid multiple inclusions of the same header is to use the #ifndef and #define preprocessor directives. When you create a header for your program, you can #define a symbolic name that is unique to that header. 9. How to pragma directive works? These directives helps us to specify the functions that are needed to run before program startup( before the controlpasses to main()) and just before program exit (just before the control returns from main()). 10.Compare the pragma and conditional directive. pragma directive conditional directive Syntax: #pragma #ifdef, #endif, #if, #else, #ifndef Definition #Pragma is used to call a function before and after main function in a C program. Set of commands are included or excluded in sourceprogram before compilation with respect to the condition. 11.Examine the six pragma directives. #pragma startup, #pragma exit, #pragma warn, #pragma GCC dependency, #pragma GCC system_header and #pragma GCC poison 12.Write the syntax of pragma directive. #pragma token_name 13.Is there any difference that arises if double quotes , instead of angular brackets are used for including the standard header file? When include file in <> then it searches the specified file system directory.Ex. #include<stdio.h>It will search stdio. h file in system directory. When including file in “” Then it search this specified file in currently working directory.
  • 4. 14.Identify the use of pragma directive in c. The preprocessordirective #pragma is used to provide the additional information to the compiler in C language. 15.List out the seven conditional directives in c. #if, #ifdef, #ifndef, #else, #elif, #endif, and defined 16.What is the use of #if directive? “#ifdef” directive checks whether particular macro is defined or not. If it is defined, “If” clause statements are included in source file. Otherwise, “else” clause statements are included in source file for compilation and execution. 17.Write a note on define macro. This macro defines constant value and can be any of the basic data types. There are two types of macros: (i) Object-like Macros (ii) Function-like Macros 18.Evaluate the advantages of a macro over a function. When writing macros for functions, they saves a lot of time that is spent by the compiler for invoking / calling the functions. Hence, The advantage of a macro over an actual function, is speed. No time is taken up in passing control to a new function, becausecontrol never leaves the home function 19.Develop an example for conditional compilation. #include <stdio.h> #define X 45.25 int main() { #if !X printf("Hello"); #else printf("World"); #endif
  • 5. return 0; } Output: main.c: In function ‘main’: main.c:2:11: error: floating constant in preprocessorexpression #define X 45.25 ^ main.c:5:10: note: in expansion of macro ‘X’ #if ! 20.What does #undef, #pragma indicate in c? The #pragma preprocessordirective is used to provide additional information to the compiler. The #pragma directive is used by the compiler to offer machine or operating- system feature PART – B 1. Describe about the various storage classes with example program.(13) C provides four storage classes as: 1. auto storage class 2. register storage 3. extern storage class 4. static storage class  Storage class determines its storage duration, scope and linkage.  Storage duration is the period during which that identifier exists in the memory.  Scope is where the identifier can be referenced in a program.  Linkage determines for a multiple-source file. Summarised all storage classes in C as following as: Features Automatic Storage Class Register Storage Class Static Storage Class External Storage Class Keyword auto register static extern Initial Value Garbage Garbage Zero Zero Storage Memory CPU register Memory Memory Scope scope limited,local to block scope limited,local to block scope limited,local to block Global Life limited life of block,where defined limited life of block,where defined value of variable persist between different function calls Global,till the program execution
  • 6. 2. Distinguish between the following macro. (i)object like macro(7) The preprocessor (i.e. translator that processes the source code before it is given to the compiler) is controlled by directives known as preprocessor directives. A preprocessor directive consists of various preprocessing tokens (i.e. smallest indivisible elements of C language) and begins with a # (pound) symbol. The various preprocessor directives available in C language are: Macro Replacement Directive (#define, #undef): A macro is a facility provided by the C preprocessor, by which a token can be replaced by the user-defined sequence of characters. The types of macros are: Object-like Macros: An object-like macro is also known as a symbolic constant. It is defined as: #define macro-name replacement-list Example: #define PI 3.142 (ii)function like macro(6). Function-like Macros: A macro with arguments is called a function-like macro. It is defined as: #define macro-name(parameter-list) replacement-list Memory location Stack Register memory Segment Segment Example void main() { auto int i; printf("%d",i); } OUTPUT 124 void main() { register int i; for(i=1; i<=5 ; i++); printf("%d ",i); } OUTPUT 1 2 3 4 5 void add(); void main() { add(); add(); } void add() { static int i=1; printf("n%d",i); i=i+1; } OUTPUT 1 2 void main() { extern int i; printf("%d",i); int i=5 } OUTPUT 5
  • 7. Example: #define SQR(x) (x*x) During the preprocessing stage, the macro names are expanded and are replaced by their replacement lists. This process is known as macro expansion. Predefined Macros: The macros that are already defined in C language are known as predefined macros. Some of the predefined macros are: a. __FILE__: This macro expands to the name of the current file in the form of a string constant. b. __LINE__: The __LINE__ macro expands to the current line number in the source file. c. __DATE__: The __DATE__ macro expands to the compilation date of the source file in the form of a string constant of the form “Mmm dd yyyy”. Example: Nov 27 2014. d. __TIME__: This macro expands to the time at which the C preprocessor is being invoked in the form of a string constant of the form “hh:mm:ss”. Example: 19:57:05. undef Directive: The undef directive causes the specified identifier to be no longer defined as a macro name. The general form is: #undef identifer Example: #undef PI 3. Illustrate and explain about unconditional preprocessor directive.(13) The unconditional directives are: #include - Inserts a particular header from another file – used to include the header files (e.g. stdio.h, math.h, string.h, etc.) that contain predefined library functions to carry out specific tasks. Example: #include<stdio.h> #include<stdio.h> // Preprocessor directive to include the standard input / output header file. main ( ) // Mandatory function { printf(“Welcome to C Programming…”); // Function body containing only one executable statement. } #define - Defines a preprocessor macro – used to define symbolic constants. Example: #define PI 3.1429 #undef - Undefines a preprocessor macro The undef directive causes the specified identifier to be no longer defined as a macro name. The general form is: #undef identifer Example: #undef PI 4. Explain about #if, #else ,#elif directive with an example program.(13)
  • 8. These directives are used for conditional compilation. Conditional compilation means that a part of a program is compiled only if a condition evaluates to true. EXAMPLE PROGRAM FOR #IF & #ELSE IN C: #include <stdio.h> #define a 100 int main() { #if (a==100) printf("This line will be added in this C file since " "a = 100n"); #else printf("This line will be added in this C file since " "a is not equal to 100n"); #endif return 0; } #elifdirective The preprocessor will include the C source code that follows the #elif statement when the condition of the preceding #if, #ifdef or #ifndef directive evaluates to false and the #elif condition evaluates to true. Syntax The syntax for the #elif directive in the C language is: #elif conditional_expression conditional_expression Expression that must evaluate to true for the preprocessor to include the C source code into the compiled application. The #elif directive must be closed by an #endif directive. Example The following example shows how to use the #elif directive in the C language: /* Example using #elif directive by TechOnTheNet.com */ #include <stdio.h> #define YEARS_OLD 12 int main() { #if YEARS_OLD <= 10 printf("TechOnTheNet is a great resource.n");
  • 9. #elif YEARS_OLD > 10 printf("TechOnTheNet is over %d years old.n", YEARS_OLD); #endif return 0; } In this example, YEARS_OLD has a value of 12 so the statement #if YEARS_OLD <=10 evaluates to false. As a result, processing is passed to the #elif YEARS_OLD > 10 statement which evaluates to true. The C source code following the #elif statement is then compiled into the application. Here is the output of the executable program: TechOnTheNet is over 12 years old. 5. Describe the defined operator and #error directive .(13) The defined Operator Another way to verify that a macro is defined is to use the defined unary operator. The defined operator has one of the following forms: defined name defined (name) An expression of this form evaluates to 1 if name is defined and to 0 if it is not. The defined operator is especially useful for checking many macros with just a single use of the #if directive. In this way, you can check for macro definitions in one concise line without having to use many #ifdef or #ifndef directives. For example, consider the following macro checks: #ifdef macro1 printf( "Hello!n" ); #endif #ifndef macro2 printf( "Hello!n" ); #endif #ifdef macro3 printf( "Hello!n" ); #endif Another use of the defined operator is in a single #if directive to perform similar macro checks: #if defined (macro1) || !defined (macro2) || defined (macro3) printf( "Hello!n" ); #endif Error Directive (#error): The error directive causes the preprocessor to generate the customized diagnostic messages and causes the compilation to fail.
  • 10. The #error directive causes preprocessing to stop at the location where the directive is encountered. Information following the #error directive is output as a message prior to stopping preprocessing. Syntax The syntax for the #error directive in the C language is: #error message message Message to output prior to stopping preprocessing. Example Let's look at how to use #error directives in your C program. The following example shows the output of the #error directive: /* Example using #error directive by TechOnTheNet.com */ #include <stdio.h> #include <limits.h> /* * Calculate the number of milliseconds for the provided age in years * Milliseconds = age in years * 365 days/year * 24 hours/day, 60 minutes/hour, 60 seconds/min, 1000 millisconds/sec */ #define MILLISECONDS(age) (age * 365 * 24 * 60 * 60 * 1000) int main() { /* The age of TechOnTheNet in milliseconds */ int age; #if INT_MAX < MILLISECONDS(12) #error Integer size cannot hold our age in milliseconds #endif /* Calculate the number of milliseconds in 12 years */ age = MILLISECONDS(12); printf("TechOnTheNet is %d milliseconds oldn", age); return 0; } 6. Define a macro in C to check whether a given three-digit number is an Armstrong number or odd. Illustrate the use of this macro in a program. (13) #include <stdio.h> #define arms(x) (x*x*x) int main() { int num, originalNum, remainder, result = 0;
  • 11. printf("Enter a three-digit integer: "); scanf("%d", &num); originalNum = num; while (originalNum != 0) { remainder = originalNum % 10; result += arms(remainder); originalNum /= 10; } if (result == num) printf("%d is an Armstrong number.", num); else if (num%2!=0) printf("%d is odd number.", num); return 0; } 7. Write about conditional preprocessor directive with an example.(13) The conditional directives are: #ifdef - If this macro is defined #ifndef - If this macro is not defined #if - Test if a compile time condition is true #else - The alternative for #if #elif - #else an #if in one statement #endif - End preprocessor conditional These directives are used for conditional compilation. Conditional compilation means that a part of a program is compiled only if a condition evaluates to true. EXAMPLE PROGRAM FOR #IF & #ELSE IN C: #include <stdio.h> #define a 100 int main() { #if (a==100) printf("This line will be added in this C file since " "a = 100n"); #else
  • 12. printf("This line will be added in this C file since " "a is not equal to 100n"); #endif return 0; } #elifdirective The preprocessor will include the C source code that follows the #elif statement when the condition of the preceding #if, #ifdef or #ifndef directive evaluates to false and the #elif condition evaluates to true. Syntax The syntax for the #elif directive in the C language is: #elif conditional_expression conditional_expression Expression that must evaluate to true for the preprocessor to include the C source code into the compiled application. The #elif directive must be closed by an #endif directive. Example The following example shows how to use the #elif directive in the C language: /* Example using #elif directive by TechOnTheNet.com */ #include <stdio.h> #define YEARS_OLD 12 int main() { #if YEARS_OLD <= 10 printf("TechOnTheNet is a great resource.n"); #elif YEARS_OLD > 10 printf("TechOnTheNet is over %d years old.n", YEARS_OLD); #endif return 0; } For example, consider the following macro checks: #ifdef macro1 printf( "Hello!n" );
  • 13. #endif #ifndef macro2 printf( "Hello!n" ); #endif #ifdef macro3 printf( "Hello!n" ); #endif Another use of the defined operator is in a single #if directive to perform similar macro checks: #if defined (macro1) || !defined (macro2) || defined (macro3) printf( "Hello!n" ); #endif 8. Define a macro in C to check whether a given number is evenor odd. Illustrate the use of this macro in a program. (13) #include <stdio.h> #define ISEVEN(n) ((n%2 == 0) ? 1 : 0) #define ISODD(n) ((n%2 != 0) ? 1 : 0) int main(void) { int number; printf("Enter an integer number: "); scanf("%d",&number); if(ISEVEN(number)) printf("%d is an EVEN numbern",number); else if(ISODD(number)) printf("%d is an ODD numbern",number); else printf("An Invalid Inputn"); return 0; } 9. Write about all the pragma directive and explain in detail.(13) The #pragma preprocessor directive is used to provide additional information to the compiler. The #pragma directive is used by the compiler to offer machine or operating-system feature.
  • 14. The pragma directive configures some of the compiler options that can otherwise be configured from the command line. An unrecognized pragma directive is ignored without an error or a warning message. The pragma directive is written as: #pragma token-sequence The commonly used forms of the pragma directive are: a. #pragma option: It is written as #pragma option [options…]. Example: #pragma option –C Allows nested comments. #pragma option –C– Does not allow the nesting of comments. b. #pragma warn: It can be used to turn on, off or toggle the state of warnings. It can be written as: #pragma warn +www (Turns on the warning with character code www) #pragma warn –www (Turns off the warning with character code www) #pragma warn .www (Toggles the state of warning with character code www) Example:#pragma warn –dup Suppresses the warning ‘Redefinition of macro is not identifal’. #pragma warn –aus Suppresses warning ‘Identifier is assigned value that is never used’. c. #pragma startup and #pragma exit: These directives can be used to execute a function before and after the execution of the function main. These directives can be written as: Example: #pragma startup function-name #pragma exit function-name #include<stdio.h> #include<conio.h> void School(); void College() ; #pragma startup School 105 #pragma startup College #pragma exit College #pragma exit School 105 void main(){ printf("nI am in main"); getch(); } void School(){ printf("nI am in School"); getch(); } void College(){ printf("nI am in College"); getch(); }
  • 15. Output : I am in College I am in School I am in main I am in School I am in College 10. Write the C coding for finding the average of number using any of the storage class declarations.(13) #include<stdio.h> int main() { printf("nnttStudytonight - Best place to learnnnn"); int n, i; /* auto */ float sum = 0, x; printf("Enter number of elements: "); scanf("%d", &n); printf("nnnEnter %d elementsnn", n); for(i = 0; i < n; i++) { scanf("%f", &x); sum += x; } printf("nnnAverage of the entered numbers is = %f", (sum/n)); printf("nnnntttCoding is Fun !nnn"); return 0; } 11. Explain the various ways in which a source file inclusion directive can be written.(13) The source file inclusion directive tells the preprocessor to replace the directive with the content of the file specified in the directive. This directive is generally used to include the header files, which contain the prototypes of the library functions and the definitions of the predefined constants. The include directive can be written as: a. #include <name-of-file> : Searches the prespecified list of directories for the source file whose name is given within angular brackets, and text embeds the entire content of the source file in place of itself. If the file is not found there, it will show the error “Unable to include ‘name-of-file’”.
  • 16. b. #include “name-of-file” : First searches the file in the current working directory. If the search fails, the search will be carried out in the prespecified list of directories. If the search still fails, it will show the error “Unable to include ‘name-of-file’”. Example: #include <stdio.h> #include<stdio.h> // Preprocessor directive to include the standard input / output header file. main ( ) // Mandatory function { printf(“Welcome to C Programming…”); // Function body containing only one executable statement. } 12. Examine the various stages a program undergoes before execution.(13) Pre-processing, Compiling, Linking, Loading Execution of a C/C++ program involves four stages using different compiling/execution tool, these tools are set of programs which help to complete the C/C++ program's execution process. 1. Preprocessor 2. Compiler 3. Linker 4. Loader These tools make the program running. 1) Preprocessor This is the first stage of any C/C++ program execution process; in this stage Preprocessor processes the program before compilation. Preprocessor include header files, expand the Macros. 2) Complier This is the second stage of any C/C++ program execution process, in this stage generated output file after preprocessing ( with source code) will be passed to the compiler for compilation. Complier will compile the program, checks the errors and generates the object file (this object file contains assembly code). 3) Linker This is the third stage of any C/C++ program execution process, in this stage Linker links the more than one object files or libraries and generates the executable file. 4) Loader This is the fourth or final stage of any C/C++ program execution process, in this stage Loader loads the executable file into the main/primary memory. And program run. Different files during the process of execution Suppose, you save a C program with prg1.c – here .c is the extension of C code, prg1.c file contains the program (source code of a C program). Preprocessor reads the file and generates the prg1.i
  • 17. (prg1.ii – for c++ source code) file, this file contains the preprocessed code. Compiler reads the prg1.i file and further converts into assembly code and generates prg1.s and then finally generates object code in prg1.o file. Linker reads prg1.o file and links the other assembly/object code or library files and generates executable file named prg1.exe. Loader loads the prg1.exe file into the main/primary memory and finally program run. One more file is created that contains the source code named prg1.bak; it’s a backup file of the program files. 13. Write a C Program to calculate the factorial of a number by using the keyword static.(13) #include<stdio.h> void main() {
  • 18. int num,i,f=1; clrscr(); printf("Enter the a Num : "); scanf("%d",&num); printf("Factorial of %d = %d", n, fact(num)); getch(); } fact(static int n) { int f=1; if(n<=1) return 1; else { f=n*fact(n-1); return f; } } 14. Write a C Program to generate Fibonacci series by using keyword auto.(13) #include<stdio.h> int f(int); int main() { int n, i = 0, c; scanf("%d", &n); printf("Fibonacci series terms are:n"); for (c = 1; c <= n; c++) { printf("%dn", f(i)); i++; }
  • 19. return 0; } int f( auto int n) { if (n == 0 || n == 1) return n; else return (f(n-1) + f(n-2)); } PART C 1. Write any two common macro pitfalls with example program. (i) Misnesting When a macro is called with arguments, the arguments are substituted into the macro body and the result is checked, together with the rest of the input file, for more macro calls. It is possible to piece together a macro call coming partially from the macro body and partially from the arguments. For example, #define twice(x) (2*(x)) #define call_with_1(x) x(1) call_with_1 (twice) ==> twice(1) ==> (2*(1)) Macro definitions do not have to have balanced parentheses. By writing an unbalanced open parenthesis in a macro body, it is possible to create a macro call that begins inside the macro body but ends outside of it. For example, #define strange(file) fprintf (file, "%s %d", … strange(stderr) p, 35) ==> fprintf (stderr, "%s %d", p, 35) The ability to piece together a macro call can be useful, but the use of unbalanced open parentheses in a macro body is just confusing, and should be avoided. (ii) Operator Precedence Problems You may have noticed that in most of the macro definition examples shown above, each occurrence of a macro argument name had parentheses around it. In addition, another pair of parentheses usually surround the entire macro definition. Here is why it is best to write macros that way. Suppose you define a macro as follows,
  • 20. #define ceil_div(x, y) (x + y - 1) / y whose purpose is to divide, rounding up. (One use for this operation is to compute how many int objects are needed to hold a certain number of char objects.) Then suppose it is used as follows: a = ceil_div (b & c, sizeof (int)); ==> a = (b & c + sizeof (int) - 1) / sizeof (int); his does not do what is intended. The operator-precedence rules of C make it equivalent to this: a = (b & (c + sizeof (int) - 1)) / sizeof (int); 2. Write preprocessor directives code in C for roots of a quadratic equation. 3. Develop a C Program based on conditional directive to display the Distinction, First class and Second class based on the student mark is above 70, between 60 to 70 and between 40 to 60 respectively otherwise Fail. 4. Summarize of storage classes with respect to various parameters storage location, initial value, lifetime and linkage 2. Write preprocessor directives code in C for roots of a quadratic equation. (15) #include <math.h> #include <stdio.h> int main() { double a, b, c, discriminant, root1, root2, realPart, imagPart; printf("Enter coefficients a, b and c: "); scanf("%lf %lf %lf", &a, &b, &c); discriminant = b * b - 4 * a * c; // condition for real and different roots if (discriminant > 0) { root1 = (-b + sqrt(discriminant)) / (2 * a); root2 = (-b - sqrt(discriminant)) / (2 * a); printf("root1 = %.2lf and root2 = %.2lf", root1, root2); }
  • 21. // condition for real and equal roots else if (discriminant == 0) { root1 = root2 = -b / (2 * a); printf("root1 = root2 = %.2lf;", root1); } // if roots are not real else { realPart = -b / (2 * a); imagPart = sqrt(-discriminant) / (2 * a); printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart, imagPart); } return 0; } 3. Developa C Program based on conditional directive to display the Distinction, First class and Second class based on the student mark is above 70, between 60 to 70 and between 40 to 60 respectively otherwise Fail. (15) #include<stdio.h> #define marks 80 void main() { #if(marks<0 || marks>100) { printf("Wrong Entry"); } #elif (marks>70) { printf("Distinction"); } #elif (marks<=70 && marks>60) { printf("First class "); } #elif (marks<=60 && marks>=40) { printf("Second class "); } #elif(marks<40) { printf("Fail"); } #endif }
  • 22. 4. Summarize of storage classes with respect to various parameters storage location, initial value, lifetime and linkage .(15) Features Automatic Storage Class Register Storage Class Static Storage Class External Storage Class Keyword auto register static extern Initial Value Garbage Garbage Zero Zero Storage Memory CPU register Memory Memory Scope scope limited,local to block scope limited,local to block scope limited,local to block Global Life limited life of block,where defined limited life of block,where defined value of variable persist between different function calls Global,till the program execution Memory location Stack Register memory Segment Segment Example void main() { auto int i; printf("%d",i); } OUTPUT 124 void main() { register int i; for(i=1; i<=5 ; i++); printf("%d ",i); } OUTPUT 1 2 3 4 5 void add(); void main() { add(); add(); } void add() { static int i=1; printf("n%d",i); i=i+1; } OUTPUT 1 2 void main() { extern int i; printf("%d",i); int i=5 } OUTPUT 5