de. Find it(them) out!!! Assume that the function getop returns the appropriate return values for operands, opcodes, EOF etc.. #include <stdio.h> #include <stdlib.h>
#define MAX 80 #define NUMBER '0'
int getop(char[]);
void push(double);
double pop(void);
int main()
{
int type;
char s[MAX];
while((type = getop(s)) != EOF)
{
switch(type)
{
case NUMBER:
push(atof(s));
break;
case '+':
push(pop() + pop());
break;
case '*':
push(pop() * pop());
break;
case '-':
push(pop() - pop());
break;
case '/':
push(pop() / pop());
break;
/* ... * ... * ... */ }
}
}
The following is a simple program which implements a minimal version of banner command available on most *nix systems. Find out the logic used in the program. #include<stdio.h> #include<ctype.h>
char t[]={
0,0,0,0,0,0,12,18,33,63,
33,33,62,32,62,33,33,62,30,33,
32,32,33,30,62,33,33,33,33,62,
63,32,62,32,32,63,63,32,62,32,
32,32,30,33,32,39,33,30,33,33,
63,33,33,33,4,4,4,4,4,4,
1,1,1,1,33,30,33,34,60,36,
34,33,32,32,32,32,32,63,33,51,
45,33,33,33,33,49,41,37,35,33,
30,33,33,33,33,30,62,33,33,62,
32,32,30,33,33,37,34,29,62,33,
33,62,34,33,30,32,30,1,33,30,
31,4,4,4,4,4,33,33,33,33,
33,30,33,33,33,33,18,12,33,33,
33,45,51,33,33,18,12,12,18,33,
17,10,4,4,4,4,63,2,4,8,
16,63 };
int main(int argc,char** argv)
{
int r,pr;
for(r=0;r<6;++r)
{
char *p=argv[1];
while(pr&&*p)
{
int o=(toupper(*p++)-'A')*6+6+r;
o=(o<0||o>=sizeof(t))?0:o;
for(pr=5;pr>=-1;--pr)
{
printf("%c",( ( (pr>=0) && (t[o]&(1<<pr)))?'#':' '));
}
}
printf("\n");
}
return 0;
}
What is the output of the following program? #include <stdio.h> #include <stdlib.h>
#define SIZEOF(arr) (sizeof(arr)/sizeof(arr[0]))
#define PrintInt(expr) printf("%s:%d\n",#expr,(expr)) int main()
{
/* The powers of 10 */ int pot[] = {
0001,
0010,
0100,
1000 };
int i;
for(i=0;i<SIZEOF(pot);i++)
PrintInt(pot[i]);
return 0;
}
The following is the implementation of the Euclid's algorithm for finding the G.C.D(Greatest Common divisor) of two integers. Explain the logic for the below implementation and think of any possible improvements on the current implementation.
BTW, what does scanf function return? #include <stdio.h> int gcd(int u,int v)
{
int t;
while(v > 0)
{
if(u > v)
{
t = u;
u = v;
v = t;
}
v = v-u;
}
return u;
}
int main()
{
int x,y;
printf("Enter x y to find their gcd:");
while(scanf("%d%d",&x, &y) != EOF)
{
if(x >0 && y>0)
printf("%d %d %d\n",x,y,gcd(x,y));
printf("Enter x y to find their gcd:");
}
printf("\n");
return 0;
}
Also implement a C function similar to the above to find the GCD of 4 integers.
What's the output of the following program. (No, it's not 10!!!) #include <stdio.h> #define PrintInt(expr) printf("%s : %d\n",#expr,(expr)) int main()
{
int y = 100;
int *p;
p = malloc(sizeof(int));
*p = 10;
y = y/*p; /*dividing y by *p */;
PrintInt(y);
return 0;
}
The following is a simple C program to read a date and print the date. Run it and explain the behaviour #include <stdio.h> int main()
{
int day,month,year;
printf("Enter the date (dd-mm-yyyy) format including -'s:");
scanf("%d-%d-%d",&day,&month,&year);
printf("The date you have entered is %d-%d-%d\n",day,month,year);
return 0;
}
The following is a simple C program to read and print an integer. But it is not working properly. What is(are) the mistake(s)? #include <stdio.h> int main()
{
int n;
printf("Enter a number:\n");
scanf("%d\n",n);
printf("You entered %d \n",n);
return 0;
}
The following is a simple C program which tries to multiply an integer by 5 using the bitwise operations. But it doesn't do so. Explain the reason for the wrong behaviour of the program. #include <stdio.h> #define PrintInt(expr) printf("%s : %d\n",#expr,(expr)) int FiveTimes(int a)
{
int t;
t = a<<2 + a;
return t;
}
int main()
{
int a = 1, b = 2,c = 3;
PrintInt(FiveTimes(a));
PrintInt(FiveTimes(b));
PrintInt(FiveTimes(c));
return 0;
}
Is the following a valid C program? #include <stdio.h> #define PrintInt(expr) printf("%s : %d\n",#expr,(expr)) int max(int x, int y)
{
(x > y) ? return x : return y;
}
int main()
{
int a = 10, b = 20;
PrintInt(a);
PrintInt(b);
PrintInt(max(a,b));
}
The following is a piece of C code, whose intention was to print a minus sign 20 times. But you can notice that, it doesn't work. #include <stdio.h> int main()
{
int i;
int n = 20;
for( i = 0; i < n; i-- )
printf("-");
return 0;
}
Well fixing the above code is straight-forward. To make the problem interesting, you have to fix the above code, by changing exactly one character. There are three known solutions. See if you can get all those three.
What's the mistake in the following code? #include <stdio.h> int main()
{
int* ptr1,ptr2;
ptr1 = malloc(sizeof(int));
ptr2 = ptr1;
*ptr2 = 10;
return 0;
}
What is the output of the following program? #include <stdio.h> int main()
{
int cnt = 5, a;
do {
a /= cnt;
} while (cnt --);
printf ("%d\n", a);
return 0;
}
What is the output of the following program? #include <stdio.h> int main()
{
int i = 6;
if( ((++i < 7) && ( i++/6)) || (++i <= 9))
;
printf("%d\n",i);
return 0;
}
What is the bug in the following program? #include <stdlib.h> #include <stdio.h> #define SIZE 15 int main()
{
int *a, i;
a = malloc(SIZE*sizeof(int));
for (i=0; i<SIZE; i++)
*(a + i) = i * i;
for (i=0; i<SIZE; i++)
printf("%d\n", *a++);
free(a);
return 0;
}
Is the following a valid C program? If so, what is the output of it? #include <stdio.h> int main()
{
int a=3, b = 5;
printf(&a["Ya!Hello! how is this? %s\n"], &b["junk/super"]);
printf(&a["WHAT%c%c%c %c%c %c !\n"], 1["this"],
2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]);
return 0;
}
What is the output of the following, if the input provided is:
Life is beautiful #include <stdio.h> int main()
{
char dummy[80];
printf("Enter a string:\n");
scanf("%[^a]",dummy);
printf("%s\n",dummy);
return 0;
}
Note : This question has more to do with Linker than C language
We have three files a.c, b.c and main.c respectively as follows:
a.c
--- int a;
b.c
--- int a = 10;
main.c
------ extern int a;
int main()
{
printf("a = %d\n",a);
return 0;
}
Let's see what happens, when the files are compiled together: bash$ gcc a.c b.c main.c
bash$ ./a.out
a = 10
Hmm!! no compilation/linker error!!! Why is it so??
The following is the offset macros which is used many a times. Figure out what is it trying to do and what is the advantage of using it. #define offsetof(a,b) ((int)(&(((a*)(0))->b)))
The following is the macro implementation of the famous, Triple xor swap. #define SWAP(a,b) ((a) ^= (b) ^= (a) ^= (b))
What are the potential problems with the above macro?
What is the use of the following macro? #define DPRINTF(x) printf("%s:%d\n",#x,x)
Let's say you were asked to code a function IAddOverFlow which takes three parameters, pointer to an integer where the result is to be stored, and the two integers which needs to be added. It returns 0 if there is an overflow and 1 otherwise: int IAddOverFlow(int* result,int a,int b)
{
/* ... */ }
So, how do you code the above function? (To put in a nutshell, what is the logic you use for overflow detection?)
What does the following macro do? #define ROUNDUP(x,n) ((x+n-1)&(~(n-1)))
Most of the C programming books, give the following example for the definition of macros. #define isupper(c) (((c) >= 'A') && ((c) <= 'Z'))
But there would be a serious problem with the above definition of macro, if it is used as follows (what is the problem??) char c;
/* ... */ if(isupper(c++))
{
/* ... */ }
But most of the libraries implement the isupper (declared in ctypes.h) as a macro (without any side effects). Find out how isupper() is implemented on your system.
I hope you know that ellipsis (...) is used to specify variable number of arguments to a function. (What is the function prototype declaration for printf?) What is wrong with the following delcaration? int VarArguments(...)
{
/*....*/ return 0;
}
Write a C program to find the smallest of three integers, without using any of the comparision operators.
What does the format specifier %n of printf function do?
Write a C function which does the addition of two integers without using the '+' operator. You can use only the bitwise operators.(Remember the good old method of implementing the full-adder circuit using the or, and, xor gates....)
How do you print I can print % using the printf function? (Remember % is used as a format specifier!!!)
What's the difference between the following two C statements? const char *p;
char* const p;
What is the difference between memcpy and memmove?
What is the format specifiers for printf to print double and float values?
Write a small C program to determine whether a machine's type is little-endian or big-endian.
Write a C program which prints Hello World! without using a semicolon!!!
评论