This Guides provides Important C Programming Questions asked during InterviewsFull description
C++ interview questions
englishFull description
.
Sample QuestionsFull description
C TBW60 74 Sample Questions
C TS4FI 1511 Sample-Questions
c language mcqs
An sample for SAP SD certification TSCM62_65
Code and implementation details of LR(0) parsing in C.
Full description
Descripción completa
norma ansi tiaDescripción completa
Casting horoscopeFull description
auxiliary boiler
important
more.......
1.#include void main() { int x = 5; printf("%d,%d,%d",x,(x<<2),(x>>2)); } Ans: 5,20,1 2.#include void main() { char a[]="hello"; char b[]="hai"; a=b; printf("%s,%s",a,b); } ANs : since a is the base address of the array it is a lValue(constant) which can not be assigned with a value a=b is a error. 3.#include void main() { char * p = "hello",ch; ch = *++p; printf("%c,%s",ch,p); } Ans e,ello 4.#include #include #define TOTAL 3 #define MAXNAMELEN 80 struct company { char organization[TOTAL][MAXNAMELEN]; }; int main()
{ void nameswap(struct company) struct company x; int i; strcpy(x.organisation[0],"AATT India"); strcpy(x.organisation[1],"AATT Corporation"); strcpy(x.organisation[2],"AATT Limited"); nameswap(x); for(i=0;i #define Stringizer(x) printf(#x) void main() { Stringizer(hello); } Ans: '#' is called Stringizer Operator - #x converts x to String Constant 6. #include #define Charizing(x) printf("%d",#@x) void main() { Charizing(a); } Ans : 97 7.#include #include int main( ) {
char *ptr1,*ptr2; ptr1 = "Hello AATT"; ptr2 = "Hai"; ptr1= strcat(ptr1,ptr2); printf("\n Input string %s",ptr1); return 1; } Ans : Enough memory not allocated for concatenation 8.int main( ) { int x=20,y=35; x=y++ + x++; y=++y + ++x; printf("%d %d",x,y); return 1; } Ans : 57,94 9.int main( ) { int i,*p; i=10; p=&i; printf("%d",10/*p); return 1; } Ans:Since there is no space between / and * it is taken to be a comment and the output will not be 10/10 =1 as expected 10. #include int main() { int a=0; if(a= 0) printf("hello"); printf("AATT"); return 0; } Ans :AATT 11.Write a function to swap two numbers without using temp . Ans : Swap(a,b) { a= a+b;
b= a-b; a= a-b; } 12.O/p of Following int main() { char a[2]; *a[0]=7; *a[1]=5; printf("%d",&a[1]-a); } Ans illegal indirection 13. int main() { char a[]="hellow"; char *b="hellow"; char c[5]="hellow"; printf("%s %s %s ",a,b,c); printf(" %d,%d,%d",sizeof(a),sizeof(b),sizeof(c)); } ans : 7,4,5 (Size of b is dependent on the machine) 14. #define min(a,b) ((a)<(b))?(a):(b) main() { int i=0,a[20],*ptr; ptr=a; while(min(ptr++,&a[9])<&a[8]) i=i+1; printf("i=%d\n",i); } Ans :5 15.Prog to find PrimeFactors void Prime( int); int main() { Prime(56);
} void Prime (int a) { int j; for(j=1; j<=a; j++) { if(a% j == 0) { a/=j; printf("%d,",j); j= 1; } } return ; } 16 Function to Reverse the string . char * reverse(char a[]) { int len ,i=0; char * p; len = strlen(a); p =(char *) malloc(len+1); while(i<= len) { p[i++] = a[len -i]; } p[len] = '\0'; return p; } 17. Output of int main() { printf("%d%x\n",0x2,12); } ans :2C
18.swap two var without Temp and Arithmetic ops Ans: swap(a,b) { a=a^b; b=a^b; a=a^b; } 19.int main() { int a[5],*p; for(p=a;p<&a[5];p++) { *p=p-a; printf("%d\n",*p); } } Ans : 0,1,2,3,4 20.Prg to Reverse A number Reverse(n) { int result = 0; while(n!=0) { temp = n % 10; result =result * 10 + temp n = n/10; } return result; } 21. int zap(int n) { if(n<=1)then zap=1; else zap=zap(n-3)+zap(n-1); } then the call zap(6) gives the values of zap [a] 8 [b] 9 [c] 6 [d] 12 [e] 15 Ans b 22.op for following prg int main() {
char *x="new"; char *y="dictonary"; void swap (char * , char *); swap (x,y); printf("(%s, %s)",x,y); } void swap (char *x,char *y) { char *t; t=x; x=y; y=t; printf("(%s, %s)-",x,y); } a).(New,Dictionary)-(New,Dictionary) b).(Dictionary,New)-(New,Dictionary) c).(New,Dictionary)-(Dictionary,New) d).(Dictionary,New)-(Dictionary,New) e).None of the above Ans :c 23. main() { main(); printf("InMain \t"); } Ans : No Statement will be printed on the screen. 24. main() { printf("InMain \t"); main(); } Ans ??? 25.void strcp1(char *s,char *t) { while(*t) *s++ = *t++; } main()
{ char a[] = "God is great"; char b[] = "god is man or god"; strcp1(b,a); printf("%s",b); }
Ans :GOD IS GREAT man or god 26.int x ; int intg() { return x+=4; } int diff() { return x/=4; } main() { x = intg()+diff(); printf("%d",x); } Ans 5 27. int x; main() { int j =2; printf("%d",fun(j)); } fun(int x) { x++,++x; return x++; } Ans 4 28. main() {
static char *s[] = {"ice","green","cone","please"}; static char **ptr[] = {s+3,s+2,s+1,s}; char ***p = ptr; printf("%s",**++p); printf("\n%s",*--*++p +3); printf("\n%s",*p[-2] +3); printf("\n%s" , p[-1][-1] +1); } Ans : cone ase reen 29. main() { float **a; int i,j; for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%f",&a[i][j]); for(i=0;i<3;i++) for(j=0;j<3;j++) printf("%f",a[i][j]); } Ans : Memory not allocated for a 30. main() { printf("%d",sizeof("1234")); printf("%d",sizeof(1)); printf("%d",sizeof(1.1)); } Ans 548 31. main() { int *x; int y[3] = {10,20,30};
x=y; printf("%d",++*x); printf("\n%d",++*x++); } Ans 11,12
32. int main() { char s[] = "Get organised"; printf("%s",&s[2]); printf("\n %s",s); printf("\n %s",&s); printf("\n %c",s[2]); } Ans: t organised Get organised Get organised t 33. What is the diff between the i) int const *k; ii) int * const k; Ans : i) specifies that k is a pointer to integer constant , the k can be changed to point to another int value,but the value pointed by k can not be changed. ii) specifies that k is a constant pointer to an integer , the value pointed to by the k can be changed ,but the k can not be made to point to another integer . 34 . main() { int const *f; int x = 10; f = & x; *f = 15 printf("%d",x); }
Ans : f cannot change the value of the pointed memory. 35. main() { int const *f; int x = 10,y =15; f = & x; printf("%d",*f); f = & y; printf("%d",*f); } Ans : f cannot be assigned different addresses because it is a constant pointer.
36. main() { int a[6]={0,1,2,3,4,5}; int (*p)[3]; p=a; *p[1] = 5; printf("%d",a[1]); } 37. main() { char i ; for(i=0;i<256;i++) printf("%d",i); }
Ans :Infinite loop 38. main() { char s[]="Hello World"; s++;
printf("%s",s); } Ans :S is an l value ,cannot be a target of an assignment stmt. 39.main() { char *s="Hello World"; s++; printf("%s",s); } Ans : ello world 40. main() { char *s; s = "Hello World"; printf("%s",s); } Ans :Hello World 41. main() { int *s; s = "Hello World"; printf("%s",s); } Ans :Hello World 42. main() { char s[] = "Hello"; printf("%s" , s[3]); } Ans : %s needs an address whereas s[3] returns a content in the location s+3. 43. main() {
extern int a; printf("%d",a); } Ans : Linkage error . 44. main() { inti=-3,j=2,k=0,m; m=++i&&++j||++k; printf("\n %d %d %d %d",i,j,k,m); } Ans : 2 3 0 1 45. main() { int a,b; a=sumdig(123); b=sumdig(123); printf("%d %d",a,b); } sumdig(int n) { static int s=0; int d; if(n!=0) { d=n%10; n=(n-d)/10; s=s+d; sumdig(n); } else return(s); } Ans :6,12 46. #define CUBE(x) (x*x*x) main() { int a,b=3; a=CUBE(b++);
printf("\n %d %d",a,b); } Ans :27,6 47. main() { char *p,*f(); p=f(); printf("f() returns:%s\n",p); } char *f() { char result[80]; strcpy(result,"anything will do"); return (result); } Ans : f() returns : 48. main() { short int *p,*q; p=(short int *)1000; q=(short int *)2000; printf("%d",(q-p)); } Ans 1000/2 = 500 49 main() { char a =0xAA ; int b ; b = (int) a ; b = b >> 4 ; printf("%x",b); }
Ans : ffffffaa 50.
.main() { char *p = "hello world!"; p[0] = 'H'; printf("%s",p); } Ans : Run time error . 51. main() { char *p ; char s[20] = "Hello world!"; strcpy(p,s); printf("%s",p); } Ans : Run time error 52. main() { char *p ; char s[20] = "Hello world!"; p = (char *)malloc(20); strcpy(p,s); printf("%s",p); }
Ans : Hello World! 53. main() { int a=10,b; a>= 5 ? b=100 : b=200; printf("\n%d",b); } Ans :lvalue required 54. main()
{ int a=10,b; a>5 ? a++ : a++; printf("\n%d",a); } Ans : 11 55. Given a piece of code int x[10]; int *ab; ab=x; To access the 6th element of the array which of the following is incorrect? (A) *(x+5) (B) x[5] (C) ab[5] (D) *(*ab+5} . Ans : d 56. Consider the following program main() { int a[5]={1,3,6,7,0}; int *b; b=&a[2]; } The value of b[-1] is (A) 1 (B) 3 (C) -6 (D) none Ans : b 57. What does the following program print? #include int sum,count; void main(void) { for(count=5;sum+=--count;) printf("%d",sum); } a. The pgm goes to an infinite loop b. Prints 4791010974 c. Prints 4791001974 d. Prints 5802112085 e. Not sure Ans: a 58. #include
void main(void) { int i; for(i=2;i<=7;i++) printf("%5d",fno()); } fno() { staticintf1=1,f2=1,f3; return(f3=f1+f2,f1=f2,f2=f3); } a. produce syntax errors b. 2 3 5 8 13 21 will be displayed c. 2 2 2 2 2 2 will be displayed d. none of the above e. Not sure Ans : b 59. #include void main (void) { int x; x = 0; if (x=0) printf ("Value of x is 0"); else printf ("Value of x is not 0"); } a. print value of x is 0 b. print value of x is not 0 c. does not print anything on the screen d. there is a syntax error in the if statement e. Not sure
Ans : a 60. void main (void) { char arr[100] = {"Welcome to Mistral"}; foo (arr); } foo (char *x) { printf ("%d\t",strlen (x)); printf ("%d\t",sizeof(x)); return0;
} a. 100 100 b. 18 100 c. 18 18 d. 18 2 e. Not sure Ans : 18,100 61. #include display() { printf ("\n Hello World"); return 0; } void main (void) { int (* func_ptr) (); func_ptr = display; printf ("\n %u",func_ptr); (* func_ptr) (); } Ans : it prints the address of the function display and prints Hello World on the screen 62. #include void main (void) { int i = 0; char ch = 'A'; do putchar (ch); while(i++ < 5 || ++ch <= 'F'); } Ans : AAAAAABCDEF
* MORE C PROGRAMS *
#include #include void main()
{ clrscr(); int a,b,c; char q; printf("Enter the values for the number \n"); scanf("%d%*[-/]%d%*[-/]%d",&a,&b,&c); printf("%d %d %d",a,b,c); getch(); } /*assignment supression character*/
#include void tower(unsigned int ,char,char,char); void main() { unsigned int v; char s='L',i='M',d='R'; clrscr(); printf("Enter the no of disks"); scanf("%u",&v); printf("The Towers of hanoi problem for %u disks\n",v); tower(v,s,i,d); printf("\n"); getch(); } void tower (unsigned int v,char s,char i,char d) { if(v!=0) { tower(v-1,s,d,i); printf("Move %d disk from %c to %c\n",v,s,d); tower(v-1,i,s,d); } } #include #include void main(int argc,char **argv,char **env) { int i=0; clrscr(); printf("the no of commandline arguments %d\n",argc); printf("The commandline arguments are as follows\n"); for(i=0;i
#include #include #include void main() {
int **p; int i,j,m,n; printf("Enter the dimension of the 2d array\n"); scanf("%d%d",&m,&n); p=(int **)malloc(m*sizeof(int)); for(i=0;i
#include #include void main() { static char s[]="dhankumar"; clrscr(); printf("%s",s+3); getch(); }
#include #include #include void main() { char far * p; clrscr(); p=(char far *)MK_FP(0xB800,0x0000); printf("%c\n",*p); unsigned int of,se; of=FP_OFF(p);
#include #include int fun(int x) { return (x*x); } void main() { int (*p)(int); p=fun; clrscr(); printf("%d",p(14)); getch(); }
#include #include int f(int x) { return (x*x); } int fun(int x,int (*p)(int x)) { return (x+p(x)); } void main() { int (*r)(int x,int (*w)(int x)); int (*p)(int x); r=fun; p=f; clrscr(); printf("%d",r(3,p)); getch(); }
#include #include
int b(int ,int ); int s(int ,int ,int (*p)(int x,int y)); int b(int x,int y) { return (x>y?x:y); } int s(int x1,int y1,int (*p)(int x,int y)) { return (p(x1,y1)); } void main() { int (*p)(int x,int y); p=b; clrscr(); printf("%d",s(21,3,p)); getch(); }
#include #include void main() { clrscr(); printf("%d\n",stdin); printf("%d\n",stdout); printf("%d\n",stdprn); printf("%d\n",stdaux); printf("%d\n",stderr); getch(); } /*stream is actually a pointer to the FILE structure .The above mentioned stream are predefined streams and they are opened whenever a C program executes */
#include #include #include void main() { int n; char s[23]; FILE *fp; clrscr(); if((fp=fopen("kumar","w"))==0) { printf("File cannot be opened \n"); exit(1); } printf("Enter the name and age \n"); while((scanf("%s %d",s,&n))!=EOF) fprintf(fp,"%s %d\n",s,n); fclose(fp); printf("output \n"); fp=fopen("kumar","r"); printf("NAME AGE\n"); printf("---------------\n"); while((fscanf(fp,"%s %d",&s,&n))!=EOF) printf("%s %d\n",s,n); fclose(fp); getch(); }
#include #include #include void main() { FILE *fp; int c,co=0; printf("Enter the character \n"); fp=fopen("kumar1","w"); while((c=getchar())!=EOF) fputc(c,fp); fclose(fp);
printf("output\n"); fp=fopen("kumar1","r"); while((c=fgetc(fp))!=EOF) { printf("%c",(char)c);++co; } fclose(fp); printf("The no of characters present in the file are %d ",co); getch(); }
#include #include #include void main() { clrscr(); assert(3>3); printf("finishing of the program"); getch(); } /*assert checks for the condition if it evaluates to zero then print the error message to the screen and calls the abort function to abort the program*/
f(); f(); getch(); } void f() { static int a=4; // a=a*a; printf("%d\n",a=a*a); //this satement will change the value of the 'a' } /*if you put the statement printf("%d",a*a); instead of the above statement this printf just print the value and did not change the value of the variable a .hence use the above two lines*/ /*but if you want to increment the variable using the static variable then you can put the statement a++ int the printf itself*/ /*you can also use the statement printf("%d",a*=a);*/
{ int *p; p=malloc(4); clrscr(); printf("%d",*p); getch(); } /*In C void* pointer is automatically converted to the type of the left hand side of the assignement */
#include #include int substr(char *s1,char *s2); void main() { clrscr(); if(substr("dhankumar is hard","is")!=-1) printf("The substring is found \n"); else printf("The substring is not found\n"); getch(); } int substr(char *s1,char *s2) { int i; char *p1,*p2; for(i=0;s1[i];i++) { p1=&s1[i]; p2=s2; while(*p2&&*p2++==*p1++) if(!*p2) return i; } return -1; }
#include #include int strc(char *,char ); void main() { clrscr();
if(strc("dhankumar",'e')!=-1) printf("The given character is found "); else printf("The given character is not found "); getch(); } int strc(char *s1,char c) { while(*s1&&*s1++!=c); if(*(--s1)==c) return 1; return -1; }
#include #include void main() { clrscr(); printf("%e\n",0.034); printf("%e\n",335.5); printf("%E\n",335.5); getch(); } /*note the use of the format specifier the printf function adjusts such that the output has one whole digit and fractional digits in terms of power of both positive number and negative number*/
#include #include void main() { clrscr(); double f; for(f=1.0;f<1.0e+10;f*=10) printf("%g \n",f); getch(); } /*by using the %g or %G format specifier you can tell the printf function to use either %f or %e but this cause the printf() to select that produces the shortest output*/
#include #include void main() { clrscr(); printf("%x\n",10); printf("%X",10); getch(); } /*%x and %X are used to display the specifed number in hexadecimal number in the lowercase and uppercase */
#include
#include void main() { clrscr(); int p; printf("%d\n",&p); printf("%u\n",&p); printf("%X\n",&p); printf("%p",&p); getch(); } /*display the address in a format compatiblee with the type of addressing used by the computer */
#include #include void main() { clrscr(); int i; i=printf("%d\n",i); printf("%d",i); getch(); } /* printf retunrs the no of characters outputed*/
#include #include void main() { int p; clrscr(); printf("kumar%n\n",&p); printf("%d",p); getch(); } /* This is one of the special format specifier availiable in the C language this makes printf() to load the variable pointed to by its arguments with a value equal to the number of characters that have been output*/
#include #include
void main() { clrscr(); printf("%11.4f\n",123.1234567); printf("%3.8d\n",1000); printf("%10.15s\n","this is a simple test"); getch(); }
#include #include main() { char *p="kumar"; clrscr(); printf("%c",++*(p++)); getch(); } /* '++' operator comes after the indirection operator ,hence the increment is done at the value .here ofcourse 'r' ,but 'r'+1 leads to 's'*/
#include #include struct t { int h; int m; int s; }; void up(struct t *); void di(struct t *); void de(void ); void main() { struct t sy; sy.h=0; sy.m=0; sy.s=0; clrscr(); for(;;) { if(!kbhit()) { up(&sy); di(&sy); } else break; } clrscr(); printf("The current time is \n"); di(&sy);
#include #include void main() { int i=10; int a=i/++i; clrscr(); printf("%d %d",a,i); getch(); }
#include
#include void main(int argc,char *argv[],char *env[]) { int i; clrscr(); printf("The command line arguments are %d\n",argc); printf("\n"); for(i=0;i
#include #include void main() { char c[23]; int i=0; clrscr(); printf("Enter a string :"); while( (c[i++]=getchar())!='\n'); c[--i]='\0'; printf("%s",c); getch(); }
#include typedef char *cp; void main() { const cp p=0; clrscr(); strcpy(p,"kumara"); printf("%d",*++p); getch(); } /*Here the important is the typedef declaration statement .The typedef statement declares that cp is a char * and in the main() we are declaraing the variable p as const cp here the typedef works differently from #define it makes the pointer to be const and not the object*/
#include #include #include void main() { char *p; char s[]="dhankumaraa is a goood boy and he sincerely love his country"; clrscr(); p=s; puts(p); strcpy(p,s); puts(p); strncpy(p,s,sizeof(p)); puts(p); strncpy(p,s,sizeof(p)+1); puts(p); strncpy(p,s,sizeof(s)+1); puts(p); getch(); }
#include #include void main() { int i,j; printf("Enter two numbes:"); scanf("%d %d",&i,&j); j=i^j; i^=j; j^=i; clrscr(); printf("%d %d",i,j); getch(); }
#include #include
void main() { int a; clrscr(); printf("%d %d",sizeof a,sizeof (int )); getch(); } /*The build in datatype requires the paranthesis */ /*Where as the variable doesn't neeed the paranthesis */
#include #include void main() { char *p; char *q; clrscr(); printf("%Fp %Np",p,q); getch(); } /*pointer size modifier are 'F' and 'N', hence the address for the two pointers differs */
#include #include void main()
{ enum ra { j,k}; enum ra o; clrscr(); printf("%d %d\n",j,k); o=j; o+=6; printf("%d %d\n",o,j); getch(); }
printf("%s %s %d %f",o.c,o.p,o.i,o.f); getch(); } /*During the initialization of the structure variable if the first fields value is given and the remainig members values are not given ,then the other meebres are initialized to zero automatically */
#include #include void main() { struct { struct { int i; char c; }o={12,'A'}; }w; clrscr(); printf("%d",w.o.i); getch(); } /*the above structure is attempting to initialize a struct member which inturn a structure ,that is not possible in C hence the error*/
#include #include void main() { struct a { char c[7]; char *p; }; struct b { char *o; struct a u; }; struct b w={"kumar","ravi","vijay"};
*p=0; ++*p++; clrscr(); printf("%d",i); getch(); } /*the program contains a critical statement ++*p++ thi increments the *p only once that is in the preincrement operation and left the value being incremented postwards */ /*if you include a paranthesis before the post increment operator the post incrementation takes place */
#include #include void main() { static int a[3][3][3]={
{1,2,3,4,5,6,7,8,9}, {2,4,6,8,10,12,14,16,18}, {3,6,9,12,15,18,21,24,27} }; static int *ptr[]={ a[0][0],a[0][1],a[0][2], a[1][0],a[1][1],a[1][2], a[2][0],a[2][1],a[2][2] }; int *ptr1[]={a[0],a[1],a[2]}; int **ptr2=ptr,i; clrscr(); printf("\n"); for(i=0;i<=8;i++) { printf("%d\n",**ptr2); ptr2++; //printf("%d\n",*(ptr[i])); }
#include #include void main() { clrscr(); printf("%d",sizeof '2'); getch(); } /*THE same program will produce different result in the turboc2 compiler that is in pure C compiler the value will be 2 because the character in C are stored as integers hence two butes are required */
#include #include #define min(i,j) ((i)<(j)?(i):(j)) int minf(int i,int j) { return (i
{ printf("g() is called \n"); return 21; } void main() { clrscr(); printf("The macro\n"); printf("%d\n",min(f(),g())); printf("The function \n"); printf("%d\n",minf(f(),g())); getch(); }
#include #include int i; int f() { printf("f() is called \n"); i+=4; return i; } int g() { printf("g() is called \n"); i+=2; return i; } void main() { clrscr(); printf("%d %d",f(),g()); getch(); }
#include #include void main() {
clrscr(); f(12,122); getch(); } f() { printf("kumar"); } /*A function , by default ,sends a integer type parameters and returns an int type value */
#include #include /*int i=12;*/ void main() { static int h=i; clrscr(); printf("%d ",h); getch(); } /*static variable can only be initialized initialized by a constant expression expression or a constant value and static variable cannot be initialized by the local variable or by using a global variable */
#include #include void main() { static int i=printf("kumar"); clrscr(); printf("%d",i); getch(); } /*you cannot initialize the static variable by using a run time function hence the error */
#include void main() {
int i; clrscr(); printf("Enter an integer :"); scanf("%d",&i); switch(i) { case 1: printf("kumar"); continue; } getch(); } /*'continue' statement cannot be placed inside the switch statement */ /*In the similar way 'break' statement cannot be placed inside the for loop*/
#include #include void main() { static int i; i=printf("kumar\n"); printf("%d",i); getch(); } /*here the program works because first the static variable does not have any initializer hence it is initialized to zero but when the printf() comes in to picture ,it is a run time function it returns the no of characters outputed hence the static variable is given the value of 4 */
return s; } /*Here the output is a magic because the variable s[] is an automatic variable ,that is the variable is created and destroyed inside the function but you are trying to return the address the destroyed variable hence the garbage value printed */ /*The solution to this problem is to declare the variable as static storage class */
#include #include extern void f() { printf("kumar"); } void main() { clrscr(); f(); getch(); } /*you can also use the keywords extern and static in the function definition normally you cannot define a extern variable (ie.) a variable name preceded by the keyword extern cannot define a new value (ie..) extern int n=12; this statement tries to define a variable which is d efined elsewhere*/