Final Programming in C

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 325

Programming in C

Computer Basics

Computer: It is a electronic device which is used to


perform calculations or computations and it is invented
by charles babbage in 1822.
Software: Set of programs is called software.
Hardware: Physical and replaceable part of a system is
called hardware.
• Input Devices: Mouse, keyboard, joystick, stylus pen
etc.
• Output Devices: Monitor, printer, plotter etc
• Primary storage : Primary memory is the main memory
and stores data temporarily.
• Secondary memory: It is the external memory and
stores data permanently.
History of C
Contd..
• The root of all modern languages is ALGOL Introduced in the early
1960s.
• In 1967, Martin Richards developed a language called Basic
Combined Programming Language (BCPL)
• In 1970, Ken Thompson created a language using many features of
BCPL and called it simply B.
• Both BCPL and B were “typeless” system programming languages.
• In 1972 C was evolved from ALGOL, BCPL and B by Dennis Ritchie at
bell laboratories of AT&T (American Telephone & Telegraph),
located in the U.S.A.
Contd..
• C uses many concepts from these languages and added the
concept of data types and other powerful features.
• In 1978 K & RC was developed by Brian Kerningham Dennis
Ritchie where new features like long int, unsigned int and
compound assignment operator were introduced.
• In 1983 American National Standards Institute (ANSI)
appointed a technical committee to define a standard for C.
• In December 1989 the committee approved a version of C
which is known as ANSI C.
Contd..
• It was then approved by the International Organization for
Standardization(ISO) in 1990.
• This version of C is also referred to as C89 or ANSI/ISO C
which includes new keywords such as volatile , constant.
Type specifier called “void” and also introduced multi line
comments.
• In 1999 Standardization committee approved new version of
C called C99 which includes features like new data types
such as bool, complex and imaginary.
• It also includes new header files such as stdint.h and
complex.h and also introduced single line comments.
• In 2011 Standardization committee approved new version of
C called C11 which contains anonymous structures.
Programming Paradigm
• Monolithic programming
Entire program is written in single module.
• Structured programming
In SP, program is divided into small parts called
functions and each part performs a specific task,
should maintain high cohesion and low coupling.
• Object Oriented Programming
In OOP programs will be developed with
classes and objects.
• Structured Programming can solve moderately
complex programs.(bcz of lack of OOP)
• Structured Programming provides less reusability,
more function dependency.
• Three features of structured programming language
includes:
 Sequence(Block { })
 Selection(Conditional Statements)
 Iteration (Loops)
• C follows both sequential and control flow of
execution.
• Control flow is the order in which individual
statements or instructions of a program are executed.
Computer Languages
They are classified as
• Low-Level Language
• Machine Level
• Assembly Level
• High-Level Language
1. Low-Level Language:
a) Machine Level: It is the first generation programming
language which contains only 0’s and 1’s.
Pros: Computer can understand directly.
Cons: i) Very difficult to remember the code
ii) It is machine dependent
iii) User can’t modify and debug the program
iv) It is suitable for simple applications
Contd..
2. Assembly Language:
It is the second generation programming
language. It uses symbols or mnemonics to
represent various instructions.
Pros:
i) User can modify the programs and can debug.
ii) User can remember the mnemonics.
iii) Suitable for simple applications.
Cons:
i) Translator called Assembler is required to translate
the assembly level code to machine level code.
Contd..
3. High-Level Language:
It is the third generation programming
language. It is based on English grammatical
notations and mathematical formulas.
Pros:
i) Easy to modify and debug.
ii) Suitable for complex applications.
Cons:
i) Translator called compiler or interpreter is
required to translate the high level code to
machine level code.
Introduction to C
• C was invented by Dennis Ritchie in the year
1972.
• C is a High Level Language.
• C is a structured programming language.
• Structured programming follows top-down
approach.
• Structured Programming is also known as
Modular Programming.
• Top down analysis for problem solving: It
focuses on dividing the problem into sub parts
and hence simplifies the problem solving.
Creating and Running Programs
Structure of a C program

Preprocessor Directives
Global
Dives Declarations

Void main()
{
Local Declarations
Statements
}
Other Functions if any
Programs
1. Write a C program to print “Hello world”.
2. Write a C program to perform addition of two numbers.
3. Write a C program to find the area of square a2.
4. Write a C program to find the area of rectangle l*b.
5. Write a C program to find the area of circle pi*r2.
6. Write a C program to find the area of triangle ½*b*h.
7. Write a C program to find the perimeter of square 4*a.
8. Write a C program to find the perimeter of circle 2*pi*r.
9. Write a C program to find the perimeter of cylinder
pi*r2*h.
10. Write a C program to find the perimeter of sphere
(4/3)*pi*r3.
Contd..
11. Write a C program to calculate the simple interest and
final amount. (hint:(p*t*r)/100)
12. To enter marks of five subjects and calculate total and
average.
13. Calculate Net salary of employee by taking Basic as
input with DA=65%, HRA=10%, PF=2000.
14. Write a C program to convert Miles to Kms.
15. Write a C program to convert dollar to Indian rupee.
16. Write a C program to calculate altitude of the Isosceles
Triangle √(a2 − b2/4).
17. Write a C program to perform swapping of two numbers
with using third variable.
18. Write a C program to perform swapping of two numbers
without using third variable.
Contd..
19. Write a python program to generate random numbers.
20. Write a python program to convert Celsius to Fahrenheit.
21. Write a python program to calculate area of equilateral
triangle (√3/4)a2
In Structured Programming, Programs are divided into small self
contained functions.
Identifier
Identifier is a common name given to
• variables
• constants
• functions
• Structures
• unions
Rules of a Identifier

• The only valid symbols are upper case


letters(A to Z), lower case letters (a to z), digits
(0-9) and the special symbol called (_)
• The first character of the identifier cannot be a
digit.
• The name of the identifier cannot be a
keyword.
• Identifiers should not have special characters
like space, operators or special symbols.
Key words in C
• Auto double int struct
• Break else long switch
• Case enum register typedef
• Char extern return union
• Continue for signed void
• do if static while
• Default goto sizeof volatile
• Const float short unsigned
Data Types
Contd..

Derived Data Types:


