# C questions • ### Question

• Hi friends,

I will post C code here. Try posting your answers.. If no correct answer is got by the end of the day, i will post the answer myself.. I think 5 questions per day will do... If you have any suggestions, please come up with them.. Also you can also post your own questions...

Sunday, September 23, 2007 3:06 AM

• 2500.00000011

This is because as it is a float value 2500 is followed by six zero's.

so 2500.000000 is printed.

Now the no.of digits is 11 in the float value(2500.000000).so 11 is printed.

Sunday, September 23, 2007 9:42 AM
• O/p:    0

this is because you printed with "%d"

if you use "%f" it prints the desired answer 2600.

Sunday, September 23, 2007 9:47 AM
• Mythri_Pericharla_804db1 wrote:
 Where did you declare 'b'???

That is the error Mythri.. I had put "Point out the error if any " in the question...
So the error is "b undeclared"...
Sunday, September 23, 2007 12:04 PM
• Error: 'Lvalue required in function main'

Since, array is a constant pointer. The statement a++ is not valid. Coz 'a' will always store the base address of the array.
Sunday, September 23, 2007 8:54 PM
• Output: 5

This kind of expressions are resolved right to left. Let me derive:-

a=(z>y)?(z>x)?x:z: (x>z)?y:z

=(z>y)?(z>x)?x:z:z  .....[Evaluating (x>z)?y:z, Since (x>z) is false so z is returned]
=(z>y)?x:z...................[Evalutating (z>x)?x:z, Since (z>x) is true x is returned]
=x...............................[Evalutaing (z>y)?x:z, Since (z>y) is true x is returned]

Therefore a gets the value of x i.e. 5

Sunday, September 23, 2007 9:05 PM
• Output:10099
(since no new line is used in printf)

Explanation:

Local variables have a greater priority than global ones. So here the outermost variable 'i' is never used. Static variable has a property that its scope is that of the function but lifetime is that of a program. So in the function print the values decreases from 2 to 1 to 0. In the function main the value decreases from 100 to 99. So 100 and 99 is displayed.
Monday, September 24, 2007 7:14 PM
• Output:10 c

Explanation: Here two methods of using structure members using structure type pointers are shown. The statement x= malloc(sizeof(struct name)), allocates a name type block and returns its base address to x. Now the statement x->i is used to access structure members through pointer. Now (*x) goes to the address stored in x (i.e. the name type structure block). Hence, (*x).c is yet another method of using the structure members.
Monday, September 24, 2007 7:20 PM
• Error:cannot modify a const object in function main

Explanation: k is defined as constant in the global scope. Since, no local k is define the global k is tried to be modified, which is denied since, it is a constant.
Monday, September 24, 2007 7:24 PM
• Output:Value of x 10

Explantion: The statement 'int printf(const char*,...)' is nothing but a function prototype of printf. For additional information the '...' operator in printf is known as ellipsis operator. It can take any number of arguments.
Monday, September 24, 2007 7:32 PM

### All replies

• Here goes the first question

1.          What is the output?

main(){

float x = 2500;

printf (“%d”,printf (“%f ”,x) );}

Sunday, September 23, 2007 3:12 AM
• 2. Point out error if any:

main(){

int z = 10, y;

int x = y = z;

int a = b = z;

printf (“%d %d “,x,y);}

Sunday, September 23, 2007 3:13 AM
• 3. What is the output of the following code....

float sum (int ,int );

int main()

{

int x = 2500,y = 100;

printf (“%d”,sum (x,y) );

}

float sum (int x, int y)

{

int f = x + y;

return (f);

}

Sunday, September 23, 2007 3:14 AM
• 4. What is the output of the following code segment?

main(){

int x = 5, y = 5, z = 6, a;

a=(z > y)?(z > x)?x:z:(x>z)?y:z;

printf (“%d”,a);}

Sunday, September 23, 2007 3:15 AM
• 5.

