Sie sind auf Seite 1von 24

PLACES where float should not be used:

1 .mod(
fmod(x,y) - Calculates x modulo y, the remainder of x/y.
This function is the same as the modulus operator. But fmod() performs

2.switch and cCASE

3. Bit field type must be signed int or unsigned int.


The char type: char scheme:4; is also a valid statement.
float category:5; NOT ALLOWED
4. printf("%f\n",f<<2);if f is float is not aloowed

Option A: assignment statements are always return in paranthesis in the case of conditional operator.
It should be a>b? (c=30):(c=40);
4.

Which of the following is the correct order if calling functions in the below code?
a = f1(23, 14) * f2(12/4) + f3();

2.
What will be the output of the program in 16 bit platform (Turbo C under DOS)?

#include<stdio.h>
int main()
{
extern int i;
i = 20;

printf("%d\n", sizeof(i));
return 0;
}
Linker Error : Undefined symbol 'i'
The statement 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
void modify(struct emp*);//ERROR SICE EMP NOT DEFINED TILL NOWAND WE R MAKING USE OF

struct emp
{
char name[20];
int age;
float sal;
};
struct emp e = {"Tiger",45};

note: learn al the string function utility.


CASE: struct emp e2 = e1;
if(e1 == e2)
printf("The structure are equal");
return 0;
}
OUTPUT Error: Structure cannot be compared using '=='

CASE 2:
struct emp e;
e.name = "Suresh";
e.age = 25;
We cannot assign a string to a struct variable like e.name = "Suresh"; in C.
We have to use strcpy(char *dest, const char *source) function to assign a string.
Ex: strcpy(e.name, "Suresh");

1. strnset()
char *strnset(char *s, int ch, size_t n); Sets the first n characters of s toch

strcmp(const char *s1, const char*s2)


strrchr() returns a pointer to the last occurrence of character in a string.

Declaration: char *strrchr(const char *s, int c);


It scans a string s in the reverse direction, looking for a specific character c.
The function strstr() Finds the first occurrence of a substring in another string
Declaration: char *strstr(const char *s1, const char *s2);
Return Value:
On success, strstr returns a pointer to the element in s1 where s2 begins (points tos2 in s1).
On error (if s2 does not occur in s1), strstr returns null.

* big endian or little endian architecture


* Null macro is defined in stdio.h and stddef.h.It is used to represent a null pointer in your code.
its value is zero. NULL Pointer is a pointer which is pointing to nothing. It points the base address of
segment.
* printf() returns the number of charecters printed on the console.
--The keyword continue cannot be used in switch case
*switch without case is possible .ie only switch no cases
* Step 2: m = ++i && ++j || ++k;//i=-3,j=2;
becomes m = (-2 && 3) || ++k;
becomes m = TRUE || ++k;.
(++k) is not executed because (-2 && 3) alone return TRUE.
Note: printf("%d\n%d\n%d", c, c <<= 2, c >>= 2);
Compiler eror ayega
* printf("%d,%d\n", ++i, ++i);//no precedence of pameter execution is defined in c
Hence,ans is compile dependent.holds true foe all when ever same variable has to be
modified in the argument and their depend on each other

* In 16 bit compiler, the pointer size is always 2 bytes.


In 32 bit compiler, the pointer size is always 4 bytes.
* printf("%e, ", f); Here '%e' specifies the "Scientific Notation" format. So, it prints the 43.20
as 4.320000e+01.

printf("%f, ", f); Here '%f' specifies the "Decimal Floating Point" format. So, it prints the 43.20
as 43.200001.

printf("%g, ", f); Here '%g' "Use the shorter of %e or %f". So, it prints the 43.20 as 43.2.

note:
in post increment:always it has high precedence but if other operator r there it will
show its action a the last
ex. Return (n--)//n=5,,,,,,now it return the value 5 to the function then decrement 5
to 4

4.

What will be the output of the program?

#include<stdio.h>
int reverse(int);
int main()
{
int no=5;
reverse(no);
return 0;
}
int reverse(int no)
{
if(no == 0)
return 0;
else
printf("%d,", no);
reverse (no--);
}
A.

Print 5, 4, 3, 2, 1

B.

C.

Print 5, 4, 3, 2, 1, 0

D.

Print 1, 2, 3, 4, 5
Infinite loop

Answer & Explanation