• The derived data types are basically derived out of the
fundamental/ basic data types.
• A derived data type won’t typically create a new data
type – but would add various new functionalities to the
existing ones instead.
User Defined Data Types:
• A user-defined data type (UDT) is a data type that
derived from an existing data type.
• You can use UDTs to extend the built-in types already
available and create your own customized data types.
Basic data types
Types Size(bytes) Range
• signed char 1 -128 to 127
• unsigned char 1 0 to 255
• signed int 2 -32768 to
32767
• unsigned int 2 0 to 65536
• signed short int 1 -128 to 127
• unsigned short int 1 0 to 255
Contd..
Types Size(bytes) Range
• signed long int 4 -2,147,483,648
to 2,147,483,647
• unsigned long int 4 0 to 4,294,967,295
• float 4 1.2E-38 to 3.4E+38
6 decimal places
• double 8 2.3E-308 to 1.7E+308
15 decimal places
• long double 10 3.4E-4932 to
1.1E+4932
19 decimal places
Operators
 An operator is a symbol that tells the compiler to
perform specific mathematical or logical
manipulations.
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Increment and Decrement
• Assignment Operators
• Bitwise Operators
• Conditional operators
• Special operators
Arithmetic Operators

 Arithmetic operators are used to perform arithmetic


operations between two operands.
Operator Description Example
+ Adds two operands A + B will give 30
- Subtracts two operands A - B will give -10
* Multiplies both operands A * B will give 200
/ Divides both operands B / A will give 2
% Modulo Division B % A will give 0
Increment and Decrement

• The decrement (–) and increment (++) operators


are special types of operators used in programming
languages to decrement and increment the value of the
given variable by 1 (one).

++ Increments A++ will give 11


-- Decrements A-- will give 9
Relational Operators

 Relational operators are used to compare the values of the


two operands and it returns Boolean values as output.
• Assume variable A holds 10 and variable B holds 20,
then:
• (A == B) is not true.
• (A != B) is true.
• (A > B) is not true.
• (A < B) is true.
• (A >= B) is not true.
• (A <= B) is true.
Conditional operators

•The conditional operator is also known as a ternary


operator.
Logical Operators

•Logical operators are used to combine conditional statements.


(in other words it is used to construct compound conditions)
Example Return Value
5>3 && 5<10 1
8>5 || 8<2 1
8!=8 0
Assignment Operators

• The assignment operators are used to assign the value of the right
expression to the left operand.
(Simple Assignment Operator: = (Equals to))
• C = A + B will assign value of A + B into C

(Compound Assignment Operators: +=, -=, *=, /=, %=, <<=, >>=)
• C += A is equivalent to C = C + A
• C -= A is equivalent to C = C – A
• C *= A is equivalent to C = C * A
• C /= A is equivalent to C = C / A
• C %= A is equivalent to C = C % A
• C <<= 2 is same as C = C << 2
• C >>= 2 is same as C = C >> 2
Bitwise Operators

•The bitwise operators are the operators used to perform


the operations on the data at the bit-level.
Special Operators

• The Comma Operator

• Type cast Operator

• Reference operator or Address Operator ("&")

• Dereference operator ("*") or Pointer Operator

• Double Pointer operator ("**")

• sizeof operator
Branching( one way selection)
Two way selection (if-else)
Nested if -else
Multi Way Selection (else-if ladder)
Switch
Loops
• There are 3 loops in C

for
while
do-while
for loop
While loop
do -while
Syntax:
initialization;
do
{
statements;
updation;
}while(condition);
do-while
Arrays in C

• Arrays has been classified in to three types

1. One Dimensional Arrays


2. Two Dimensional Arrays
3. Multi Dimensional Arrays
Arrays
• An array is a collective name given to a group of similar elements.
• Each variable in an array is called an array element.
• All the elements are of same type, but may contain different values.
• The entire array is contiguously stored in memory.
• The position of each array element is known as array index or
subscript.
• An array can either be one dimensional (1-D) or two dimensional (2-D)
or Multi-dimensional .
• An integer 1-D array looks like this:
Declaring a 1-D Array
• Syntax:
data-type arrayname[size];
• Example:

int EmployeeNumbers[6];

float Salary[6];

• The array index starts with zero.

• The valid array indexes for the above declared array is 0 to 5.


Declaring and Initializing arrays
• Arrays can be initialized as they are declared.
• Example:
int aiEmployeeNumbers[] = {15090, 15091, 15092,
15093,15094, 15095};
• The size in the above case is optional and it is automatically computed.
• In the above example, size of the array is 6 and it occupies 6 * 4 = 24 bytes (6
is the size of the array and 4 is the number of bytes required to store one
integer on Windows platform)

5
1509
1 5094
3
2 1509
1 1509
0 1509
1509 5
4
3
2
1
0
2-D Arrays
• A 2-D array is used to store tabular data in terms of rows and columns.

• A 2-D array should be declared by specifying the row size and the column size.

• Example: Consider a 3 x 2 array with the elements labeled by row and column
• For an array of dimension n rows and m columns, the total number of
elements is equal to m x n.
• To access the individual elements, the row and the column should be supplied
2-D Arrays – Graphical Representation
Graphically, integer array of size 3x4 can be depicted as follows:
Initializing 2-D Arrays (1 of 2)
• A 2-D array can be initialized as given below:
int aiEmployeeInfo[3][2]= {101,1,102,1,103,2};
or
int aiEmployeeInfo [3][2]={ {101,1},
{102,1},
{103,2}
};
In the above declaration,
– 101, 102 and 103 refers to Employee Id’s and they are stored in [0][0], [1][0] and [2][0] positions
– 1,1 and 2 refers to Job Codes and they are stored in [0][1], [1][1] and [2][1] positions

int aiEmployeeInfo [3][2]={ {101,1},{102,1},{103,2}


};

aiEmployeeInfor[0][0] aiEmployeeInfor[1][1] aiEmployeeInfor[2][0]


Initializing 2-D Arrays (2 of 2)
• A 2-D array can be declared without specifying the row size if it is initialized

• Example:
int aiEmployeeInfo[][3]=

{101,1,102,1,103,2};
Since there are six initial values and the column size is 3, the number of rows is taken as 2

• If the column size is not supplied then there will


be a compilation error.
Strings
• A group of characters (Alphabets, digits and special
characters) is called as a string.
• A string has a terminator character which indicates the end of the
string.
• The terminator character is represented by ‘\0’
• Memory Space should be allocated to store ‘\0’ as part of the
string.
• A null character (\0) occupies 1 byte of memory.
• Strings are enclosed in “ “(double quotes)
• Example:
“My Training”
“Programming Fundamentals”
Declaration of Strings
Syntax:
char variablename
[Number_of_characters];

