Answered by:
C/C++ puzzles and concepts

Question
-
this is a thread to post and get the correct answer for questions in c/cpp,
everyone is invited to post puzzles here, but it will be his resposibility to post the answer as well here in 24 hours.
correct post will definitely be marked as "ANSWER"
so lets begin.
Thursday, March 22, 2007 1:43 PM
Answers
-
QUESTION:7
QUESTION FOR ALL MEMBERS
what is output of following program with reason.
part a
#define square(x) x*x
main()
{
int i;
i = 64/square(4);
printf("%d",i);
}part b
void main()
{
char far *farther,*farthest;
printf("%d..%d",sizeof(farther),sizeof(farthest));
}Monday, March 26, 2007 4:26 PM -
how many storage classes r there in c and what is the default storage class??????Thursday, March 22, 2007 1:44 PM
-
post has been changed for benefit for allFriday, March 23, 2007 8:37 AM
-
QUESTION 3
what is o/p of the following program:
do give explanation
void main()
{
int i=32767;
printf("%d",++i);
}
Friday, March 23, 2007 1:48 PM -
ANSWER TO QUESTION 3
soory varun, but the answer is not correct as the integer range in c is from -32768 to 32767 the variable iafter increment will go to -32768, this number system concept is circular in c/c++
just try this on the compiler
Saturday, March 24, 2007 6:19 AM -
KANAV_AGGARWAL_502530 wrote: ANSWER TO QUESTION 3
soory varun, but the answer is not correct as the integer range in c is from -32768 to 32767 the variable iafter increment will go to -32768, this number system concept is circular in c/c++
just try this on the compiler
ahh nice one man.. i totally forgot about the range.. i thought the main point in the prob was of increment operator.. nice one...Saturday, March 24, 2007 11:18 AM -
QUESTION NUMBER 4,
EVERYONE IS WELCOME TO PARTICIPATE
what is the output of following program:
main()
{
float me = 1.1;
double you = 1.1;
if(me==you)
printf("I love U");
else
printf("I hate U");
}
Saturday, March 24, 2007 1:02 PM -
The output will be "I hate U"
the reason is because the internal representation of float.... when you save 1.1, in binary equivalent the number is saved as 1.099999
1 bit data is lost.. thatz not the case in double.. so this 2 are not same...
now instead of if(me= = you)
you can even try if(me = = 1.1)
the output will be the same..
but not for if(you = = 1.1)
if the explanation is not satisfactory than please correct me...Saturday, March 24, 2007 2:54 PM -
QUESTION:5
what is the output of following program:
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);
}
Saturday, March 24, 2007 4:38 PM -
Ans : - 0 0 1 3 1
Everyone will be knowing about the increment operator so i wont be explaining that... now the question is about the value of m
As we have used the logical operator.. the value of m would be a boolean one only, i.e. 1 or 0
Now for computer a 0 value is false and a non zero value is true.. so as L is 3, the condition becomes true, and so 1 value is assigned to m....Saturday, March 24, 2007 5:46 PM -
Ans for that is 00131.Sunday, March 25, 2007 4:54 AM
-
QUESTION::6
what is the output of following program and do give explanation for the answer to this question
main()
{
int i=5;
printf("%d%d%d%d%d%d", i++ ,i-- ,++i ,--i ,i );
}
Sunday, March 25, 2007 6:24 AM -
answer given by sanket and varun is correct hence the next question i.e. question 6 is being posted here-aboveSunday, March 25, 2007 6:27 AM
-
output :- 4 5 5 4 5
Now as we know that the associativity of increment or decrement operator is from right to left...
This is the reason the increment operation above gets executed as from right to left.... now the right most part i is sent as it is, than i gets decremented and than goes, and so on...
but printing is from left to right only.. so they get printed in the same order, its just the value they hold gets assigned this way, i.e. right to left...Sunday, March 25, 2007 11:23 AM -
out put is 20.. as it is the macro you have defined.. the code will become,
i = 64/4^4 = 16^4
now ^ is an Xor Operator...
so 16^4 - 20
i = 20....Monday, March 26, 2007 6:47 PM -
Part (a)
----------Answer:
64
Explanation:
The macro call square(4) will be substituted by 4*4 so the expression becomes i = 64/4*4 . Since / and * have equal priority the expression will be evaluated as (64/4)*4 i.e. 16*4 = 64Part (b)
----------Answer:
4..2
Explanation:
The second pointer is of char type and not a far pointer.Monday, March 26, 2007 8:17 PM -
dear varun, ANSWER TO QUESTION 7for both the parts has been given correctly by sanket
with explanation i think i should not repeat this and if u want me to fo so then post a message,
well i am posting next question below.
Tuesday, March 27, 2007 5:12 PM -
QUESTION ::8
explain & give o/p of the program
#define f(g,g2) g##g2
main()
{
int var12=100;
printf("%d",f(var,12));
}
Tuesday, March 27, 2007 5:30 PM -
## is a token pasting operator. When f(var, 12) is executed, the macro converts into var##12, ultimately leading to var12.
Thus, the printf() statement will now become
printf("%d", var12);
and that will print 100 on screen.Tuesday, March 27, 2007 5:38 PM -
QUESTION:9
main( )
{
int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}};
printf(“%u %u %u %d \n”,a,*a,**a,***a);
printf(“%u %u %u %d \n”,a+1,*a+1,**a+1,***a+1);
}
Friday, March 30, 2007 7:20 PM -
The output will be :
<<address of a>> <<address of a>> <<address of a>> 2
<<address of 2nd row of a ((width+depth) * sizeof(int))>> <<address of 2nd depth of a (depth * sizeof(int))>> <<address of next element of a>> 3
I'm pasting output from my screen below :
65502 65502 65502 2
65514 65506 65504 3Sunday, April 1, 2007 4:56 PM
All replies
-
how many storage classes r there in c and what is the default storage class??????Thursday, March 22, 2007 1:44 PM
-
reply has been changed.Thursday, March 22, 2007 6:03 PM
-
post has been changed for benefit for allFriday, March 23, 2007 8:37 AM
-
QUESTION 3
what is o/p of the following program:
do give explanation
void main()
{
int i=32767;
printf("%d",++i);
}
Friday, March 23, 2007 1:48 PM -
Friday, March 23, 2007 3:53 PM
-
Friday, March 23, 2007 5:40 PM
-
What the hell is problem with you two??? we are all here to share and learn, don start this all things here... try to resolve your problems or else both of you will loose support from everyone else here... pls stop all these..
As for the answer, it is 32768, because it is a prefix increment operator which will increment the value before passing it to the calling function printf();Saturday, March 24, 2007 4:19 AM -
ANSWER TO QUESTION 3
soory varun, but the answer is not correct as the integer range in c is from -32768 to 32767 the variable iafter increment will go to -32768, this number system concept is circular in c/c++
just try this on the compiler
Saturday, March 24, 2007 6:19 AM -
KANAV_AGGARWAL_502530 wrote: ANSWER TO QUESTION 3
soory varun, but the answer is not correct as the integer range in c is from -32768 to 32767 the variable iafter increment will go to -32768, this number system concept is circular in c/c++
just try this on the compiler
ahh nice one man.. i totally forgot about the range.. i thought the main point in the prob was of increment operator.. nice one...Saturday, March 24, 2007 11:18 AM -
QUESTION NUMBER 4,
EVERYONE IS WELCOME TO PARTICIPATE
what is the output of following program:
main()
{
float me = 1.1;
double you = 1.1;
if(me==you)
printf("I love U");
else
printf("I hate U");
}
Saturday, March 24, 2007 1:02 PM -
The output will be "I hate U"
the reason is because the internal representation of float.... when you save 1.1, in binary equivalent the number is saved as 1.099999
1 bit data is lost.. thatz not the case in double.. so this 2 are not same...
now instead of if(me= = you)
you can even try if(me = = 1.1)
the output will be the same..
but not for if(you = = 1.1)
if the explanation is not satisfactory than please correct me...Saturday, March 24, 2007 2:54 PM -
there is no button for deleting the posts. I've replied to yours in my thread. Consider my replies as deleted.Saturday, March 24, 2007 3:16 PM
-
QUESTION:5
what is the output of following program:
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);
}
Saturday, March 24, 2007 4:38 PM -
Ans : - 0 0 1 3 1
Everyone will be knowing about the increment operator so i wont be explaining that... now the question is about the value of m
As we have used the logical operator.. the value of m would be a boolean one only, i.e. 1 or 0
Now for computer a 0 value is false and a non zero value is true.. so as L is 3, the condition becomes true, and so 1 value is assigned to m....Saturday, March 24, 2007 5:46 PM -
Ans for that is 00131.Sunday, March 25, 2007 4:54 AM
-
QUESTION::6
what is the output of following program and do give explanation for the answer to this question
main()
{
int i=5;
printf("%d%d%d%d%d%d", i++ ,i-- ,++i ,--i ,i );
}
Sunday, March 25, 2007 6:24 AM -
answer given by sanket and varun is correct hence the next question i.e. question 6 is being posted here-aboveSunday, March 25, 2007 6:27 AM
-
output :- 4 5 5 4 5
Now as we know that the associativity of increment or decrement operator is from right to left...
This is the reason the increment operation above gets executed as from right to left.... now the right most part i is sent as it is, than i gets decremented and than goes, and so on...
but printing is from left to right only.. so they get printed in the same order, its just the value they hold gets assigned this way, i.e. right to left...Sunday, March 25, 2007 11:23 AM -
The answer is same as that of varun :
4 5 5 4 5Sunday, March 25, 2007 5:29 PM -
QUESTION:7
QUESTION FOR ALL MEMBERS
what is output of following program with reason.
part a
#define square(x) x*x
main()
{
int i;
i = 64/square(4);
printf("%d",i);
}part b
void main()
{
char far *farther,*farthest;
printf("%d..%d",sizeof(farther),sizeof(farthest));
}Monday, March 26, 2007 4:26 PM -
out put is 20.. as it is the macro you have defined.. the code will become,
i = 64/4^4 = 16^4
now ^ is an Xor Operator...
so 16^4 - 20
i = 20....Monday, March 26, 2007 6:47 PM -
Kanav your 2nd part is not clear.. is it a pointer or what?? please check it out...Monday, March 26, 2007 6:55 PM
-
Part (a)
----------Answer:
64
Explanation:
The macro call square(4) will be substituted by 4*4 so the expression becomes i = 64/4*4 . Since / and * have equal priority the expression will be evaluated as (64/4)*4 i.e. 16*4 = 64Part (b)
----------Answer:
4..2
Explanation:
The second pointer is of char type and not a far pointer.Monday, March 26, 2007 8:17 PM -
dear varun, ANSWER TO QUESTION 7for both the parts has been given correctly by sanket
with explanation i think i should not repeat this and if u want me to fo so then post a message,
well i am posting next question below.
Tuesday, March 27, 2007 5:12 PM -
Where is the next question kanav ? I can't see any.Tuesday, March 27, 2007 5:20 PM
-
QUESTION ::8
explain & give o/p of the program
#define f(g,g2) g##g2
main()
{
int var12=100;
printf("%d",f(var,12));
}
Tuesday, March 27, 2007 5:30 PM -
## is a token pasting operator. When f(var, 12) is executed, the macro converts into var##12, ultimately leading to var12.
Thus, the printf() statement will now become
printf("%d", var12);
and that will print 100 on screen.Tuesday, March 27, 2007 5:38 PM -
QUESTION:9
main( )
{
int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}};
printf(“%u %u %u %d \n”,a,*a,**a,***a);
printf(“%u %u %u %d \n”,a+1,*a+1,**a+1,***a+1);
}
Friday, March 30, 2007 7:20 PM -
hey!!
can u again write the qus. in big size..
it is unreadable....
Saturday, March 31, 2007 7:04 PM -
The output will be :
<<address of a>> <<address of a>> <<address of a>> 2
<<address of 2nd row of a ((width+depth) * sizeof(int))>> <<address of 2nd depth of a (depth * sizeof(int))>> <<address of next element of a>> 3
I'm pasting output from my screen below :
65502 65502 65502 2
65514 65506 65504 3Sunday, April 1, 2007 4:56 PM -
@ sanket, your answer is correct, here is next question:
QUESTION:10
give the output of this program with explanation::
main()
{
char a[4]="HELL";
printf("%s",a);
}
Monday, April 16, 2007 7:37 PM -
in Turbo C++,
it displays "HELL".
in MinGW,
it gives an error that "initializer-string for array of chars is too long". That is because last position of the string must be occupied by NULL and it appends it (NULL) to string. Hence, string is now of 5 characters. it is indeed big for its size.Tuesday, April 17, 2007 1:40 AM -
Well Brother as you know that we use a NULL character or a End line character to end a string....
here you have allocated space for 4 characters and assigned HELL, i.e. 4 characters to it.. leaving no space for the white space character... as a result the white space character does not get attached to a valid allocated space...
now when we try to read the string.. the cmd is simple to print out the characters at the address a untill a white space character is encountered... now the white space character is not present and hence we will get HELL printed with some garbage value at the end....
As for Turbo C, it is not reliable... now turbo C must have attached the white space char at the 5th location which is not a valid location.. this space can be used by any other program any time and hence you can get garbage value anytime latter while running your program...Tuesday, April 17, 2007 3:20 AM -
4 classes. & auto is the default 1 in CFriday, May 25, 2007 11:39 AM
-
-32768Friday, May 25, 2007 11:41 AM
-
00131Friday, May 25, 2007 11:47 AM
-
a)64
b)2Friday, May 25, 2007 11:50 AM -
Sanket_Shah_734609 wrote: ## is a token pasting operator. When f(var, 12) is executed, the macro converts into var##12, ultimately leading to var12.
Thus, the printf() statement will now become
printf("%d", var12);
and that will print 100 on screen.
but what if i modify the macros to:
#define a 5
#define b #aFriday, May 25, 2007 11:53 AM -
Varun_Modi_a59ed9 wrote: Well Brother as you know that we use a NULL character or a End line character to end a string....
here you have allocated space for 4 characters and assigned HELL, i.e. 4 characters to it.. leaving no space for the white space character... as a result the white space character does not get attached to a valid allocated space...
now when we try to read the string.. the cmd is simple to print out the characters at the address a untill a white space character is encountered... now the white space character is not present and hence we will get HELL printed with some garbage value at the end....
1) string is not ended with a white space character
2) this thing happens in the gcc compiler & not on turbo.
3) space is never provided for NULL character in the string.
Varun_Modi_a59ed9 wrote:
As for Turbo C, it is not reliable... now turbo C must have attached the white space char at the 5th location which is not a valid location.. this space can be used by any other program any time and hence you can get garbage value anytime latter while running your program...
--> what if i modify it to;
char str[4]="helli";
puts(str);
what do you think the output will be?Friday, May 25, 2007 12:01 PM -
QUESTION 11.
below is a small code snippet.
void main()
{
if(<condition>)
printf("say hello ");
else
printf("to me");
}
part a)
i want the OUTPUT to be::
"say hello to me"
suggest the possible answers for such output with or without altering the condition.
part b)
how can i execute both if & else statements.Friday, May 25, 2007 12:08 PM -
Well Sunil, try to get the facts clear.. A string is ended by a Null character, and it ends with a whitespace if you use cout in c++ or printf()...
Now trubo is full of bugs.. that is the reason you are not able to get them clear... If you get a C++ book there is specifically mention that in C++ you need to allocate a size for the end of string, or else the compiler will give error.. this happens in all Bloodshed, VC, etc except for Turbo C,
And buddy the space is allocated for a null character, it is denoted by '/0' explicitly....
now for your question....
This will give an error
char str[4]="helli";
in c++, as the string length is bigger than the allocated size.. so it cannot intialize it this way...
in C and in Turbo, I think this will be allowed...Friday, May 25, 2007 2:06 PM -
buddy, any1 can be wrong.
but here either i have wrong basics or you.
buddy the space is allocated for a null character, it is denoted by '/0' explicitly....
space for "\0" is never explicitly mentioned.
Now trubo is full of bugs.. that is the reason you are not able to get them clear... If you get a C++ book there is specifically mention that in C++ you need to allocate a size for the end of string, or else the compiler will give error.. this happens in all Bloodshed, VC, etc except for Turbo C,
talking about the books.
all books are mainly on TURBO C/C++ compiler.
and if you will run that snippet on gcc compiler which is also used in QUIZ, you will notice the very certain output.Saturday, May 26, 2007 1:16 PM -
1. It depends upon two things:
- Compiler (e.g. TURBO or VC++ etc)
- Target Operating System - 16-bit or 32 bit for which your compiler generates code.
Still remember the 16-bin / 32-bin data bus concept ?
Please run your code on both the compilers. You will see different results.
Also, please do a printf ("%d, sizeof (i)); on both the compilers.
Your answer lies in there.
Also, please do the same using...
- unsigned int (the last bit is not used as signed bit, so the limit it become 2 raised to n+1 instead of 2 raised to n).
- short int or just short
- int
- long
- double
- unsigned char
- char
Happy Coding!!Thursday, June 26, 2008 8:10 AM -
Varun_Modi_62ad0b wrote: output :- 4 5 5 4 5
Now as we know that the associativity of increment or decrement operator is from right to left...
This is the reason the increment operation above gets executed as from right to left.... now the right most part i is sent as it is, than i gets decremented and than goes, and so on...
but printing is from left to right only.. so they get printed in the same order, its just the value they hold gets assigned this way, i.e. right to left...
i am afraid the actual reason is different from this. the right to left execution is not because of associativity of increment/decrement operator but because of parameter passing. the parameters to a function are passed *usually* from right to left since they get stored on the function's stack. hence, i gets passed first followed by --i and then ++i then i-- and then i++ and finally "%d%d%d%d%d%d"
this order of parameter passing is compiler dependent and is not specified in the ANSI C standards just like the bounds of int in another question where the answer is correct only on a 16 bit compiler like turbo c but not on 32 bit compilers like gcc or vc++.
hence the last parameter is passed first but in its last position and so on. this is the reason why the execution happens in that order but the parameters stay where they are and hence the final answer given by Varun is correct.
thank youSaturday, June 28, 2008 12:50 AM -
altering the condition it can be made to
if(!printf("say hello "))
where printf will return 10 and !10 will be 0 and execution will go to else part and "to me" will also be printed
without altering the condition.. well.. 2 extra ifs are req as follows (cant think of anything better)
if(!<condition>) printf("say hello "); //first addition
if(<condition>) printf("say hello ");
else printf("to me");
if(<condition>) printf("to me"); //second addition
cant think of a way to execute both if and else. (hope there is a proper ans! and eager to know that too)Saturday, June 28, 2008 1:16 AM -
4 Different storage Classes.
1) auto
2) static
3) Register,
4) extern.Friday, August 8, 2008 6:36 AM -
in C ,4 storage classes are ther.Auto,Static,Register and Extern.Auto is the default Storage class.Tuesday, October 21, 2008 9:29 AM
-
The Output will be " I hate U ", because The precision of the floating point numbers (float, double, long double) varies with their storage capacity.Saturday, September 12, 2009 11:13 PM
-
it give some known value or sometime it does not show anything ................here in this program the size(range) used of int 32768 but actually int range is in positive 32767 so it does not show output
- Proposed as answer by khushdeep Sunday, November 1, 2009 6:47 AM
Sunday, November 1, 2009 6:47 AM -
the output is I LOVE UIF I M NOT right then correct meSunday, November 1, 2009 6:49 AM
-
I Love You
- Proposed as answer by Anushiva Friday, September 17, 2010 7:02 AM
Friday, September 17, 2010 7:02 AM -
5 6 6 5 5Friday, September 17, 2010 7:05 AM
-
32768 bcoz by default int unsigned hota h to uski range hogi 0-65535
- Proposed as answer by Anushiva Saturday, September 18, 2010 4:48 AM
Saturday, September 18, 2010 4:46 AM -
QUESTION:5
what is the output of following program:
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);
}
0 0 1 3 1- Proposed as answer by Anushiva Saturday, September 18, 2010 5:08 AM
Saturday, September 18, 2010 4:53 AM -
QUESTION::6
what is the output of following program and do give explanation for the answer to this question
main()
{
int i=5;
printf("%d%d%d%d%d%d", i++ ,i-- ,++i ,--i ,i );
}
4 5 5 4 5Saturday, September 18, 2010 4:54 AM -
prb 1:- 64/4*4=(64/4)*4=16*4=64
Saturday, September 18, 2010 5:01 AM -
part a
#define square(x) x*x
main()
{
int i;
i = 64/square(4);
printf("%d",i);
}Answer for Part A is 64
Explanation:
Since square is a macro it will expand in the statement
i = 64/square(4)
like
i = 64/ 4*4
so 64/4 is performed first as per Rule which is equal to 16
and then 16* 4 is performed which is 64.
To avoid such surprising result, it is recomended to use parenthesis which definign macros
#define square(x) = (x*x)
Wednesday, October 6, 2010 10:59 AM -
QUESTION 3
what is o/p of the following program:
do give explanation
void main()
{
int i=32767;
printf("%d",++i);
}
Wednesday, August 29, 2012 4:32 AM -
-32768
Wednesday, August 29, 2012 5:06 PM -
The output will be "32768".
The reason is the when calling ++i, "i" is promoted BEFORE being used by printf(), as opposed to i++, which will promote "i" AFTER the call to printf(), so had you used i++, the output would have been 32767.
Friday, January 4, 2013 6:25 PM