Você está na página 1de 23

`

C Programming Techniques

SASTRA UNIVERSITY

PAGE 1 of 22

SNO

PROGRAM
main() { int i=3; switch(i) { default:printf("zero"); case 1:printf("one"); break; case 2:printf("two"); break; case 3:printf("three"); break; } } main() { int c= --2; printf("c=%d",c); } main() { int i=10; i=!i>14; Printf ("i=%d",i); } Answer : three

OUTPUT

Explanation : The default case can be placed anywhere inside the loop. It is executed only when all other cases doesn't match.

Answer : c=2 Explanation: Here unary minus (or negation) operator is used twice. Same maths rules applies, i.e. minus * minus= plus. Answer: i=0 Explanation: In the expression !i>14 , NOT (!) operator has more precedence than > symbol.! is a unary logical operator. !i (!10) is 0 (not of true is false).0>14 is false (zero). Answer: 50 Explanation: The preprocessor directives can be redefined anywhere in the program. So the most recently assigned value will be taken. Answer: 100

#define a 10 main() { #define a 50 printf("%d",a); } #define clrscr() 100 main() { clrscr(); printf("%d\n",clrscr()); } main() { int i=400,j=300; printf("%d..%d"); }

main() { int i=1; while (i<=5) { printf("%d",i); if (i>2) goto here; i++; } }

fun() { here: printf("PP"); }

Answer: 400..300 Explanation: printf takes the values of the first two assignments of the program. Any number of printf's may be given. All of them take only the first two values. If more number of assignments given in the program, then printf will take garbage values. Answer: Compiler error: Undefined label 'here' in function main Explanation: Labels have functions scope, in other words The scope of the labels is limited to functions. The label 'here' is available in function fun() Hence it is not visible in function main.

C Programming Techniques
8 main() { int i=1,j=2; switch(i) { case 1:printf("GOOD"); break; case j:printf("BAD"); break; } } main() { int i=0; for(;i++;printf("%d",i)); printf("%d",i); }

SASTRA UNIVERSITY

PAGE 2 of 22

Answer: Compiler Error: Constant expression required in function main. Explanation: The case statement can have only constant expressions (this implies that we cannot use variable names directly. so an error). Note: Enumerated types can be used in case statements Answer: 1 Explanation: Before entering into the for loop the checking condition is "evaluated". Here it evaluates to 0 (false) and comes out of the loop, and i is incremented (note the semicolon after the for loop). Answer: i = -1, +i = -1 Explanation: Unary + is the only dummy operator in C. Where-ever it comes you can just ignore it just because it has no effect in the expressions (hence the name dummy operator). Answer : 0 times

10

main() { int i=-1; +i; printf("i = %d, +i = %d \n",i,+i); }

11

How many times "IndiaBIX" is get printed? int main() { int x; for(x=-1; x<=10; x++) { if(x < 5) continue; else break; printf("IndiaBIX"); } return 0; } int main() { int j=1; while(j <= 255) { printf("%c %d\n", j, j);

12

Answer: 255 times Explanation: The while(j <= 255) loop will get executed 255 times. The size short int(2 byte wide) does not affect the while() loop. Answer:C-program Explanation: Step 1: char str[]="C-program"; here variable str contains "C-program". Step 2: int a = 5; here variable a contains "5". Step 3: printf(a >10?"Ps\n":"%s\n", str); this statement can be written as if(a > 10)

j++;}}

13

int main() { char str[]="C-program"; int a = 5; printf(a >10?"Ps\n":"%s\n", str); return 0; }

C Programming Techniques

SASTRA UNIVERSITY
{ printf("Ps\n");

PAGE 3 of 22

14

int main() { int a = 500, b = 100, c; if(!a >= 400) b = 300; c = 200; printf("b = %d c = %d\n", b, c); return 0; }

} else { printf("%s\n", str); } Here we are checking a > 10 means 5 > 10. Hence this condition will be failed. So it prints variable str. Hence the output is "C-program". Answer: b = 100 c = 200 Explanation: Initially variables a = 500, b = 100 and c not assigned. Step 1: if(!a >= 400) Step 2: if(!500 >= 400) Step 3: if(0 >= 400) Step 4: if(FALSE) Hence the if condition failed. Step 5: So, variable c is assigned to value '200'. Step 6: printf("b = %d c = %d\n", b, c); prints value of b and c. Hence the output is "b = 100 c = 200" Answer: x and y are equal

is

is a It

15

int main() { int x = 3; float y = 3.0; if(x == y) printf("x and y are equal"); else printf("x and y are not equal"); return 0; }

Explanation: Step 1: int x = 3; here variable x is an integer type and initialized to '3'. Step 2: float y = 3.0; here variable y is an float type and initialized to '3.0' Step 3: if(x == y) here we are comparing if(3 == 3.0) hence this condition is satisfied. Hence it prints "x and y are equal". Answer: Error

16

17

void main() { int i; for(i=1;i<4,i++) switch(i) case 1: printf("%d",i);break; { case 2:printf("%d",i);break; case 3:printf("%d",i);break; } switch(i) case 4:printf("%d",i); } void main() { int i=7; printf("%d",i++*i++); }

Answer: 56

C Programming Techniques
18

SASTRA UNIVERSITY
Answer: 16,21

PAGE 4 of 22

19

20

21

22

23

main() { int i=0; for(i=0;i<20;i++) { switch(i) { case 0:i+=5; case 1:i+=2; case 5:i+=5; default i+=4; break; } printf("%d,",i); } } main() { int i=3; i=i++; printf(%d,i); } main() { static i=3; printf("%d",i--); return i>0 ? main():0; } main() { int x=20,y=35; x = y++ + x++; y = ++y + ++x; printf("%d %d\n",x,y); } main() { int x=5; printf("%d %d %d\n",x,x<<2,x>>2); } #define swap1(a,b) a=a+b;b=a-b;a=a-b; main() int swap2(int { a,int b) int x=5,y=10; { swap1(x,y); int temp; printf("%d %d\n",x,y); temp=a; swap2(x,y); b=a; printf("%d %d\n",x,y); a=temp; } return;} void main() { int i; i=1; i=i+2*i++; printf("%d",i); }

Answer: 4

Answer: 321

Answer: 57 94

Answer: 5 20 1

Answer: 10 5

24

Answer. 4

C Programming Techniques
25 void main() { int i=7; printf("%d",i++*i++); } void main() { int const * p=5; printf("%d",++(*p)); }

SASTRA UNIVERSITY
Answer: 56

PAGE 5 of 22

26

Answer: Compiler error: Cannot modify a constant value. Explanation: p is a pointer to a "constant integer". But we tried to change the value of the "constant integer". Answer: 2222223465 Explanation: Initially pointer c is assigned to both p and q. In the first loop, since only q is incremented and not c , the value 2 will be printed 5 times. In second loop p itself is incremented. So the values 2 3 4 6 5 will be printed.

27

28

main() { int c[ ]={2.8,3.4,4,6.7,5}; int j,*p=c,*q=c; for(j=0;j<5;j++) { printf(" %d ",*c); ++q; } for(j=0;j<5;j++) { printf(" %d ",*p); ++p; } } main() { int i=-1,j=-1,k=0,l=2,m; m=i++&&j++&&k++||l++; printf("%d %d %d %d %d",i,j,k,l,m); }

Answer: 00131 Explanation : Logical operations always give a result of 1 or 0 . And also the logical AND (&&) operator has higher priority over the logical OR (||) operator. So the expression i++ && j++ && k++ is executed first. The result of this expression is 0 (-1 && -1 && 0 = 0). Now the expression is 0 || 2 which evaluates to 1 (because OR operator always gives 1 except for 0 || 0 combination- for which it gives 0). So the value of m is 1. The values of other variables are also incremented by 1. Answer: H Explanation: * is a dereference operator & is a reference operator. They can be applied any number of times provided it is meaningful. Here p points to the first character in the string "Hello". *p dereferences it and so its value is H. Again & references it to an address and * dereferences it to the value H.

29

main() { char *p; p="Hello"; printf("%c\n",*&*p); }

C Programming Techniques
30 main() { char not; not=!2; printf("%d",not); }

SASTRA UNIVERSITY
Answer: 0

PAGE 6 of 22

31

32

33

void main() { int i,j,k; for(i=1;i<=5;i++) for(j=1;j<=i;j++) for(k=1;k<=j;k++) if(i==k && j==k) printf("%d\n",i); } void main() { int i,j,k; for(i=1;i<=5;++i) for(j=1;j<=i;++j) for(k=1;k<=j;++k) if(i!=k && k!=j) printf("%d",i); } void main() { int i,j; for(i=5;i>=1;i--) for(j=1;j<=i;j++) if(i>j); printf("%d\n",i); printf("%d\n",i); } void main() { int i,j; for(i=5;i>=1;--i) for(j=1;j<=i;++j) if(i>j) printf("%d",i); printf("%d,i); } void main() { int i =0;j=0; if(i && j++) printf("%d..%d",i++,j); printf("%d..%d,i,j); }

Explanation: ! is a logical operator. In C the value 0 is considered to be the boolean value FALSE, and any non-zero value is considered to be the boolean value TRUE. Here 2 is a non-zero value so TRUE. !TRUE is FALSE (0) so it prints 0. Answer: 1 2 3 4 5

Answer: 23334444445555555555

Answer: 0 0

34

Answer: 55554443320

35

Answer: 0..0 Explanation: The value of i is 0. Since this information is enough to determine the truth value of the boolean expression. So the statement following the if statement is not executed. The values of i and j remain unchanged and get printed.

C Programming Techniques
36 void main() { int i; i = abc(); printf("%d",i); }

SASTRA UNIVERSITY
Answer: 1000

PAGE 7 of 22

37

abc() { _AX = 1000; } int i; main() { int t; for ( t=4;scanf("%d",&i)-t;printf("%d\n",i)) printf("%d--",t--); } // If the inputs are 0,1,2 find the o/p

Explanation: Normally the return value from the function is through the information from the accumulator. Here _AH is the pseudo global variable denoting the accumulator. Hence, the value of the accumulator is set 1000 so the function returns value 1000. Answer: 4--0 3--1 2--2 Explanation: Let us assume some x= scanf("%d",&i)-t the values during execution will be, t 4 3 2 Answer: hello i 0 1 2 x -4 -2 0

38

void main() { int a= 0; int b = 20; char x =1; char y =10; if(a,b,x,y) printf("hello"); }

39

void main() { unsigned int i; for(i=1;i>-2;i--) printf("c aptitude"); } void main() { int i=1,j=2,k=3; for(i=1;i<=5;++i) for(j=1;j<=i;++j) for(k=1;k<=j;++k) if(i!=k && k!=j) printf("%d",i); }

40

Explanation: The comma operator has associativity from left to right. Only the rightmost value is returned and the other values are evaluated and ignored. Thus the value of last variable y is returned to check in if. Since it is a non zero value if becomes true so, "hello" will be printed. Explanation: i is an unsigned integer. It is compared with a signed value. Since the both types doesn't match, signed is promoted to unsigned value. The unsigned equivalent of -2 is a huge value so condition becomes false and control comes out of the loop. Answer: 23334444445555555555

C Programming Techniques
41 main() { float me = 1.1; long double you = 1.1; if(me==you) printf("SASTRA"); else printf("SRC");

SASTRA UNIVERSITY
Answer: SRC

PAGE 8 of 22

42

void main() { static int i=5; if(--i) { main(); printf("%d ",i); } }

Explanation: For floating point numbers (float, double, long double) the values cannot be predicted exactly. Depending on the number of bytes, the precession with of the value represented varies. Float takes 4 bytes and long double takes 10 bytes. So float stores 0.9 with less precision than long double. Rule of Thumb: Never compare or at-least be cautious when using floating point numbers with relational operators (== , >, <, <=, >=,!= ) . Answer: 0000 Explanation: The variable "I" is declared as static, hence memory for I will be allocated for only once, as it encounters the statement. The function main() will be called recursively unless I becomes equal to 0, and since main() is recursively called, so the value of static I ie., 0 will be printed every time the control is returned. Answer: Ok here Explanation: Printf will return how many characters does it print. Hence printing a null character returns 1 which makes the if statement true, thus "Ok here" is printed. Answer: -1 Explanation: Unary + is the only dummy operator in C. So it has no effect on the expression and now the while loop is, while(i--!=0) which is false and so breaks out of while loop. The value 1 is printed due to the postdecrement operator Answer: 1 Explanation: The expression can be treated as i = (++i==6), because == is of higher precedence than = operator. In the inner expression, ++i is equal to 6 yielding true(1). Hence the result.

43

44

void main() { int i; char a[]="\0"; if(printf("%s\n",a)) printf("Ok here \n"); else printf("Forget it\n"); } void main() { int i=0; while(+(+i--)!=0) i-=i++; printf("%d",i); }

45

main() { int i=5; printf(%d,i=++i==6); }

C Programming Techniques
46 void main() { static int i; while(i<=10) (i>2)?i++:i--; printf(%d, i); }

SASTRA UNIVERSITY
Answer: 32767

PAGE 9 of 22

47

void main() { int i=5,j=10; i=i&=j&&10; printf("%d %d",i,j); }

Explanation: Since i is static it is initialized to 0. Inside the while loop the conditional operator evaluates to false, executing i--. This continues till the integer value rotates to positive value (32767). The while condition becomes false and hence, comes out of the while loop, printing the i value. Answer: 1 10 Explanation: The expression can be written as i=(i&=(j&&10)); The inner expression (j&&10) evaluates to 1 because j==10. i is 5. i = 5&1 is 1. Hence the result 1. Answer: 5 4 3 2 1 Explanation: When static storage class is given, it is initialized once. The change in the value of a static variable is retained even between the function calls. Main is also treated like any other ordinary function, which can be called recursively. Answer: i=0j=0k=0 Explanation: Since static variables are initialized to zero by default. Answer:45545 Explanation:The arguments in a function call are pushed into the stack from left to right. The evaluation is by popping out from the stack. and the evaluation is from right to left, hence the result. Answer: Compilation error. Explanation: We cannot use keyword in conditional operator. Answer: i = -1, -i = 1 Explanation: -i is executed and this execution doesn't affect the value of i. In printf first you just print the value of i. After that the value of the expression -i = (-1) is printed. return

48

void main() { static int var = 5; printf("%d ",var--); if(var) main(); } void main() { static int i=i++, j=j++, k=k++; printf(i = %d j = %d k = %d, i, j, k); } void main() { int i=5; printf("%d%d%d%d%d%d",i++,i--,++i,--i,i); }

49

50

51

52

void main(){ int res; res= 56>76 ? return 0:return 1; printf("%d",res); } main() { int i=-1; -i; printf("i = %d, -i = %d \n",i,-i); }

C Programming Techniques
53

SASTRA UNIVERSITY
Answer: Compiler error

PAGE 10 of 22

54

main() { const int i=4; float j; j = ++i; printf("%d %f", i,++j); } main() { int k=1; printf("%d==1is""%s",k,k==1?"TRUE":"FALSE"); }

Explanation: i is a constant. you cannot change the value of constant

Answer: 1==1 is TRUE Explanation: When two strings are placed together (or separated by white-space) they are concatenated (this is called as "stringization" operation). So the string is as if it is given as "%d==1 is %s". The conditional operator( ?: ) evaluates to "TRUE". Answer: TRUE Explanation: The input program to the compiler after processing by the preprocessor is, main() { if(0) puts("NULL"); else if(-1) puts("TRUE"); else puts("FALSE"); } Preprocessor doesn't replace the values given inside the double quotes. The check by if condition is boolean value false so it goes to else. In second if -1 is boolean value true hence "TRUE" is printed. Answer: 1

55

#define FALSE -1 #define TRUE 1 #define NULL 0 main() { if(NULL) puts("NULL"); else if(FALSE) puts("TRUE"); else puts("FALSE"); }

56

main() { int i; printf("%d",scanf("%d",&i)); input }

// value 10 is

57

58

main() { 100; printf("%d\n",100); } main() { int i=1,j=2; switch(i) { case 1: printf("MURGA MURGA"); break; case j: printf("GOVINDA GOVINDA"); break; } }

Explanation: Scanf returns number of items successfully read and not 1/0. Here 10 is given as input which should have been scanned successfully. So number of items read is 1. Answer: 100

Answer: Compiler Error: Constant required in function main.

expression

C Programming Techniques
59

SASTRA UNIVERSITY
Answer: i = -1, +i = -1

PAGE 11 of 22

60

61

main() { int i=-1; +i; printf("i = %d, +i = %d \n",i,+i); } main() { int y; scanf("%d",&y); // input given is 2000 if( (y%4==0 && y%100 != 0)|| y%100==0) printf("%d is a leap year"); else printf("%d is not a leap year"); } main() { const int i=4; float j; j = ++i; printf("%d %f", i,++j); } main() { int i=5,j=6,z; printf("%d",i+++j); } main() { int i=abc(10); printf("%d\n",--i); } void main() { char a[]="12345\0"; int i=strlen(a); printf("%d",++i); }

Answer: 2000 is a leap year

Answer: Compiler error

62

Answer: 11 Explanation: the expression i+++j is treated as (i++ + Answer: 9 int abc(int i) { return(i++); } Explanation: return(i++) it will first return i and then increments. i.e. 10 will be returned. Answer: 6 Explanation: The char array 'a' will hold the initialized string, whose length will be counted from 0 till the null character. Hence the 'I' will hold the value equal to 5, after the pre-increment in the printf statement, the 6 will be printed Answer: Garbage-value 0

63

64

65

66

main() { int x,y=2,z,a; if(x=y%2) z=2; a=2; printf("%d %d ",z,x); } void main() { int i=32765; while(i++!=0); printf("%d",i); printf("%d",i); }

Answer: 1 Explanation: Note the semicolon after the while statement. When the value of i becomes 0 it comes out of while loop. Due to postincrement on i the value of i while printing is 1.

C Programming Techniques
67 main () { int i, j, k; i = 3; j =2*(i++); k =2*(++i); printf(j=%d,k=%d,j,k); } main() { int i; i = 0; do { --i; printf (%d,i); i++; } while (i >= 0); } switch (option) { case H : printf(Hello); case W : printf(Welcome); case B : printf (Bye); break; }// option = H

SASTRA UNIVERSITY
Answer: j = 6, k = 10.

PAGE 12 of 22

Explanation : In the expression j = 2 * (i++) the value of i is used before incrementing and in expression k =2*(++i); will get incremented first and then used in the expression Answer: Infinite loop Explanation: In every iteration value of i is decremented and then incremented so remains 0 and hence a Infinite Loop.

68

69

Answer: Hello Welcome Bye Explanation : If option = H then the first case is true so Hello gets printed but there is no break statement after this case to come out of the switch statement so the program execute all other case statements also and Hello Welcome Bye get printed. Answer : 1 Explanation: 1. !((b + c) > (a + 10)) 2. !((6 + 7) > (5+10)) 3. !(13 > 15) 13 is less than 15 so it will return False (0 ). 4. !(0). Not of 0 is 1. Answer: 10 Explanation : Condition part of for loop ( j>0, i<10 ) is separated by commas which means that compiler will use the value which is at right hand side of comma i.e of i<10 so the loop will execute till the value of i is less than 10. Answer : ic Explanation: Gyantonic is a constant string and statement printf(3+Gyantonic+4); will skip seven(3 + 4) characters of the string before printing. Answer : t Explanation: Gyantonic is a constant string and character at index 4 will get printed.

70

Suppose a,b,c are integer variables with values 5,6,7 respectively. What is the value of the expression: !((b + c) > (a + 10))

71

main () { int i, j; for (i=0,j=5; j>0, i<10; i++, j--) printf(\nGyantonic.com); } How many times Gyantonic.com will get printed?

72

main() { printf(3+Gyantonic+4); }

73

main() { printf(%c,Gyantonic[4]); }

C Programming Techniques
74

SASTRA UNIVERSITY
Answer: Gyantonic

PAGE 13 of 22
.com

main() { printf(Gyantonic \t .com); } int fun(int,int,int); void main() { int i=10; i = fun(++i,i++,i++); printf("%d\n",i); i=10; i = fun(++i,i++,++i); printf("%d",i); } void main() { int i=2/5; clrscr(); switch(i) { default:printf("0"); case 1: printf("1"); case 2: printf("2"); } } main() { int a, b, c; a = 10; b = 20; c = printf(%d,a) + ++b; printf (%d,c);

75

int fun(int a,int b,int c) { int d; d= a +b +c; return d; }

Explanation: printf() print Gyantonic then a tab and then print .com. It is same as writing it as printf(Gyantonic\t.com); Answer: 34 35

76

Answer: 0 1 2

77

Answer: 23 Explanation: printf() will return no. of bytes it printed. Expression becomes c = 2 + ++b; then value of b is incremented before addition. Answer: Infinite loop int i; for (i=9 ; i ; i=i-2) { printf(\n%d,i); } Explanation : Above loop will iterate till i have non zero value. Initial value of i is 9 and it get decremented by 2 in every iteration ( 9, 7, 5, 3, 1, -1, -3 .). Value of i will never become 0 and loop will run infinitely. Answer : 5 11 Explanation: Value in a function get passed from right to left. First i++ get passed and it make i = 11. Answer: 0 Explanation: a==b is a expression and will return 1 (true) or 0 (False) depending on the values of a and b. Here a and b are not equal so 0 is printed

78

} main() {

} 79 main() { int i; i = 10; printf(%d\t,5,6); printf(%d, i , i++); } What the below statement will print if a=10 and b = 20? printf( %d,a==b);

80

C Programming Techniques
81

SASTRA UNIVERSITY
Answer: 11 0

PAGE 14 of 22

What the below statement will print if a=10? printf( %d %d,a, !a++);

82

main() { int i; i =10; if (i ==20 || 30) printf(True); else printf(False); }

Explanation: Values in the function get passed from right to left. First !a++ get processed which pass zero as argument and make a equal to 11. Answer : True Explanation: i==20 is a expression which will return TRUE or FALSE depending on the value of i. In this program it will return 0 so the statement become If ( 0 || 30) 30 is a nonzero value which means TRUE (1) in C when ORed with 0 will result TRUE. Answer: t Explanation: Gyantonic is a constant string and character at index 4 will get printed. Answer : False Explanation : comma(,) operator returns the value which at the right hand side of , . if statement become if(0)

83

84

main() { printf(%c,4[Gyantonic]); } main() { if (1,0) printf(True); else printf(False); }

85

main() { int i , j ,*ptr, *ptr1; i =10; j = 10; ptr = &i; ptr1 = &j; if (ptr == ptr1) printf(True); else printf(False); } main() { int i; for (i=20, i =10 ; i<=20 ; i++); printf(\n%d,i); }

Answer : False Explanation: In this program we are comparing the addresses contained by ptr & ptr1 not the value at those addresses and pointers ptr and ptr1 have the addresses of different variables so above condition is false.

86

Answer : 21 Explanation: i will start from 10.

C Programming Techniques
87

SASTRA UNIVERSITY
Answer : 11

PAGE 15 of 22

main()
{ int i,j; i = 10; for (j=i==10 ; j<=10 ; j++); printf(\n%d,j) }

Explanation: Expression i ==10 return 1 and j get initialized to 1.

88

main ( ) { int a = 1 ; while ( a <= 100) ; printf ( %d, a++ ) ; }

Answer : Infinite Loop Explanation: Loop will execute infinite no of times because of the ; at the end while loop. Answer : Infinite Explanation: There is no condition in the main() to stop the recursive calling of the main() hence it will be called infinite no of times. Answer: 1 2 3 4 Explanation: ptr contains the base address of the array and printf() is printing the value at the current address contained by ptr and then incrementing the pointer to point to the next array element address.

89

main( ) { printf ( \nMain called again ) ; main( ) ; } main( ) { int int ptr for }

90

gyan[] = { 1, 2, 3, 4, 5 }; i, *ptr ; = gyan ; ( i = 0 ; i <4 ; i++ ) printf ( \n%d, *ptr++ ) ;

91

main( ) { int int ptr for { } }

Answer: 20 30 40 50 gyan[] = { 10, 20, 30, 40, 50 }; i, *ptr ; = gyan; ( i = 0 ; i <4 ; i++ ) fun(ptr++); printf ( \n%d, *ptr ) ; Explanation: When we call the function fun() the current address contained by it will get passed and then it get incremented. For ex. If the base address is of the array is 100 then successive elements are stored at 102, 104, 106 and 108. In the first call 100 get passed to fun() and ptr becomes 102. Now the called function fun() manipulates the value stored at 100 and in the main() function values at the address contained by ptr are getting printed. Answer: H Explanation: ++*ptr++ will retrieve the value currently pointed by ptr i.e G and then increment the value and will print H.Answer: 3

void fun(int *i) { *i = *i + 1;


92 main() { char *ptr = Gyantonic.com; printf(%c, ++*ptr++); } If abc is the input find the output. char x,y,z; printf(%d,scanf(%c%c%c,&x,&y,&z);

93

C Programming Techniques
94 char c=a; while(c++<=z) putchar(xxx); int x,y=2,z,a; x=(y*=2)+(z=a=y); printf(%d,x); printf(%c,100); for(i=3;i<15;i+=3) printf(%d,i); for(i=1;i<5;i++) if(i==3) continue; else printf(%d,i); if(a=0) printf(a is zero); else printf(a is not zero);

SASTRA UNIVERSITY
Answer: error

PAGE 16 of 22

95

Answer: 8 Answer: ASCII value of 100 Answer: 3 6 9 12 Answer: 1 2 4

96 97 98

99

Answer: a is not zero

100 if(a=7) printf(a is seven); else printf(a is not seven); 101 int k=-7; printf(%d,0<!k);

Answer: a is seven

Answer: 0

102 main() { static int a[20]; int i = 0; a[i] = i ; printf("%d, %d, %d\n", a[0], a[1], i); } int a=4,b=6; printf(%d,a==b); int a=4,b=6; printf(%d,a!=b); int a=4,b=6; printf(%d,a=6); main() { int x=4, y, z; y = --x; z = x--; printf("%d, %d, %d\n", x, y, z); }

Answer:0,0,0

103 104 105 106

Answer: 0 Answer: prints non zero value Answer: 6 Answer:2,3,3

107 main() { inti=5; if(i==5) return; else printf(I is not five); printf(over);}

Answer: execution termination without printing anything

C Programming Techniques

SASTRA UNIVERSITY
Answer: infinite Answer: 3 7 11

PAGE 17 of 22

108 while(printf(%d,printf(az))) printf(by); 109 for(i=3;i<15;i+=3) { printf(%d,i); ++i; } 110 if(2<1); else x=(2<0)?printf(one):printf(four); 111 for(putchar(a);putchar(0);putchar(c)) putchar(b); 112 int main() { int x=55; printf("%d,%d,%d\n",x<=55, x=40, x>=10); return 0; } 113 Vmain() { int i=2; { int i=4,j=5; printf(%d%d,i,j); } printf(%d%d,i,j); } 114 printf(ab,cd,ef); 115 i=6720;j=4; while((i%j)==0) { i=i/j; j=j+1; }printf(%d,j); 116 main() { static int x[]={1,2,3,4,5,6,7,8}; int i; for(i=2;i<6;++i) x[x[i]]=x[i]; for(i=0;i<8;++i) printf(%d,x[i]); } 117 main() { int x=2,y=5; if(x<y) return(x=x+y); else printf(z1); printf(z2); } 118 int k=-9; printf(%d,0<!k);

Answer: four

Answer: a Answer: 1,40,1

Answer: compiler error j is undefined symbol

Answer: ab Answer:9

Answer:1 2 3 3 5 5 7 8

Answer: compiler error main() cannot return a value

Answer:0

C Programming Techniques
119 int v=3,*pv=&v; printf(%d%d,v,*pv);

SASTRA UNIVERSITY
Answer:3 3 Answer:30

PAGE 18 of 22

120 int main() { int k, num=30; k =(num>5 ? (num <=10 ? 100 : 200): 500); printf("%d\n", num); return 0; } 121 int v=3,*pv=0; printf(%d%d,*pv,v) 122 int a=5,*b=&a; printf(%d,a * b); 123 int a=5,*b=&a; printf(%d,a ** b); 124 main() { float a=.5,b=.7; if(b<.7) if(a<.5) printf(TELO); else printf(LTTE); else printf(JKLF); } 125 void max(int x,int y,int m) { if(x>5) m=x; else m=y;} int main() { int i=20,j=5,k=0; max(i,j,k); printf(%d,k); } 126 main() { int a=1,b=2,c=3; printf(%d,a+=(a+=3,5,a)); } 127 for(i=0;i<10;++i) printf(%d,i & 1); what is the output? 128 printf(%d,10?0?5:11:12); 129 what is the output? printf(%d, ++5);

Answer: 0 3 Answer: error Answer: 25 Answer: LTTE

Answer: 0

Answer: 8

Answer : 0101010101 Answer: 11

Answer: error Lvalue required

C Programming Techniques
130 void main() { char word[]="PARIMALA"; char *p = word; printf("\n %s",word); printf("\n %s",word+2); printf("\n %s",p); printf("\n %c",*p+1); printf("\n %c",*p+2); printf("\n %c",*(p+2)); }

SASTRA UNIVERSITY
Answer: PARIMALA RIMALA PARIMALA A R R

PAGE 19 of 22

131 void main() { printf("\n 1. PROGRAMMING IN C"); printf("\n 2. %c",*("PROGRAMMING IN C")); printf("\n 3. %d",*("PROGRAMMING IN C")); printf("\n 4. %c",*("PROGRAMMING IN C"+8)); printf("\n 5. %s","PROGRAMMING IN C"); } 132 void main() { char name[20]="Saadhana", *p=name; printf("\n %c",*(name+2)); printf("\n %c",*(p+1)); } 132 void main() { int a=10,*p=&a,**q=&p; printf("\n%d",a); printf("\n%d",*p); printf("\n%d",**q); printf("\n%d",*q); } 133 void main() { int a[5]={11,12,13,14,15},*p=a,i; printf("%d",*p+1); for(i=1;i<5;i++) printf("\t %d",i[a]); } 134 void main() { int a=10,*p=&a; printf("\n%d",*p); printf("\n%d",*p+1); printf("\n%d",*p+2); printf("\n%d",*(p+1)); } 135 intfunc (int x) { if (x<=0) return(1); returnfunc(x -1) +x; } main() { printf("%d\n",func(5)); }

Answer: 1.PROGRAMMING IN C 2.P 3.80 4.I 5.PROGRAMMING IN C Answer: a a

Answer: 10 10 10 -12 Answer: 12 12 13 14 15

Answer: 10 11 12 0

Answer : 16

C Programming Techniques

SASTRA UNIVERSITY
Answer:0

PAGE 20 of 22

136 void main() { static int i; int j; for(j=0;j<=5;j+=2) switch(j) { case 1: i++;break; case 2: i+=2; case 4: i%=2;j=-1;continue; default: --i;continue; } printf("%d",i); }

137 int main(void) { int x=10,y=10; printf("left shift of 10 is %d \n",x<<1); printf("right shift of 10 is %d \n",y>>1); return 0; } 138 main() { func(1); } func(int i) { static char *str[] ={ One","Two","Three","Four"}; printf("%s\n",str[i++]); return; } 139 void main() { int a=1; b=2; c=3; *pointer; pointer=&c; a=c/*pointer; b=c; printf("a=%d b=%d",a,b); }

Explanation: In first iteration :j = 0 So, control will come to default, i = -1 Due to continue keyword program control will move to beginning of for loop In second iteration :j =2 So, control will come to case 2, i+=2 i = i+2 = -1 +2 =1 Then come to case 4, i%=2 i = i%2 = 1%2 = 1 j= -1 Due to continue keyword program control will move to beginning of for loop In third iteration :j = -1 +2 =1 So, control will come to case 1 i=2 In the fourth iteration:j = 1+ 2 =3 So, control will come to default, so i = 1 In the fifth iteration:j = 3 + 2 =5 So, control will come to default, so i = 0 In the sixth iteration:j = 5 + 2 =7 Since loop condition is false. So control will come out of the for loop. Answer: left shift of 10 is 20 right shift of 10 is 5 Explanation:Left shift (by 1 position) multiplies a number by two. Right shift divides a number by 2. Answer it will give warning because str is pointer to the char but it is initialized with more values if it is not considered then the answer is Two

Answer: Error

C Programming Techniques
140 int main() { extern int a; printf("%d\n", a); return 0; } int a=20; 141 int X=40; int main() { int x=20; printf("%d\n", x); return 0; }

SASTRA UNIVERSITY
Answer: 20

PAGE 21 of 22

Explanation: - During declaration we tell the datatype of the Variable. - During definition the value is initialized. Answer: 20 Explanation: Whenever there is conflict between a local variable and global variable, the local variable gets priority. Answer: 1 Explanation: Since x < y turns to be TRUE it is replaced by 1. Then 1 < z is compared and to be TRUE. The 1 is assigned to i. Answer: 0, 0, 0 Explanation: When an automatic array is partially initialized, the remaining elements are initialized to 0. Answer: The for loop would get executed infinite times Explanation: During the for loop execution scanf() ask input and then printf() prints that given input. This process will be continued repeatedly because, scanf() returns the number of input given, the condition is always true(user gives a input means it reurns '1'). Hence this for loop would get executed infinite times. Answer: 79 58

142 int main() { int x = 10, y = 20, z = 5, i; i = x < y < z; printf("%d\n", i); return 0; } 143 int main() { int a[5] = {2, 3}; printf("%d, %d, %d\n", a[2], a[3], a[4]); return 0; } 144 int main() { int i=5; for(;scanf("%s", &i); printf("%d\n", i)); return 0; }

145 main() { int x=20,y=35; y = ++y + ++x; x = y++ + x++; printf("%d %d\n",x,y); } 146 main() { int a[3][4]={1,2,3,4,4,3,2,1,7,8,9,0}; printf(%u,%u,a+1,&a+1) }

Answer: 65480,65496 Explanation: &a has type pointer to array of 3 arrays of 4i nts,totally of 24 ints.

C Programming Techniques

SASTRA UNIVERSITY

PAGE 22 of 22

Base address is 65472 a+1=65472+(4 ints*2bytes)=65480 &a+1=65472+24=65496

147 main() { int arr[1]={10}; printf(%d,0[arr]); }

Answer : 10 Explanation: It means arr[0]=10 Answer: 30 Explanation: i**j=3*3=9 9*I=9*3=27 27+*j=27+3=30 Answer: Mello Explanation: It is because the first position of the str array is replaced by M. Answer: K Explanation: The statement printf(str,k}is replaced with printf(%s,k); So,it will print k.

148 main() { int i=3,*j,K; j=&i; printf(%d,i**j*i+*j); } 149 main() { char str[20]=hello; char *const p=str; *p=M; printf(%s,str); } 150 main() { char *str; str=%s; printf(str,k}; }

References:
1.C Programming Interview Questions and Answers - www.indiabix.com 2. Test Your Skills - Yashavant P.Kanetkar Nov.1996. 3.Programming in C Dr.P.Pandiyaraja, The American College, Madurai.

Kindly send your suggestions for the improvement of the subject and feedback to: ariseawakeachieve@gmail.com

Você também pode gostar