0% found this document useful (0 votes)
61 views11 pages

Capti 1

The document provides examples and explanations related to C programming concepts like typedef, enums, switch case statements, functions, structures, unions and data types. Some key points include: - Enum assigns numbers starting from 0 if not explicitly defined. - Switch statements can only be used with integral data types. - Functions can be declared in different notations like ANSI C and Kernighan & Ritchie styles. - Passing arguments to functions uses call by value mechanism. - Structures and unions allocate memory based on largest data type. - Character constants are represented as integers in C.

Uploaded by

Karthik Roshan
Copyright
© Attribution Non-Commercial (BY-NC)
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)
61 views11 pages

Capti 1

The document provides examples and explanations related to C programming concepts like typedef, enums, switch case statements, functions, structures, unions and data types. Some key points include: - Enum assigns numbers starting from 0 if not explicitly defined. - Switch statements can only be used with integral data types. - Functions can be declared in different notations like ANSI C and Kernighan & Ritchie styles. - Passing arguments to functions uses call by value mechanism. - Structures and unions allocate memory based on largest data type. - Character constants are represented as integers in C.

Uploaded by

Karthik Roshan
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 11

C Programs

TYPEDEF
1. What can be said of the following program ? main() { enum Months {JAN =1,FEB,MAR,APR}; Months X = JAN; if(X==1) { printf("Jan is the first month"); } } a) Does not print anything b) Prints : Jan is the first month c) Generates compilation error d) Results in runtime error Answer : b 2.main() { extern int i; i=20; printf("%d",i); } Answer: Linker Error : Undefined symbol '_i' Explanation: extern storage class in the following declaration, extern int i; specifies to the compiler that the memory for i is allocated in some other program and that address will be given to the current program at the time of linking. But linker finds that no other variable of name i is available in any other program with memory space allocated for it. Hence a linker error has occurred . 3. enum colors {BLACK,BLUE,GREEN} main() { printf("%d..%d..%d",BLACK,BLUE,GREEN); return(1); } Answer: 0..1..2 Explanation: enum assigns numbers starting from 0, if not explicitly defined.

www.Technicalsymposium.com

4. Given the following statement enum day = { jan = 1 ,feb=4, april, may} What is the value of may? (a) 4 (b) 5 (c) 6 (d) 11 (e) None of the above Answer c) 6
SWITCH:

5. What is the output of the following program? main() { int l=6; switch(l) { default : l+=2; case 4: l=4; case 5: l++; break; } printf("%d",l); } a)8 b)6 c)5 d)4 e)none

Answer : c) 5
6. main() { int i=3; switch(i) { default:printf("zero"); case 1: printf("one"); break; case 2:printf("two"); break; case 3: printf("three"); break; } } Answer : three Explanation : The default case can be placed anywhere inside the loop. It is executed only when all other cases doesn't match.

7.

#include<stdio.h> main() { int i=1,j=2; switch(i) {

www.Technicalsymposium.com

case 1: printf("GOOD"); break; case j: printf("BAD"); break; } } 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. 8. main() { float i=1.5; switch(i) { case 1: printf("1"); case 2: printf("2"); default : printf("0"); } } Answer: Compiler Error: switch expression not integral Explanation: Switch statements can be applied only to integral types. 10. Output of the following program is 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); } } a) 0,5,9,13,17 b) 5,9,13,17 c) 12,17,22 d) 16,21 e) Syntax error Ans. (d)
www.Technicalsymposium.com

11. main() { int i; for(i=0;i<3;i++) switch(i) { case 1: printf("%d",i); case 2 : printf("%d",i); default: printf("%d"i); } } Answer: 011122
FUNCTIONS:

12. What is the output of the following program? main() { int x=20; int y=10; swap(x,y); printf("%d %d",y,x+2); } swap(int x,int y) { int temp; temp =x; x=y; y=temp; } a) 10,20 b) 20,12 c) 22,10 d)10,22 e)none

Answer : d)10,22
13. Which of the following about the following two declaration is true i ) int *F() ii) int (*F)() Choice : a) Both are identical b) The first is a correct declaration and the second is wrong c) The first declaraion is a function returning a pointer to an integer and the second is a pointer to function returning int d) Both are different ways of declarin pointer to a function Answer : c). 14. main() { printf("%p",main); }
www.Technicalsymposium.com

Answer: Some address will be printed. Explanation: Function names are just addresses (just like array names are addresses). main() is also a function. So the address of function main will be printed. %p in printf specifies that the argument is an address. They are printed as hexadecimal numbers. 15. main() { clrscr(); } clrscr(); Answer: No output/error Explanation: The first clrscr() occurs inside a function. So it becomes a function call. In the second clrscr(); is a function declaration (because it is not inside any function).