• Example:
char acEmployeeName[20];

Here 20 implies that the maximum number of characters


can be 19 and one position is reserved for ‘\0’

Since a character occupies one byte, the above array


occupies 20 bytes (19 bytes for the employee name and
one byte for ‘\0’)
Static initialization of Strings (1 of 2)
• char acItemCategory[15]=“Greeting Cards”;
In the above declaration, the size of the array is 15 bytes and the length of the
string is the number of characters in the string, i.e., 14 and one extra space
for ‘\0’

• char acItemCategory[15]=“Ornaments” ;
Here the size is more than the number of characters which is valid

• char acItemCategory[]=“Groceries”;
Here the size of the array is computed automatically which is 10. Total number
of characters in the string is 9 and 1 for ‘\0’;

• char *pcStr=“Programming Basics”;


A pointer can be declared and initialized to a string as mentioned above.
Static initialization of Strings (2 of 2)

• char acItemCategory[3]=“Books”;
Here the size specified is 3. But the number of
characters in the string is 5. This is invalid

• char acItemCategory[ ]=

{'s','t','a','t','i','o','n','a','r'
,'y','\0'};
Here the character constants are supplied to initialize
the string.
Storage of strings in memory
acItemCategory

66(B) 8000
111(o) 8001
111(o) 8002
107(k) 8003
char acItemCategory[15]= “Books”; 115(s) 8004
0(\0) 8005
Garbage value 8006
Garbage value 8007
Garbage value 8008
Garbage value 8009
Garbage value 800A
Garbage value 800B
Garbage value 800C
Garbage value 800D
Garbage value 800E
Input of Strings – scanf and gets
scanf function:
char acItemCategory[50];
scanf(“%s”, acItemCategory);

• acItemCategory is declared as a character array of size 50


• It is to be used as a string storing a maximum of 49 characters with a ‘\
0’
• The scanf function with %s is used to accept the string in one line
instead of accepting character by character in a loop
• The scanf with %s accepts characters till a whitespace(space,tab
space,new line) is encountered
• scanf with %s cannot be used to accept strings containing spaces
gets function:
gets(acItemCategory);
gets(&acItemCategory[0]);
• This is an unformatted function to read strings
• This can be used to accept strings with spaces
Reading and Printing Strings - Example

/*Program to accept Item Category and display*/


int main(int argc, char **argv) {
char acItemCategory[50];

printf("Enter the category code ");


scanf("%s",acItemCategory);

printf("The given Item Category is %s",


acItemCategory);

return 0;
}
String Handling functions
• The following are the string functions that are supported by C
strlen() strcpy() strcat() strcmp()
strcmpi() strncpy()strncat() strncmp()
strnicmp()
• These functions are defined in string.h header file
• All these functions make use of the terminator character (‘\0’)
present in strings
strlen() Function (1 of 2)

• strlen() function is used to count the number of characters in the string.

• Counts all the characters excluding the null character ‘\0’

• Syntax:
unsigned int strlen (char string[]);
Here string[ ] can be a string constant or a character pointer or a character

array and the function returns unsigned int

• Example:
strlen(“Programming Fundamentals”); returns 24

strlen(acItemCategory); returns the number of characters


in the character array ‘acItemCategory’
strlen() Function (2 of 2)

• An array can be of a bigger size than its length.


• The end of string is marked by the null character.
• The memory locations beyond the null character are
unused and may contain unknown (garbage) values.

c ^
~ #
)
\0
c h
17
16
T e
1 5
14 ers
13 C h aract
12 ed
Strin
g
s y s 10
11 Unus
Start o 8
9
n f 6
7
I 5 = 12
3
4
Strin
g Null r
2 th of inato
to s

1 Leng Term
int

0
Po

2A0E34
acInfy
strcpy() Function
• strcpy() function is used to copy one string to another

• Syntax:
strcpy(Dest_String , Source_String);
– Here Dest_string should always be variable
– Source_String can be a variable or a string constant
– The previous contents of Dest_String, if any, will be over written

• Example:
char acCourseName[40];
strcpy(acCourseName , “C Programming”);

The resultant string in ‘acCourseName’ will be “C Programming”


strcat() Function
• strcat() function is used to concatenate (Combine) two strings

• Syntax
strcat( Dest_String_Variable , Source_String ) ;
• In this, the Destination should be a variable and Source_String can either be a string
constant or a variable.
• The contents of Dest_String is concatenated with Source_String contents and the
resultant string is stored into Dest_String variable.

• Example:
char acTraineeFpCourse [50] = “The course is “;
strcat(acTraineeFpCourse,”Oracle 10G”);

The resultant string in ‘acTraineeFPCourse’ will be “The course is Oracle 10G”


strcmp() Function (1 of 2)

• strcmp() function is used to compare two strings

• strcmp() does a case sensitive (Upper case and lower


case alphabets are considered to be different)
comparison on strings

• Syntax:
int strcmp( String1 , String2 );
– Here both String1 and String2 can either be a variable or a string constant
Other String Functions(1 of 2)
Other string functions
• strncpy() is used to copy only the left most n characters from source to
destination
Syntax:
strncpy( Destination_String ,Source_String, int
no_of_characters);
Here only n characters from Source_String is copied to Destination_String

• strncat() is used to concatenate only left most n-characters from source


with destination
Syntax:
strncat( Destination_String, Source_String, int

no_of_characters);
Other String Functions( 2 of 2)
Other string functions
• strncmp() is used to compare only left most ‘n’ characters from the strings

Syntax:
strncmp(char string1,char string2,int
no_of_characters );
Array of strings

• To store multiple strings, array of strings (2-D char


array) is used.

• Each row in an array of strings store only one string.

• To access a string, the row index (starts with zero)


should be supplied.

• To access a single character in an array of strings, the


row index (starts with zero) and the column index
(starts with zero) should be supplied.
Declaration of array of strings

• To declare array of strings, the row size and the


column size should be supplied.

• Syntax:
char array-name[row size][column size];

• Example:
char acEmpNames[10][50];
– The above declaration declares ‘acEmpNames’ as an array of strings
which can store maximum of 10 names and each name can have
maximum of 50 characters (including the null (‘\0’) character).
Initialization and referencing array of strings
• The array of strings can be initialized as follows:
char acEmpNames[3][5]={"JACK","RAM","LEO"};
In the above declaration,
string “JACK” is stored into acEmpNames[0],
string “RAM” is stored into acEmpNames[1] and
string “LEO” is stored into acEmpNames[2]