Code Snippet

main(){

int a = {3,2,0}, i = 0;

for ( ; a[i]; i++)

printf (“%d”,*(a++) );}

Sunday, September 23, 2007 3:16 AM
• Don't just post the answers alone.. Put the logic behind them too...

Sunday, September 23, 2007 3:17 AM
• 2500.00000011

This is because as it is a float value 2500 is followed by six zero's.

so 2500.000000 is printed.

Now the no.of digits is 11 in the float value(2500.000000).so 11 is printed.

Sunday, September 23, 2007 9:42 AM
• Where did you declare 'b'???

Sunday, September 23, 2007 9:44 AM
• O/p:    0

this is because you printed with "%d"

if you use "%f" it prints the desired answer 2600.

Sunday, September 23, 2007 9:47 AM
• Didnt understand fifth question.

Sunday, September 23, 2007 9:50 AM
• Mythri_Pericharla_804db1 wrote:
 Where did you declare 'b'???

That is the error Mythri.. I had put "Point out the error if any " in the question...
So the error is "b undeclared"...
Sunday, September 23, 2007 12:04 PM
• Error: 'Lvalue required in function main'

Since, array is a constant pointer. The statement a++ is not valid. Coz 'a' will always store the base address of the array.
Sunday, September 23, 2007 8:54 PM
• Output: 5

This kind of expressions are resolved right to left. Let me derive:-

a=(z>y)?(z>x)?x:z: (x>z)?y:z

=(z>y)?(z>x)?x:z:z  .....[Evaluating (x>z)?y:z, Since (x>z) is false so z is returned]
=(z>y)?x:z...................[Evalutating (z>x)?x:z, Since (z>x) is true x is returned]
=x...............................[Evalutaing (z>y)?x:z, Since (z>y) is true x is returned]

Therefore a gets the value of x i.e. 5

Sunday, September 23, 2007 9:05 PM
• 6.

Code Snippet

static int i = 50;

void main ()

{

static int i = 100;

while (print (i) )

{

printf (“%d”,i);

i--;

}

}

int print (int x)

{

static int i = 2;

return (i--);

}

What is the output??

Monday, September 24, 2007 4:47 PM
• 7.

Code Snippet

struct name

{

int i;

char c;

};

main()

{

struct name *x;

x= malloc(sizeof(struct name));

x -> i = 10;

(*x).c = ‘c’;

printf (“%d %c”,x->i,x->c);

}

What is the output???

Monday, September 24, 2007 4:47 PM
• 9.

Code Snippet

const int k = 10;

int main (){

for (k = 0; k < 100;k++)

printf (“%d”,k);

}

What is the output of the code?

Monday, September 24, 2007 4:48 PM
• 8.

Code Snippet

int k = 100;

int main()

{

for (k = 0;k < 100;k++);

printf (“%d”,k);

}

What is the output?

Monday, September 24, 2007 4:53 PM
• 10.

Code Snippet

int printf (const char *,...);

int main()

{

char name[] = “Value of x %d”;

int x = 10;

printf (name,x);

}

What is the output???
Monday, September 24, 2007 4:54 PM
• Output:10099
(since no new line is used in printf)

Explanation:

Local variables have a greater priority than global ones. So here the outermost variable 'i' is never used. Static variable has a property that its scope is that of the function but lifetime is that of a program. So in the function print the values decreases from 2 to 1 to 0. In the function main the value decreases from 100 to 99. So 100 and 99 is displayed.
Monday, September 24, 2007 7:14 PM
• Output:10 c

Explanation: Here two methods of using structure members using structure type pointers are shown. The statement x= malloc(sizeof(struct name)), allocates a name type block and returns its base address to x. Now the statement x->i is used to access structure members through pointer. Now (*x) goes to the address stored in x (i.e. the name type structure block). Hence, (*x).c is yet another method of using the structure members.
Monday, September 24, 2007 7:20 PM
• Error:cannot modify a const object in function main