Answer: Option D
Explanation:
Step 1: int no=5; The variable no is declared as integer type and initialized to 5.
Step 2: reverse(no); becomes reverse(5); It calls the function reverse() with '5' as
parameter.
The function reverse accept an integer number 5 and it returns '0'(zero) if(5 == 0) if the given
number is '0'(zero) or else printf("%d,", no); it prints that number 5 and calls the
function reverse(5);.
The function runs infinetely because the there is a post-decrement operator is used. It will not
decrease the value of 'n' before calling the reverse() function. So, it

callsreverse(5) infinitely.
Note: If we use pre-decrement operator like reverse(--n), then the output will be 5, 4, 3,
2, 1. Because before calling the function, it decrements the value of 'n'.

*very imp:note :while solving functions ques alwyasmake sure that before using the
function their prototype is defined
*6.68 is double.
6.68L is long double constant.
6.68f is float constant.
6.68LF is not allowed in c.

unsigned int i = 65536; /* Assume 2 byte integer*/


while(i != 0)//condition becomes false

.if(0.7 > a) here a is a float variable and 0.7 is a double constant. The double constant 0.7 is
greater than the float variable

*floata=0.7;

printf("%.10f%.10f\n",0.7,a);
0.7000000000 0.6999999881//double is always greater than float

float a=0.7;
printf("%.10f %.7f\n",0.7, a);output 0.70000000000.7000000//allthevaueshave
someprecisionvalurabovewhichtheywillshowthevaluesmallerthanexpected
whetheritisdoubleorfloat.butprecisonvaluroffloatis7approxabovewhichits
valurwilldecreasefromactualvalueexpected
What will be the output of the program if the array begins at 65472 and each integer occupies 2
bytes?

int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0};


printf("%u, %u\n", a+1, &a+1);
65480, 65496
The declaration num[SIZE] is allowed if SIZE is a macro.
: cannot use static for function parameters
Space is a character ,NULL always represent a pointer

What will be the output of the

program if the size of pointer is 4bytes?

In C and in C++ single quotes identify a single character,


while double quotes create a string literal. 'a' is a single a
character literal, while "a" is a string literal containing an #include<stdio.h>
'a' and a null terminator (that is a 2 char array).
int main()
Note that in C, the type of a character literal is int, and
not char, that is sizeof 'a' is 4 in an architecture where ints
are 32bit (and CHAR_BIT is 8), while sizeof(char) is 1
everywhere.
int main()
{
printf("%d, %d\n", sizeof(NULL), sizeof('#'));
return 0;
}
OUTPUT:

4,4
What will be the output of the program?
#include<stdio.h>

int main()
{
int arr[3] = {2, 3, 4};
char *p;
13.

p = arr;
p = (char*)((int*)(p));
printf("%d, ", *p);
p = (int*)(p+1);
printf("%d", *p);
return 0;
}
[A].

2, 3

[B].

2, 0

printf("%d,%d\n",
sizeof(NULL), sizeof(""));
return 0;
}

[C].

2, Garbage value

[D].

0, 0

p = (char*)((int*)(p));
// till now the pointer p is type casted to store the variable
of type character.
printf("%d, ", *p); // %d means integer value so value at
first address i.e. 2 will be printed.
p = (int*)(p+1); // here p is still of type character as type
casted in step 1 so p(i.e address) and plus 1 will increase
only by one byte so

Assuming that integer requires 2 bytes of storage the


integer array will be stored in memory as
value 2 3 4
address 00000010 00000000 00000011 00000000
00000100 00000000
pointer p+1

so p+1 points to that location which is unfilled as during