Note: The strings are enclosed in “ “(double quotes)


Only one subscript is needed to access one string as a single
entity
Array of strings( 1 of 2 )

Inferences from figure


• The ‘\0’ is at different
0,4
0,3 positions in different rows,
0,2 i.e., It marks the end of a
0,1
0th row 0,0 string
1,4 • Each individual character is
1,3
1,2 referenced by using row and
1,1
1,0 column subscripts
1st row
• The unused locations
2,4 contain garbage values,
2,3 these are not read by string
2,2
2nd row 2,1 handling functions
2,0
• The row numbers start from
zero
Array of strings (2-D array-3 rows and 5 columns) that stores
the name of three employees
Jagged Array
• Jagged (uneven) Array means array containing
different rows and columns.
• Either Regular or jagged the data is stored in
contiguous memory locations.
Ex: char EmpNames[3][6]={“ICE",“GREEN",“CONE"};
I C E \0
G R E E N \0
C O N E \0
Functions
• A function is a section of a program that performs a specific task.
• Function groups a number of program statements into a unit and
gives it a name. This unit can be reused wherever it is required in
the program.
• Functions employ the top down approach and hence becomes
easier to develop and manage.

main() User defined function

Function
call
Categories of functions

• They are divided in to four types


1. Function without arguments and with return
value.
2. Function with arguments but without return
value.
3. Function with arguments but with return
value.
4. Function without arguments but without
return value.
Advantages of Functions

• Solving a problem using different functions makes programming


much simpler with fewer defects.

• Easy to code, modify, debug and also understand the code.

• Functions support reusability, i.e., once a function is written it


can be called from any other module without having to rewrite
the same. This saves time in rewriting the same code.
Classification of Functions

• Library functions
- defined in the language
- provided along with the compiler

Example: printf(), scanf() etc.

• User Defined functions


- written by the user

Example: main() or any other


user-defined function
Passing values to functions and returning values

• Functions are used to perform a specific task on a set of values.

• Values can be passed to functions so that the function performs the task on
these values.

• Values passed to the function are called arguments. (Actual and Formal)

• After the function performs the task, it may send back the results to the
calling function.

• The value sent back by the function is called return value.

• A function can return back only one value to the calling function through a
return statement.

• Function may be called either from within main() or from within another
function.
Elements of a Function
 Function Declaration or Function Prototype :
- The function should be declared prior to its usage

 Function Definition :
- Implementing the function or writing the task of the function
- Consists of
• Function Header
• Function Body

 Function Invocation or Function call:


- To utilize a function’s service, the function have to be invoked
(called)
Writing User-Defined Functions (1 of 3)
Return data Arguments
type (Parameters)

int fnAdd(int iNumber1, int iNumber2){


Function
/* Variable declaration*/
header
int iSum;

/* Find the sum */ Function


iSum = iNumber1 + iNumber2; Body

/* Return the result */


return (iSum);
}
Can also be written as return iSum;
Returning values

• The result of the function can be given back to the calling


function
• ‘return’ statement is used to return a value to the calling function
• Syntax:
return (expression) ;
• Example:
return(iNumber * iNumber);
return 0;
return (3);
return (10 * iNumber);
return (a);
Contd..
• The following return statement is used to return the
address of the variable.
Ex: return(&p);
• The following return statement is used to return the
value of a variable through the pointer.
Ex: return(*p);
• The following return statement is used to return the
function value.
Ex: return(sqrt(r));
Function Terminology

Function Prototype
void fnDisplay() ;
Calling Function
int main()
{
fnDisplay();
return 0;
Function Call
}
Statement
Called Function
void fnDisplay(){
printf(“Hello World”); Function
} Definition
Formal and Actual Parameters
• The variables declared in the function header
are called as formal parameters.

• The variables or constants that are passed in the


function call are called as actual parameters.

• The formal parameter names and actual


parameters names can be the same or different.
Global Variables (2 of 2)
/* Find the sum of two integers */
void fnSumPrint();
int giNumber1,giNumber2;
int main(int argc, char **argv){ Global variables
giNumber1=10;
giNumber2=20;
fnSumPrint();
return 0;
}
void fnSumPrint(){ Variable ‘iResult’ is
int iResult; local to function
iResult = giNumber1 + giNumber2; ‘fnSumPrint’
printf(“%d”,iResult);
}
Local Vs Global Variables

Variable Scope Value after


declaration
Local Variable Can be referenced only within the Garbage value
function in which it is declared
Global Variable Can be referenced in all the functions Default values
Parameter Passing Techniques
• When a function is called and if the function
accepts some parameters, then there are two
ways by which the function can receive
parameters.
– Pass by value
– Pass by reference
Call by value
• Ex: void swap(int,int);
void main()
{
int a=40,b=50;
clrscr();
swap(a,b);
printf(“a=%d\tb=%d\t”,a,b);
}
void swap(int x,int y)
{
int t;
t=x;
x=y;
y=t;
printf(“x=%d\ty=%d\t”,x,y);
}
Output:
a=40 b=50
x=50 y=40
Call by Reference
• Ex: void swap(int *,int *);
void main()
{
int a=20,b=30;
clrscr();
printf(“numbers before swap a=%d\tb=%d\t”,a,b);
swap(&a,&b);
getch();
}
void swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
printf(“numbers after swap a=%d\tb=%d\t”,a,b);
}
Output:
a=20 b=30
a=30 b=20
Pass by value Vs Pass by reference
Pass by value Pass by reference
Consumes more memory space Consumes less memory space.
because formal parameter also Because irrespective of the actual
occupies memory space. arguments data type, each pointer
Occupies only 2 bytes.

Takes more time for execution, Takes less time because no values are
because the values are copied. copied

Any change in the formal Instead of passing values, we are


argument does not affect the passing (reference) to called function,
actual argument bcz formal So data manipulation can be done.
arguments is the copy for
actual arguments (No chance
for data manuplation)
Recursive Functions

• When a function calls itself, it is called as


Recursion.
• Recursion was classified in to two types

1. Direct Recursion
2. Indirect Recursion
Direct Recursion
Ex: int fact();
void main()
{
int n=5,f;
f=fact(n); 2*fact(1)
printf(“factorial=%d”,f);
} 3*fact(2)
int fact(int n) 5*fact(4)
{
4*fact(3)
int x;
if(n==1)
return 1; 5*fact(4)
else
x=n*fact(n-1);
return x;
}
Indirect Recursion