16.

main() { int i; printf("%d",scanf("%d",&i)); // value 10 is given as input here } Answer: 1 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. 17. main() { show(); } void show() { printf("I'm the greatest"); } Answer: Compier error: Type mismatch in redeclaration of show. Explanation: When the compiler sees the function show it doesn't know anything about it. So the default return type (ie, int) is assumed. But when compiler sees the actual definition of show mismatch occurs since it is declared as void. Hence the error. The solutions are as follows: 1. declare void show() in main() . 2. define show() before main(). 3. declare extern void show() before the use of show(). main() { main(); }

18.

www.Technicalsymposium.com

Answer: Runtime error : Stack overflow. Explanation: main function calls itself again and again. Each time the function is called its return address is stored in the call stack. Since there is no condition to terminate the function call, the call stack overflows at runtime. So it terminates the program and results in an error. 19. What are the following notations of defining functions known as? i. int abc(int a,float b) { /* some code */ } ii. int abc(a,b) int a; float b; { /* some code*/ } Answer: i. ANSI C notation ii. Kernighan & Ritche notation

20. What is printed when this program is executed main() { printf ("%d\n",f(7)); } f(X) { if ( x<= 4) return x; return f(--x); } a) 4 b)5 c) 6 d) 7 Answer : a) 21. what is printed when the following program is compiled and executed? int func (int x) { if (x<=0) return(1); return func(x -1) +x; } main()
www.Technicalsymposium.com

{ printf("%d\n",func(5)); } a) 12 b) 16 c) 15 d) 11 Answer : .b) 16. 22. Find the output: main() { int a==4 sqrt(a); printf("%d",a); } 1).2.0 2). 2 Answer : 2

3). 4.0

4). 4

23. Find the output main() { int a[]={ 2,4,6,8,10 }; int i; change(a,5); for( i = 0; i <= 4; i++) printf("\n %d",a[i]); } change( int *b, int n) { int i; for( i = 0; i < n; i++) *(b+i) = *(b+i) + 5; } Answer:

24. #include<studio.h> main() { func(1); } func(int i){ static char *str[] ={ "One","Two","Three","Four"}; printf("%s\n",str[i++]); return; } 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 */

www.Technicalsymposium.com

25. Find the output: #include<stdio.h> /* This problem was asked in PCS Bombay in a walk-in-interview Write a recursive function that calculates n * (n-1) * (n-2) * ....... 2 * 1 */ main() { int factorial(int n); int i,ans; printf("\n Enter a Number:"); scanf("%d",&i); ans = factorial(i); printf("\nFactorial by recursion = %d\n", ans); } int factorial(int n) { if (n <= 1) return (1); else return ( n * factorial(n-1)); } Answer : 26.Find the output #include <stdio.h> main() { int j,ans; j = 4; ans = count(4); printf("%d\n",ans); } int count(int i) { if ( i < 0) return(i); else return( count(i-2) + count(i-1)); } Answer : /* It is showing -18 as an answer */ 27. Find the output int x; main() { int x=0; {
www.Technicalsymposium.com

int x=10; x++; change_value(x); x++; Modify_value(); printf("First output: %d\n",x); } x++; change_value(x); printf("Second Output : %d\n",x); Modify_value(); printf("Third Output : %d\n",x); } Modify_value() { return (x+=10); } change_value() { return(x+=1); } Answer :

28. Consider the following program main() { int i=20,*j=&i; f1(j); *j+=10; f2(j); printf("%d and %d",i,*j); } f1(k) int *k; { *k +=15; } f2(x) int *x; { int m=*x,*n=&m; *n += 10; } The values printed by the program will be a) 20 and 55 b) 20 and 45 c) 45 and 45
www.Technicalsymposium.com

d) 45 and 55 e) 35 and 35 Answer : c 29. what is printed when the following program is compiled and executed? int func (int x) { if (x<=0) return(1); return func(x -1) +x; } main() { printf("%d\n",func(5)); } a) 12 b) 16 c) 15 d) 11 Answer : b
STRUCTURE AND UNION:

30. What is the size of the following union. Assume that the size of int =2, size of float =4 and size of char =1. Union Tag { int a; float b; char c; }; a)2 b)4 c)1 d) 7 Answer : b

DATA TYPES
31. What is th output of the following program? int x= 0x65; main() { char x; printf("%d\n",x) } a) compilation error b) 'A' c) 65 d) unidentified

www.Technicalsymposium.com

Answer : c

www.Technicalsymposium.com

You might also like