intialization 2,3,4 were stored in variable of type integer(2
bytes).
so p+1 will point to 00000000.
(int*)p+1 // p+1 is type casted again to integer
printf("%d", *p); // this will print 0 as output as by default
integer contains 0 as value.
Note: The number-sign or "stringizing" operator (#)

converts macro parameters to string literals without


expanding the parameter definition. It is used only with
macros that take arguments
## (macro concatenation) operator//#defineFUN(i,j)
i##j//FUN(First,Second)// FirstSecond.
1.

printf("Computer","Programming"); Computer

You might feel that answer of this statement should


be Programming because comma operator always
returns rightmost operator, in case of printf
statement once comma is read then it will consider
preceding things as variable or values for format
specifier.

2. #define MESS junk


printf("MESS\n");// is no macro calling inside the printf
statement occured.
MESS

B.

Note: #if checks for the value of the symbol, while #ifdef checks the existence of the
symbol (regardless of its value). Each #if directive in a source file must be matched by a
closing #endif directive. Any number of #elif directives can appear between
the #if and #endif directives, but at most one #else directive is allowed.
The #else directive, if present, must be the last directive before #endif. The conditional
macro #if must have an #endif. In this program there is no #endifstatement written.
The #ifdef identifier statement is equivalent to #if 1 when identifier has been defined, and
it is equivalent to #if 0 when identifier has not been defined or has been undefined with
the #undef directive. These directives check only for the presence or absence of identifiers
defined with #define, not for identifiers declared in the C or C++ source code.
Note: Macro names should only consist of alphanumeric characters and underscores, i.e. 'az', 'A-Z', '0-9', and '_', and the first character should not be a digit. Some preprocessors also

permit the dollar sign character '$', but you shouldn't use it; unfortunately I can't quote the C
standard since I don't have a copy of it.
Fill with 1s in the left side for right shift for negative numbers.

1.
2.

unsigned int a=0xffff;//REPRESENT HEXADECIMAL MAI ffff hai

NOTE:CMD NOT DONE DO IT //PAGE 58

Note: cannot initialize more than one union member.


1. itoa() converts an integer to a string.//

itoa(num1,str1,10);
itoa (no,buff,2);// itoa() takes the integer input value input and converts it to a number in base
radix. The resulting number a sequence of base-radix digits.

/*10radixvalue*/
2. ltoa() converts a long to a string.
3. ultoa() converts an unsigned long to a string.
4. sprintf() sends formatted output to a string, so it can be used to convert any type of values to
string type

fflush(FilePointer);
fflush(NULL); flushes all streams.
printf("%f,%f", floor(i), ceil(i));//IT WORKS WELL WITH FLOAT IDENTIFIER
Both ceil() and floor() return the integer found as a double. double to int it returns '0'.
So, the output is '2.000000, 0'.
float i = 2.3;
printf("%d\n", floor(i));
printf("%f", ceil(i));
return 0;
}
OUTPUT:

0
3.000000

Directly ceil and floor with int format specifier will result in zero.
memcmp(dest, src, 2)// Explanation:

memcmp compares the first 2 bytes of the blocks dest and src as unsigned chars. When comparing
the array dest and srcas unsigned chars, the first 2 bytes are same in both
variables.so memcmp returns '0'.
void *memccpy(void *dest, const void *src, int c, size_t n); : Copies a block of n
bytes from src to dest

p = (char*) memccpy(str2, str1, 'i', strlen(str1));// With memccpy(), the copying stops as
soon as either of the following occurs:
=> the character 'i' is first copied into str2
=> n bytes have been copied into str2

printf("%.#s%2s", str, str);// It prints out a minimum of 2 characters of the character string,
str. I don't see an immediate way of limiting the number of characters you want printed if that's what
your looking fo

You can terminate the string yourself by sticking the '\0' in there yourself:
char s[10];
char foo = "My hovercraft is full of eels."; // more than 10 chars
strncpy(s, foo, 9); // only copy 9 chars into positions 0-8
s[9] = '\0';
// position 9 gets the terminator

scanf() or atoi() function can be used to convert a string like "436" in to integer

. number = atoi("string");
int main()
{
int (*p)[4];
p = (int (*) [4])malloc(3*sizeof(*p));

printf("%d,%d\n", sizeof(p), sizeof(*p));


return 0;
}
Output :2,8 //considerint pointer of size 2 bytes
Note:union one member is being used at a time.
2.
What will be the output of the program?

#include<stdio.h>
#include<stdlib.h>
union employee
{
char name[15];
int age;
float salary;
};
const union employee e1;
int main()
{
strcpy(e1.name, "K");
printf("%s%d%f", e1.name, e1.age, e1.salary);
return 0;
}
OUTPUT:

K750.000000
Note: in case of constant array modification can ony be done if the is a pointer who tries to do.
A const variable can be indirectly modified by a pointer.

const int x;// Error: const variable have been initialised when declared.
x=128;

in short if substitute name is in small latters then just read it as normal statement
wheres for actual definition of typedef to hold it should contain leeter in CAPITAL for
alais name
What will be the output of the program ?

#include<stdio.h>
int main()
{
struct value
{
int bit1:1;
int bit3:4;
int bit4:4;//IF BIT FIELD IS LARGE EX.40 IT WIL DISPLAY ERROR
}bit={1, 2, 13};
printf("%d,%d,%d\n", bit.bit1, bit.bit3, bit.bit4);
return 0;
}