• In this type of recursion, two or more


functions are involved.
Ex: int s=0;
void show();
void main()
{
if(s==5)
exit(0);
show();
}
void show()
{
printf(“%d”,s);
s++;
main();
}
O/P: 0 1 2 3 4
Rules for Recursion
• Only the user defined functions can be involved in
recursion, library functions cannot be involved in
recursion because their source code cannot be
viewed.
• Two conditions are to be satisfied by a recursive
function
i) Every time a function calls itself directly or indirectly
ii) Function should have a condition to stop the
recursion otherwise an infinite loop is generated (ex if)
Typedef

• The C programming language provides a


keyword called typedef, which you can use to
give a new name to Data type.
Ex: #define M 60
void main()
{
typedef int hello;
hello hrs;
clrscr();
printf(“enter the hours”);
scanf(“%d”,&hrs);
printf(“minutes is %d \n”,hrs*M);
printf(“seconds is %d \n”,hrs*M*M);
}
o/p: enter hours: 2
minutes : 120
seconds: 7200
Enumerated data type

• The keyword enum is used to create enumeration


data type(new data type).
• The Programmer can create his own data type and
define what values the variables of these data type
can hold.
• Enum is used to assign constant values to set of
variables automatically.
Ex: void main()
{
enum month{Jan,Feb,Mar,Apr,May=5,Jun};
clrscr();
printf(“March=%d”,Mar);
printf(“April=%d”,Apr);
printf(“June=%d”,Jun);
getch();
}
o/p: March=2
April=3
June=6
Bit Fields

• Bit fields provides exact amount of bits required for


storage of values.
• If a variable contains value is 0 or 1, we need a single
bit to store it.
• If a variable contains value is 0 or 7, we need a 3 bits
to store it and so on.
Ex: #define pass 1
#define fail 0
void main()
{
struct student
{
char a[10];
unsigned int result:1;
}s;
s.name=“karthik”;
s.result= pass;
printf(“Name is %s”,s.name);
printf(“Result is %d”, s.result);
getch();
}
O/P: Name is karthik
Result is 1
Rules for Bit fields
• Bit fields is applicable only for structures.
• That to only for signed and unsigned integers.
• For signed int we need to keep one more additional
bit for ‘-’ (minus sign)
• Only Static initialization is possible.
• Only Dynamic initialization is not possible.
Interview questions
1. What is the output of printf("%d") ?
When we write printf("%d",x); this means compiler will print
the value of x. But as here, there is nothing after %d so
compiler will show in output window garbage value.
2. What is the difference between printf() and sprintf() ?
sprintf() writes data to the character array whereas printf(...)
writes data to the standard output device.
#include <stdio.h>
#include <math.h>
void main()
{
char str[80];
float pi=3.14;
sprintf(str, "Value of Pi = %f", pi);
puts(str);
getch();
}
O/P: Value of Pi = 3.14
3. The result of expression (23*2) % (int) 5.5 is

a) 2

b) 1

c) 3

d) 0
Ans:

b) 1
4. The result of 16>>2

a) 4

b) 8

c) 2

d) 5
Ans:

a) 4
5.

Void main()
{
char c=65;
clrscr();
printf(“%d %c %d”, c,c,c);
}
Ans:

65 A 65
6.

Void main()
{
unsigned a=2147483644;
long b=2147483647;
clrscr();
printf(“a=%u and b=%ld”, a,b);
}
Ans:

a=65535 (ie address of a variable)

b=2147483647
7.
Void main()
{
int x,y,z;
clrscr();
y=2;
x=2;
x=2*(y++);
z=2*(y++);
printf(“x=%d, y=%d and z=%d”, x,y,z);
}
Ans:

x=4, y=4, z=6


8.

Void main()
{
int m,j=3,k;
clrscr();
m=2*j / 2;
k=2*(j / 2);
printf(“m=%d and k=%d”, m,k);
}
Ans:

m=3 and k=2


9.
Void main()
{
int k=8;
clrscr();
printf(“k=%d”, k++ - k++);
}
Ans:

k=-1
10.
Void main()
{
int a,b,c;
clrscr();
a=9;
b=10;
c=(b<a || b>a);
printf(“c=%d”, c);
}
Ans:

c=1
11.
Void main()
{
int a,b;
clrscr();
a=65*66;
b=‘A’ * ‘B’;
printf(“a=%d b=%d”, a,b);
}
Ans:

a=4290 and b=4290


12.
Void main()
{
float x=2.3;
clrscr();
x+=.2;
printf(“x=%g”, x);
}
Ans:

x=2.5
13.
Void main()
{
clrscr();
printf(“%d %d %d %d”, ’a’,’b’,’c’,’d’);
}
Ans:

979899100
14.What will be output when you will execute following
c code?
#include<stdio.h>
int main(){
printf("%d\t",sizeof(6.5));
printf("%d\t",sizeof(90000));
printf("%d",sizeof('A'));
return 0;
}
Ans: 8 4 1
• By default data type of numeric constants is:
• 6.5 : double
• 90000: long int
• ‘A’: char
15.What will be output when you will execute following
c code?
#include<stdio.h>
int main(){
double num=5.2;
int var=5;
printf("%d\t",sizeof(!num));
printf("%d\t",sizeof(var=15/2));
printf("%d",var);
return 0;
}
Ans: 2 2 5
• !num
• =!5.2
• =0 (int)
16.What will be output when you will execute following c code?

#include<stdio.h>
int main(){
int a= sizeof(signed) +sizeof(unsigned);
int b=sizeof(const)+sizeof(volatile);
printf("%d",a+++b);
return 0;
}
Default data type of signed, unsigned, const and
volatile is int. In turbo c size of int is two byte.
So, a = 4 and b =4
Now, a+++b
= a++ + b
= 4 + 4 //due to post increment operator.
=8
17.Which of the following is integral data type?

