0% found this document useful (0 votes)
89 views2 pages

C-Aptitude Test Paper1

1. The document contains code snippets demonstrating the use of macros in C/C++. Macros allow simple text substitutions to be performed before compilation. 2. Some examples include defining mathematical functions like squaring a number, swapping variable values, conditional compilation, and recursive macros. 3. Macros must be used carefully as they are simple text replacements that do not check types or values, which can lead to bugs.

Uploaded by

suganya004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views2 pages

C-Aptitude Test Paper1

1. The document contains code snippets demonstrating the use of macros in C/C++. Macros allow simple text substitutions to be performed before compilation. 2. Some examples include defining mathematical functions like squaring a number, swapping variable values, conditional compilation, and recursive macros. 3. Macros must be used carefully as they are simple text replacements that do not check types or values, which can lead to bugs.

Uploaded by

suganya004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

1.#define prod(a,b) a*b 2.

#define one two


main() #define two one
{ int main()
Int x=3,y=4; {
printf(%d,prod(x+2,y-1)); int one=1,two=2;
} printf(%d %d,one,two);
return 0;}
3.#define sqr(x) x*x 4.#define f(g1,g2) g1##g2
main() main(){
{ int var12=100;
printf(%d,225/sqr(15)); printf(%d,f(var,12));
} }

5.main() 6.#define scanf %s SUCCESS


{ main(){
extern int i; printf(scanf,scanf);
i=20; printf(scanf);
printf(%d,i); }
}

7.#include<math.h> 8.#define octal if(c==8) printf(good);


#include<stdio.h> void main(){
#define sq(x) (x<0?sqrt(-x):sqrt(x)) int a=5,b=7,c=3;
main(); if(a==b)
{ int y; octal;
Y=sq(-9); else
printf(%d,y);} printf(Hi);}
9.int main() 10.#define RECUR printf(WIPRO);RECUR
{ main(){
int x=5, y=2, z=3, a=2; printf(INTEL);
PRINT(x,y); RECUR;
PRINT(z,a); }
PRINT(x,a);
} output:
x=3 y=2
z=3 a=2
x=5 a=2
write a macro PRINT which gives the above
output
11. #define TEST(x,y) if(x==y) printf(one) 12. void main()
main() {
{ unsigned i=12;
long diouble x=1.75; while(i++!=0)
double y =1.75; ;;;printf(%d,i);;;
TEST(x,y); }
else printf(zero);
}
13. main(){ 14.#include<stdio.h>
If(~0==(unsigned int)-1) #define a 10
printf(condition is true); main(){
} #define a 50
printf(%d,a);
}
15. write an algorithm to print the prime 16. Let a and b are two strictly distinct positive
factors of a given no. numbers . You need to find which one is
Eg: input | output greater without using conditional operator ,any
8 2 2 2 loops , any relational operators,ternary
15 3 5 operator. Is it possible?
35 7 5 If yes write the code else give proper reason.

17. Error Correction: Direction :18 -20. Find out the o/p of the
/*To print 1 to 100 and 100 to 0*/ snippets.
#define callagain main() #define TOGGLE(x) x=!x
int main(){ typedef int Boolean;
int i=1; Boolean function(unsigned n)
if(i<=100){ { Boolean flag=0;
printf(%d,i++); while(n) {
callagain; if(n&ox1)
} TOGGLE(flag);
printf(%d,i); } n>>=1;
}
return flag; }

19. typedef int Boolean; 20. unsigned function(unsigned a,unsigned b)


Boolean function(int n) { {
return ((n&1<<15) && ( n&1)); unsigned c=0;
} while(a!=0 && b!=0) {
if(b&0x1) c+=a;
b>>=1; a<<=1;
}
return c;
}

You might also like