A.

1, 2, 13

B.

1, 4, 4

C.

-1, 2, -3

D.

-1, -2, -13

Answer & Explanation

Answer: Option C
Explanation:
Note the below statement inside the struct:

int bit1:1; --> 'int' indicates that it is a SIGNED integer.


For signed integers the leftmost bit will be taken for +/- sign.
If you store 1 in 1-bit field:
The left most bit is 1, so the system will treat the value as negative number.
The 2's complement method is used by the system to handle the negative values.
Therefore, the data stored is 1. The 2's complement of 1 is also 1 (negative).
Therefore -1 is printed.

If you store 2 in 4-bits field:


Binary 2: 0010 (left most bit is 0, so system will treat it as positive value)
0010 is 2
Therefore 2 is printed.

If you store 13 in 4-bits field:


Binary 13: 1101 (left most bit is 1, so system will treat it as negative value)
Find 2's complement of 1101:
1's complement of 1101 : 0010
2's complement of 1101 : 0011 (Add 1 to the result of 1's complement)
0011 is 3 (but negative value)
Therefore -3 is printed.

1.

Bit fields be used in union ALSO.

2.

ALLcombination of nesting is possible between strut and union

ENUM PROPERTIES: enum assigns to it's variable component sequential integer values.
Enum values are constant.r++ not alloowed
Note:

char arr[]="\0";
if(printf("%s",arr))
printf("not empty");
else
printf("empty")
char arr[]="\0";

Here, the char array arr has two elements, both of them are \0. When you use:
printf("%s",arr)

%s in format specifier tells printf to look for a string. And printf found it, but it stops printing

after seeing the first \0, which is the first character


Note:
i++[s] is equivalent to s[i]; i=i+1;
++i[s] is equivalent to ++(s[i]);

18.
What will be the output of the program assuming that the array begins at location 1002?

#include<stdio.h>
int main()
{
int a[2][3][4] = { {1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 1, 2},
{2, 1, 4, 7, 6, 7, 8, 9, 0, 0, 0, 0} };
printf("%u,%u,%u,%d\n", a, *a, **a, ***a);
return 0;
}
[A].

1002, 2004, 4008, 2

[B].

2004, 4008, 8016, 1

[C].

1002, 1002, 1002, 1

[D].

Error

a' holds the starting address of 3d array here. So it will be 1002,


In a[][][], the first bracket represents the z-direction view. Second bracket represents rows(X-

direction). 3rd brackets represents columns (Y-direction).


*a is nothing but a[][] is the starting address of 2d array in xy-direction.
**a is nothing but a[] is the starting address of 1d array in y-direction.
***a is value at stating of 3d array a
So answers will be: *a=1002, **a=1002, ***a=1.
4.

Will the program compile in Turbo C?

#include<stdio.h>
int main()
{
int a=10, *j;
void *k;
j=k=&a;
j++;
k++;
printf("%u%u\n", j, k);
return 0;
}
A.

Yes

B.

No

Answer & Explanation

Answer: Option B
Explanation:
Error in statement k++. We cannot perform arithmetic on void pointers.
Note:

There is compiler error in line "printf("%d", *ptr);". void * type pointers cannot be de-referenced. We
must type cast them before de-referencing. The following program works fine and prints 12.

Note: In C, when an intger value is compared with an unsigned it, the int is promoted to unsigned.

Negative numbers are stored in 2's complement form and unsigned value of the 2's complement form is
much higher than the sizeof int.

free(p);///in thisorder it must be used otherwise memory leakage happens


p = NULL;

Consider the following program, where are i, j and k are stored in memory?

int i;
int main()
{
int j;
int *k = (int *) malloc (sizeof(int));
}