(A)void
(B)char
(C)float
(D)Double
(E)None of these
• In c char is integral data type. It stores the
ASCII value of any character constant.
#include <stdio.h>
int main()
{
int a = 1, b = 1, c;
c = a++ + b;
printf("%d, %d", a, b);
}
• a = 2, b = 1
18.#include <stdio.h>
void main()
{
m();
m();
}
void m()
{
static int x = 5;
x++;
printf("%d", x);
}
Ans: 6 7
19.#include <stdio.h>
void main()
{
static int x;
printf("x is %d", x);
}
O/P:
0
20.#include <stdio.h>
static int x;
void main()
{
int x;
printf("x is %d", x);
}
O/P:
Garbage Value
21. Void main()
{
int x=5,y=10,z=8,p;
P=x<y<z;
printf(“%d”,p);
}
O/P:
x<y<z
5<10=true =>1<8=true
So ans is 1
22. Void main()
{
int x=3,y=5,z=10,p;
P=x&&y&&z;
printf(“%d”,p);
}
Ans: 3&&5
T &&T
1&&10
T=>1
23. Void main()
{
int x=5,y=10,z;
z=(x==5)||(y=6);
printf(“%d %d%d”,x,y,z);
}
Ans: if the first part is true compiler vl never
checks the second part
24. Void main()
{
int x=5,y=7,z;
z=(x==6)||(y=6);
printf(“%d %d%d”,x,y,z);
}
Ans:
in this case first part is false so compiler
checks the second part
5 6 1
25. Void main()
{
int x=5,y=13,z;
z=x&y;
printf(“%d ”,z);
}
Ans : 5
26. Void main()
{
int x=5,y=13,z;
z=x|y;
printf(“%d ”,z);
}
Ans : 13
26. Void main()
{
int x=15,y=2,z;
z=x>>y;
printf(“%d ”,z);
}
Formula : x/pow(2,y)
Ans: 3
27. Void main()
{
int x=15,y=2,z;
z=x<<y;
printf(“%d ”,z);
}
Formula : x*pow(2,y)
Ans: 60
28. Void main()
{
int x=3,y=4,z=5,p;
p=x=y=z;
printf(“%d %d %d %d ”,p,x,y,z);
}
Ans: 5 5 5 5
29. Void main()
{
int a=1,b=2,c=3;
a+=b+=c;
printf(“%d %d %d ”,a,b,c);
}
Ans: a=a+b => a=3
b=b+c => b=5
a=a+b
a=1+5 =>6
653
30. Void main()
{
int x=3,y=4,z;
z=x,y;
printf(“%d\n”,z);
z=(x,y); // comma expression
printf(“%d”,z);
}
Ans: 3
4
31. int x=5;
Void main()
{
int x=x;
printf(“%d”,x);
}
Ans: Garbage
bcz local variables having highest prority
32. Void main()
{
int scanf=10,getch=20,clrscr;
clrscr=scanf+getch;
printf(“%d”,clrscr);
}
Ans: 30
bcz all scanf,getch,clrscr are not
keywords
33. Void main()
{
int scanf=10,getch=20,clrscr;
clrscr=scanf+getch;
printf(“%d”,clrscr);
getch();
}
Ans: compiler error
bcz we should not use same name for
variable and function in a program
34. Void main()
{
int i=011;
clrscr();
printf(“%d”,i);
getch();
}
Ans: 9 (octal to decimal conversion)
35. Void main()
{
clrscr();
printf(“Rama”+1);
getch();
}
Ans: ama
Initial address +1 is nothing but from 1st index it
is going to print the string
36. Void main()
{
clrscr();
printf(“Rama”,”bheema”);
getch();
}
Ans: rama
It prints only first string upto comma
37. Void main()
{
clrscr();
printf((“Rama”,”bheema”));
getch();
}
Ans: bheema
Bcz of comma expression, It prints only last
string
38. Void main()
{
int x=5,y,z;
clrscr();
z=printf(“%d”,scanf(“%d%d”,&y,&z));
printf(“%d”,z);
getch();
}
Ans: scanf return count of no of data it will
read, so answer is
21
39. Void main()
{
int x;
clrscr();
x=printf(“FIVE”)-printf(“SIX”);
printf(“%d”,x);
getch();
}
Ans: printf return count of no of data it is
printed, so answer is
FIVESIX1
40. Void main()
{
clrscr();
printf(“%d”,printf(“%d”,printf(“hello”)));
getch();
}
Ans: printf return count of no of data it is
printed, so answer is
hello51
41. Void main()
{
int x;
clrscr();
x=sizeof(int)-sizeof(“int”);
printf(“%d”,x);
getch();
}
Ans:
-2
42. Void main()
{
int x=5,y=6,z;
clrscr();
z=x+++y; =>z=x++ +y;
printf(“%d%d%d”,x,y,z);
getch();
}
Ans:
6 6 11
43. Void main()
{
int x=5,y=6,z;
clrscr();
z=x+++++y;
printf(“%d”, z);
getch();
}
Ans:
error L value required
44. Void main()
{
int x=3,y,z;
clrscr();
printf(“hello%n”, &y);
z=x+y;
getch();
}
Ans:
hello8
%n is nothing but it count the no of
characters is printed and stores in y address
45. Void main()
{
printf(“siri\rekha”);
getch();
}
Ans:
ekha
ekha bcz of carriage return \r
45. Void main()
{
printf(“chandra\babu”);
getch();
}
Ans:
chandrabu
bcz of back space\b
46. Void main()
{
int x=5;
if(x=6)
printf(“hello”);
printf(“bye”)
}
Ans:
hellobye
47. Void main()
{
if(printf(“hai”)-printf(“bye”))
printf(“welcome”);
else
printf(“tata”);
}
Ans:
haibyetata
48. Void main()
{
int i=5,j=6,k=7;
if(i<j,j<k,i==k)
{
printf(“correct”);
}
else
{
printf(“wrong”);
}
Ans:
wrong
bcz of comma evaluates from L to R but excutes
only last expression and first two vl become
dummy
49. Void main()
{
int x=-1,y=1;
if(++x && ++y)
{
printf(“hello %d %d”,x,y);
}
else
{
printf(“bye %d %d”,x,y);
}
Ans:
bye01
50. Void main()
{
char x=‘H’;
clrscr();
switch(x)
{
case ‘H’: printf(“%c”, ’H’);
case ‘E’: printf(“%c”, ’E’);
case ‘L’: printf(“%c”, ’L’);
case ‘L’: printf(“%c”, ’L’);
case ‘O’: printf(“%c”, ’O’);
}
}
Ans:

HELLO
51. Void main()
{
char x=‘G’;
clrscr();
switch(x)
{
if(x==‘B’)
case ‘d’: printf(“%s”, hello);
else
case ‘G’: printf(“%s”, good);
default: printf(“%s”, boy);
}
}
Ans:

goodboy
52. Void main()
{
int k;
float x=0;
clrscr();
for(k=0;k<10;k++)
x+=.1;
printf(“x=%g”,x);
}
Ans:

x=1
53. Void main()
{
int i=5;
clrscr();
switch(i)
{
default: printf(“A”);
case 4: printf(“B”);
case 5: printf(“C”);
}
}
Ans:

C
Default can be written in the beginning also
54. Void main()
{
int i=421;
clrscr();
switch(i)
{
case 420: if(i==400)
{
case 421: printf(“hello”);
}
break;
case 422: printf(“Bye”);

}
}
Ans:

hello
Switch internally works as goto
55. Void main()
{
int i;
clrscr();
for(i=1;i<=5;i++); => for(i=1;i<=5;i++);
{ {
printf(“%d”,i); printf(“%d”,i);
} }
}
Ans:

6
56.What will be output of following c code?

#include<stdio.h>
int main(){
int i=2,j=2;
while(i+1?--i:j++)
printf("%d",i);
return 0;
}
Ans:

1
57.#include<stdio.h>
int main()
{
int x=011,i;
for(i=0;i<x;i+=3)
{
printf("Start ");
continue;
printf("End");
}
return 0;
}
Ans:

Output: Start Start Start


Explantion:
• 011 is octal number. Its equivalent decimal value is 9.
• So, x = 9
• First iteration:
• i=0
• i < x i.e. 0 < 9 i.e. if loop condition is true.
• Hence printf statement will print: Start
58.
#include<stdio.h>
int main()
{
static int i;
for(++i;++i;++i)
{
printf("%d ",i);
if(i==4) break;
}
return 0;
}
O/P:
24
59. What will be output of following c code?
#include<stdio.h>
int main()
{
int i=1;
for(i=0;i=-1;i=1)
{
printf("%d ",i);
if(i!=1) break;
}
return 0;
}
Output: -1
Explanation:
Initial value of variable i is 1.
First iteration:
For loop initial value: i = 0
For loop condition: i = -1 . Since -1 is non- zero number. So loop
condition true. Hence printf function will print value of
variable i i.e. -1
Since variable i is not equal to 1. So, if condition is true. Due to
break keyword program control will come out of the for loop.
60.What will be output of following c code?

#include<stdio.h>
int i=40;
int main()
{
do
{
printf("%d",i++);
}
while(5,4,3,2,1,0);
return 0;
}
• Output: 40
• Explanation:
• Initial value of variable i is 40
• First iteration:
• printf function will print i++ i.e. 40
• do - while condition is : (5,4,3,2,1,0)
• Here comma is behaving as operator and it
will return 0. So while condition is false hence
program control will come out of the for loop.
61.How many times this loop will execute?
#include<stdio.h>
int main()
{
char c=125;
do
{
printf("%d ",c);
}
while(c++);
return 0;
}
Output: Finite times
Explanation:
If we will increment the char variable c it will
increment as:
126,127,-128,-127,126 . . . . , 3, 2, 1, 0
When variable c = 0 then loop will terminate.
62.What will be output of following c code?

#include<stdio.h>
int main()
{
int x=123;
int i={
printf("c" "++")
};
for(x=0;x<=i;x++)
{
printf("%x ",x);
}
return 0;
}
• Output: c++0 1 2 3
• Explanation:
• First printf function will print: c++ and return 3
to variable i.
• For loop will execute three time and printf
function will print 0, 1, 2 respectively.
63.#include <stdio.h>
int main()
{
int a = 0, i = 0, b;
for (i = 0;i < 5; i++)
{
a++;
continue;
}
printf(“%d”,a);
}
O/P: 5
64.#include <stdio.h>
int main()
{
printf("%d ", 1);
goto l1;
printf("%d ", 2);
l1:goto l2;
printf("%d ", 3);
l2:printf("%d ", 4);
}
O/P:
14
65.#include <stdio.h>
void main()
{
char s[7]=“hello”;
s[2]=65; or s[2]=0;
printf(“%s”,s);
}
O/P:
heAlo or he
66.#include <stdio.h>
void main()
{
char s[10]=“morning”;
int i;
for(i=0;s[i];i++)
{
puts(s+i);
}
}
O/P:
morning
orning
rning
ning
ing
ng
g
67.#include <stdio.h>
void main()
{
char x[5]=“gate”;
char t;
t=x[0];
x[0]=x[1];
x[1]=t;
puts(x);
}
O/P:
agte
Here normal swapping takes place because of
index
68.#include <stdio.h>
void main()
{
char x[5]=“ABCD”;
char t;
t=x[0];
x[0]=x[4];
x[4]=t;
puts(x);
}
O/P:
no output
Bcz null comes first
69.#include <stdio.h>
void main()
{
int i,j;
char s[3][4]={“abcd”,”ef”,”gh”};
puts(s[0]); //represents row subscript
puts(s[0]);
puts(s[0]);
}
Ans: abcdef
ef
gh

a b c d
0 \0 (e) f \0
1 replace
g h \0
2
70.#include <stdio.h>
void main()
{
int i,j;
char x[5]={“ABC”};
i=sizeof(x);
j=strlen(x);
printf(“%d”,i+j);
}
O/P:
8
Strlen() doesnot count null character
71.#include <stdio.h>
void main()
{
char x[]={‘h’,’e’,’l’,’l’,’o’,’\0’};
char y[]=“hello”;
printf(“%d %d”,strlen(x),strlen(y));
}
O/P:
55
Strlen() doesnot count null character
72.#include <stdio.h>
void main()
{
char t[10]=“hari”;
char s[10];
strrev(t);
strcpy(s,t);
puts(s);
puts(t);
}
O/P:
irah
irah
73. What will be output of following program?
#include<stdio.h>
int main(){
int i = 3;
int *j;
int **k;
j=&i;
k=&j;
printf("%u %u %d ",k,*k,**k);
return 0;
}
O/P:
Address, Address, 3
74. What will be output of following program?
#include<stdio.h>
#include<string.h>
int main(){
register a = 25;
Int *p;
p=&a;
printf("%d ",*p);
return 0;
}
• Compilation error

Register data type stores in CPU. So it has not


any memory address. Hence we cannot write
&a.
75.What will be output of following program?
#include<stdio.h>
int main(){
int a = 10;
void *p = &a;
int *ptr = p;
printf("%u",*ptr);
return 0;
}
• 10
Void pointer can hold address of any data type
without type casting. Any pointer can hold
void pointer without type casting.
76.What will be output of following program?
#include<stdio.h>
int main(){
char arr[10];
arr = "world";
printf("%s",arr);
return 0;
}
Compilation error Lvalue required

Array name is constant pointer and we cannot


assign any value in constant data type after
declaration.
77.What will be output of following program?
#include<stdio.h>
#include<string.h>
int main(){
int a = 5,b = 10,c;
int *p = &a,*q = &b;
c = p - q;
printf("%d" , c);
return 0;
}
• Difference of two same type of pointer is
always one.
78.What will be output of following program?
#include<stdio.h>
int main(){
int i = 5 , j;
int *p , *q;
p = &i;
q = &j;
j = 5;
printf("%d %d",*p,*q);
return 0;
}
O/P:

55
79. What will be output of following program?
#include<stdio.h>
int main(){
int i = 5;
int *p;
p = &i;
printf(" %u %u", *&p , &*p);
return 0;
}
O/P:
Address Address

Since * and & always cancel to each other.

i.e. *&a = a

so *&p = p which store address of integer i

&*p = &*(&i) //since p = &i

= &(*&i)

= &i
80.What will be output of following program?
#include<stdio.h>
int main(){
int i = 100;
printf("value of i : %d addresss of i : %u",i,&i);
i++;
printf("\nvalue of i : %d addresss of i : %u",i,&i);
return 0;
}
O/P:
value of i : 100 addresss of i : Address
value of i : 101 addresss of i : Address

Within the scope of any variable, value of


variable may change but its address will never
change in any modification of variable.
81.What will be output of following program?
#include<stdio.h>
int main(){
int i = 3;
int *j;
int **k;
j = &i;
k = &j;
printf("%u %u %u",i,j,k);
return 0;
}
O/P:
3 Address Address

Here 6024, 8085, 9091 is any arbitrary


address, it may be different.
82.What will be output of following c code?
#include<stdio.h>
int main(){
int *p1,**p2;
double *q1,**q2;
clrscr();
printf("%d %d ",sizeof(p1),sizeof(p2));
printf("%d %d",sizeof(q1),sizeof(q2));
getch();
return 0;
}
O/P:
2222
• Size of any type of pointer is 2 byte (In case of
near pointer)
83.#include <stdio.h>
void main()
{
m();
}
void m()
{
printf("hi");
m();
}
O/P:
Infinite hi
84.void main()
{
struct world
{
int a;
char b;
struct india
{
char c;
float d;
}p;
};
struct world st ={1,'A','i',1.8};
clrscr();
printf("%d\t%c\t%c\t%f",st.a,st.b,st.p.c,st.p.d);
getch();
}
O/P:
Output: 1 A i 1.800000
85. struct A
{
int i;
char j;
}a,*b;
main()
{
a.i=5;
a.j=‘A’;
b=&a;
Printf(“%d %c”,(*b).i,(*b).j);
Printf(“%d %c”,b->i,b->j);
}
O/P:
5 A
5 A
86. struct A
{
int rollno;
char name[20];
}s;
main()
{
s.rollno=101;
s.name=“Rama”; //strcpy(s.name,”Rama”);
Printf(“%d”,sizeof(s));
}
O/P:
Error bcz char name[20] acts as a constant
pointer
22
87. struct Demo
{
int i;
char j;
struct tag
{
int k;
}l;
}d={2,’A’,4};
Printf(“%d”,sizeof(d.l));
Printf(“%d”,sizeof(d));
O/P:
2

5
88. f(int *x,int y)
{
*x=*x+y;
y=*x+y;
}
main()
{
int x=3,y=5;
f(&x,y);
printf(“%d”,x+y);
}
Ans:13
89. Void f(char *s)
{
if(*s)
{
puts(s);
f(s+1);
}
}
main()
{
f(“Data”);
}
O/P:
Data
ata
at
a
91. f(int n)
{
if(n)
{
printf(“%d”,n%10);
f(n/10);
}
}
main()
{
f(1234);
}
O/P:
4321
92. f(static int I, int j)
{
i=i+j;
printf(“%d %d”,i,j);
}
main()
{
f(1,2);
f(2,3);
}
O/P:
Error
Bcz parameter variable should not be a static
93. main()
{
int p=1,q=2,r=3,m=4;
printf(“%d”,m?p?q:r:m+2);

}
O/P:
2
m is True bcz of m=4, so it checks p, p is also
true then true block will be executed i.e q
value of q is 2
94. main()
{
int x=10;
static int y=x;
x+1;
++x;
y++;
printf(“%d %d”,x,y);
}
O/P:
Error
Bcz static variable must be initialized with
constant data
95. Struct bit
{
int i:3;
int j:2;
}b;
main()
{
printf(“%d”,sizeof(b));
}
O/P:
1 byte
Sizeof always works with byte, so eventhough we
use 5 bits it shows as 1 byte
96. main()
{
Struct bit
{
int i:3;
int j:2;
}b;
b.i=13;
b.j=6;
printf(“%d”,b.i+b.j)
}
O/P:
12 => 5+7
97. #define A 10
void main()
{
int j;
j=A++;
printf(“%d”,j);
}
O/P: error
10++ constant cant be modified
98.#define A 10
void main()
{
printf(“%d”,A);
#define A 20
printf(“%d”,A);
}
#define A 30
O/P: 10 20
Same macro name we can define many times in
the same program
99.#define plus +
#define minus -
void main()
{
int i=2,j=3,k=4,p;
p=i plus j minus k;
printf(“%d”,p);
}
O/P: 14
#define + -
#define - *
Error bcz macro name cant be a constant
100.#define square (x) x*x
void main()
{
int x;
x=36/square(6);
printf(“%d”,x);
}
O/P:
X=36/6*6
=>6*6
=>36
101.#define square (x) (x*x)
void main()
{
int x;
x=36/square(6);
printf(“%d”,x);
}
O/P:
X=36/(6*6)
=>36/36
=>1
102.#define scanf “%sHello world
void main()
{
printf(scanf,scanf);
}
O/P:
%sHelloworld Hello world
103.
void main()
{
printf(“Rama%sai”,”hari”);
}
O/P:
RamaHariai
104.#define macro(n,a,i,m) m##a##i##n
#define MAIN macro(n,a,i,m)
void MAIN()
{
printf(“Hello C”);
}
O/P:
Hello C
105.#define A 5
void main()
{
#ifdef A
#define B 10
#endif
printf(“%d %d”,A,B);
}
O/P:
5 10
106.#define f(g,g2) g##g2
void main()
{
int var12=100;
printf(“%d “,f(var,12));
}
O/P:
100

You might also like