Explanation: k is defined as constant in the global scope. Since, no local k is define the global k is tried to be modified, which is denied since, it is a constant.
Monday, September 24, 2007 7:24 PM
• Output:01234567891011.................................99

Monday, September 24, 2007 7:27 PM
• Output:Value of x 10

Explantion: The statement 'int printf(const char*,...)' is nothing but a function prototype of printf. For additional information the '...' operator in printf is known as ellipsis operator. It can take any number of arguments.
Monday, September 24, 2007 7:32 PM
• can i get the downloads of all the questions so that i can try solving in free times as i do not get lot of time solving in net?

Tuesday, September 25, 2007 5:31 AM
• Abhrajit_Mukherjee_280650 wrote:

The answer is 100 buddy.. Note the semi-colon at the end of for loop statement...

100 alone wil be printed..

Congrats Abhrajit for answering all other questions corectly...

Wednesday, September 26, 2007 4:14 AM
• 11.

Code Snippet

main(){

int a = {10,1};

int x;

x = a/a;

printf (“%d”,a[x]);}

What is the output?
Wednesday, September 26, 2007 4:15 AM
• 12.

Code Snippet

main()

{

unsigned int c = 10,x = 0x0003;

switch (c & x)

{

case 3: printf (“Hello”);

case 2: printf (“Welcome”);

case 1: printf (“To all”);

}

}

Wednesday, September 26, 2007 4:20 AM
• 13.

Code Snippet

main()

{

int i = 5,sum = 0;

do

sum = 1 / i;

while (0 < i--);

printf (“%d”,sum);

}

Wednesday, September 26, 2007 4:20 AM
• 14.

Code Snippet

main()

{

int  i = 100,j  = 20;

i++ = j;

printf (“%d”,i);

}

Wednesday, September 26, 2007 4:21 AM
• 15.

Code Snippet

main()

{

int a = {1,2};

printf (“%d %d”,a,1[a]);

}

Wednesday, September 26, 2007 4:22 AM
• o/p is 1 2 ....

Wednesday, September 26, 2007 11:55 AM
• O/P :

For 11th

The pointer usage is not Right !! It wont compile !!

For 12th

c&x=2 .. therefre WELCOME TO ALL

For 13th

o/p is 1

For 14th

LValue Required ! [Cant assign a Constant To a Constant ........]

For 15th

1 2

Wednesday, September 26, 2007 12:13 PM
• @Raghuram

Ya I overlooked the semi colon and made a silly mistake. Thanks for your congratulations. Today's 5 Mohan has already solved so congrats to him.

But I feel the 13th Answer is wrong:

O/p: Divide error

Explanation: The i value will reach '0' at one stage and in that case sum=1/i will produce the divide error.
Wednesday, September 26, 2007 5:43 PM
• HI BRO...........

REALLY GOOD TECHNICAL STUFF..........

Thursday, September 27, 2007 8:14 PM
• 16

Code Block

main (int argc,char *argv[])

{

if (argc < 1)

printf (“ONE”);

else

printf (“TWO”);

}

What will be the o/p if the program is run without supplying any arguments?

Saturday, September 29, 2007 1:07 AM
• 17.

Code Block

int a = 1,b = 2,c = 3,*p;

p = &c;

a = c/*p;

b = c;

printf (“%d %d”,a,b);

Comment on the code segment…

Saturday, September 29, 2007 1:11 AM
• 18.

Qusetion

Automatic variables are stored in

a) Stack           b) Disk

c) Swap space d) Data area

Saturday, September 29, 2007 1:13 AM
• 19.

Code Block

struct name

{

int i;

char c;

};

main()

{

struct name *x;

x -> i = 10;

(*x).c = ‘c’;

printf (“%d %c”,x->i,x->c);

}

What is the output?? Is there any error in the program??
Saturday, September 29, 2007 1:14 AM