NOTE:
i is global variable and it is uninitialized so it is stored on BSS part of Data Segment
(http://en.wikipedia.org/wiki/.bss) j is local in main() so it is stored in stack frame
(http://en.wikipedia.org/wiki/Call_stack) k is dynamically allocated so it is stored on Heap Segment. See
int a[][3] = {1, 2, 3, 4, 5, 6};
int (*ptr)[3] = a;
printf("%d %d ", (*ptr)[1], (*ptr)[2]);

output : 2 3

note: int x = 10;

static int y = x;
In, C static variables can only be initialized using constant literals.

static int i = initializer();//false way


#include <stdio.h>
int a, b, c = 0;
void prtFun (void);
int main ()
{
static int a = 1; /* line 1 */
prtFun();
a += 1;
prtFun();
printf ( "\n %d %d " , a, b) ;
}
void prtFun (void)
{
static int a = 2; /* line 2 */
int b = 1;
a += ++b;
printf (" \n %d %d " , a, b);
}
Output
42
62
20
Note:

Static variable will exist till the program exit ,but its scope will be within function wher e it is defined
Points:
On extern

1. Declaration can be done any number of times but definition only once.
2. extern keyword is used to extend the visibility of variables/functions().
3. Since functions are visible through out the program by default. The use of extern is not needed
in function declaration/definition. Its use is redundant.
4. When extern is used with a variable, its only declared not defined.
5. As an exception, when an extern variable is declared with initialization, it is taken as definition
of the variable as well.
2.
3.

http://www.geeksforgeeks.org/understanding-extern-keyword-in-c/
http://www.geeksforgeeks.org/understanding-register-keyword/

4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.

#include <stdio.h>
char *fun()
{
static char arr[1024];
return arr;
}

int main()
{
char *str = "geeksforgeeks";
strcpy(fun(), str);
str = fun();
15.
strcpy(str, "geeksquiz");
16.
printf("%s", fun());
17.
return 0;
}
Question 19 Explanation:
Note that arr[] is static in fun() so no problems of returning address, arr[] will stay there even after the fun()
returns and all calls to fun() share the same arr[].
strcpy(fun(), str); // Copies "geeksforgeeks" to arr[]
str = fun();

// Assigns address of arr to str

strcpy(str, "geeksquiz"); // copies geeksquiz to str which is address of arr[]


printf("%s", fun()); // prints "geeksquiz"

Predict the output


#include <string.h>
#include <stdio.h>
#include <stdlib.h>

void fun(char** str_ref)


{
str_ref++;
}
int main()
{
char *str = (void *)malloc(100*sizeof(char));
strcpy(str, "GeeksQuiz");
fun(&str);
puts(str);
free(str);
return 0;
}
Output
GeeksQuiz

Question 5 Explanation:
Note that str_ref is a local variable to fun(). When we do str_ref++, it only changes the local variable
str_ref. We can change str pointer using dereference operator *. For example, the following program prints
"eeksQuiz"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

void fun(char** str_ref)


{
(*str_ref)++;
}

int main()
{
char *str = (void *)malloc(100*sizeof(char));
strcpy(str, "GeeksQuiz");
fun(&str);
puts(str);
free(str);
return 0;
}

Output of following program


#include <stdio.h>
int fun(int arr[]) {
arr = arr+1;
printf("%d ", arr[0]);
}
int main(void) {
int arr[2] = {10, 20};
fun(arr);
printf("%d", arr[0]);
return 0;
}
Output
20 10

Question 9 Explanation:
In C, array parameters are treated as pointers (See http://www.geeksforgeeks.org/why-c-treats-arrayparameters-as-pointers/ for details). So the variable arr represents an array in main(), but a pointer in
fun().
What is the output of the following C code? Assume that the address of x is 2000 (in decimal) and an
integer requires four bytes of memory.
#include <stdio.h>
int main()
{
unsigned int x[4][3] = {{1, 2, 3}, {4, 5, 6},
{7, 8, 9}, {10, 11, 12}};
printf("%u, %u, %u", x+3, *(x+3), *(x+2)+3);
}

x= 2000

Since x is considered as a pointer to an


array of 3 integers and an integer takes 4
bytes, value of x + 3 = 2000 + 3*3*4 = 2036

The expression, *(x + 3) also prints same


address as x is 2D array.

The expression *(x + 2) + 3 = 2000 + 2*3*4 + 3*4


= 2036

int * g (void)

int x = 10;
return (&x);
}
pointer variable x is a local variable to g(), and g() returns pointer to this variable. x may vanish after
g() has returned as x exists on stack. So, &x may become invalid

1. Consider the following C program segment:


char p[20];
char *s = "string";
int length = strlen(s);
int i;
for (i = 0; i < length; i++)
p[i] = s[length i];
printf("%s",p);
Run on IDE
The output of the program is (GATE CS 2004)
a) gnirts
b) gnirt
c) string
d) no output is printed
Answer(d)
Let us consider below line inside the for loop
p[i] = s[length i];
For i = 0, p[i] will be s[6 0] and s[6] is \0
So p[0] becomes \0. It doesnt matter what comes in p[1], p[2].. as P[0] will not change for i >0. Nothing
is printed if we print a string with first character \0

