Final Programming in C
Final Programming in C
Final Programming in C
Computer Basics
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 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
• 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
int EmployeeNumbers[6];
float Salary[6];
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
• 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
• Example:
char acEmployeeName[20];
• 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 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);
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)
• Syntax:
unsigned int strlen (char string[]);
Here string[ ] can be a string constant or a character pointer or a character
• Example:
strlen(“Programming Fundamentals”); returns 24
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”);
• 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”);
• 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
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
• 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]
Function
call
Categories of functions
• Library functions
- defined in the language
- provided along with the compiler
• 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.
• 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 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.
Takes more time for execution, Takes less time because no values are
because the values are copied. copied
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
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:
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:
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:
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:
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:
#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
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
i.e. *&a = a
= &(*&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
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