5. Consider the following C program


main()
{
int x, y, m, n;
scanf ("%d %d", &x, &y);
/* x > 0 and y > 0 */
m = x; n = y;

while (m != n)
{
if(m>n)
m = m - n;
else
n = n - m;
}
printf("%d", n);
}
Run on IDE
The program computes (GATE CS 2004)
a) x + y using repeated subtraction
b) x mod y using repeated subtraction
c) the greatest common divisor of x and y
d) the least common multiple of x and y
Answer(c)
This is an implementation of Euclids

algorithm to find GCD

2. Consider the C program shown below.


# include <stdio.h>
# define print(x) printf ("%d", x)
int x;
void Q(int z)
{
z += x;
print(z);
}
void P(int *y)
{
int x = *y+2;
Q(x);
*y = x-1;
print(x);
}
main(void)

x=5;
P(&x);
print(x);
getchar();

}
Run on IDE
The output of this program is (GATE CS 2003)
a) 1276
b) 22 12 11
c) 14 6 6
d) 766
Answer (a)
Note that main() and Q() are accessing the global variable x. Inside P(), pointer variable y also holds
address of global variable x, but x in P() is its Ps own local variable.

int f1(int n)
{
if(n == 0 || n == 1)
return n;
else
return (2*f1(n-1) + 3*f1(n-2));
}
f1(8) a
(A) 1661 and

(B) 59 and
(C) 1640 an
(D)
Both functions perform same operation, so output is same, means either (B) or (C) is correct.
f1(2) = 2*f1(1) + 3*f1(0) = 2
f1(3) = 2*f1(2) + 3*f1(1) = 2*2 + 3*1 = 7
f1(4) = 2*f1(3) + 3*f1(2) = 2*7 + 3*2 = 20
f1(5) = 2*f1(4) + 3*f1(3) = 2*20 + 3*7 = 40 + 21 = 61
We can skip after this as the only remaining choice is (C)
f1(6) = 2*f1(5) + 3*f1(4) = 2*61 + 3*20 = 122 + 60 = 182
f1(7) = 2*f1(6) + 3*f1(5) = 2*182 + 3*61 = 364 + 183 = 547
f1(8) = 2*f1(7) + 3*f1(6) = 2*547 + 3*182 = 1094 + 546 = 1640

Following is the declaration for putchar() function.


int putchar(int char)

char -- This is the character to be written. This is passed as its int promotion.

Following is the declaration for getchar() function.

int getchar(void)

Return Value
This function returns the character written as an unsigned char cast to an int or EOF on error.
2. What is the value printed by the following C program?
#include<stdio.h>
int f(int *a, int n)
{
if(n <= 0) return 0;
else if(*a % 2 == 0) return *a + f(a+1, n-1);
else return *a - f(a+1, n-1);
}
int main()
{
int a[] = {12, 7, 13, 4, 11, 6};
printf("%d", f(a, 6));
getchar();
return 0;
}
Run on IDE
(A) -9
(B) 5
(C) 15
(D) 19
Answer (C)
f() is a recursive function which adds f(a+1, n-1) to *a if *a is even. If *a is odd then f() subtracts f(a+1, n-1)
from *a. See below recursion tree for execution of f(a, 6).
.

f(add(12), 6) /*Since 12 is first element. a contains address of 12 */


|
|
12 + f(add(7), 5) /* Since 7 is the next element, a+1 contains address of 7 */
|
|
7 - f(add(13), 4)
|
|
13 - f(add(4), 3)
|
|
4 + f(add(11), 2)
|
|
11 - f(add(6), 1)
|
|
6+0

So, the final returned value is 12 + (7 (13 (4 + (11 (6 + 0))))) = 15


char *getString()
{
char *str = "Nice test for strings";
return str;
}
int main()
{
printf("%s", getString());
getchar();
return 0;
}
Run on IDE
Output: Nice test for strings
The above program works because string constants are stored in Data Section (not in Stack Section). So,
when getString returns *str is not lost.
char *getString()
{
char str[] = "Will I be printed?";
return str;

}
int main()
{
printf("%s", getString());
getchar();
}
Run on IDE
Output: Some garbage value
The above program doesnt work because array variables are stored in Stack Section. So, when getString
returns values at str are deleted and str becomes dangling pointer

Das könnte Ihnen auch gefallen