473,387 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Really good problems

Parul Bagadia
188 100+
I came across certain MCQs in some book n some of them are very interestingly unexplainable :-),... at least i couldnt understand them;plz if anyone has explaination for any of them, let me know.
Expand|Select|Wrap|Line Numbers
  1.  
  2. void main()
  3. {
  4.   printf("%c",'%d');
  5. }
  6.  
whatever you may write after %.. i mean only 1 more charachter, only % will be the output.. how come?
Expand|Select|Wrap|Line Numbers
  1.  
  2. void main()
  3. {
  4.   int i=1,,j=0,k=i--&&++j||--i;
  5.   printf("%d%d%d%"i, j,k);
  6. }
  7.  
output for this is 0 1 1
where as when i debugged it, value of i is coming as -1;.
Expand|Select|Wrap|Line Numbers
  1.  
  2. void main()
  3. {
  4.     printf("%d%d%d%",8);
  5. }
  6.  
output for this 8,0 ,garbage.. how come 0 and not garbage at second place?
Expand|Select|Wrap|Line Numbers
  1.  
  2. void main()
  3. {
  4.   int no=3;
  5.   printf("%d%d",scanf("%c",&no),no); 
  6. }
  7.  
we have to enter a value here, and no matter what value we enter, output is 1 and 3. !!
Expand|Select|Wrap|Line Numbers
  1.  
  2. void main()
  3. {
  4.   printf("%d",5/0);
  5. }
  6.  
output to this is very surprisingly 5.. when i checked in help of turbo C, they had given u cant divide any integer by 0, else an error will be generated.
but only a warning had come and answer was 0.
Expand|Select|Wrap|Line Numbers
  1.  
  2. void main()
  3. {
  4. int i=0;
  5. printf("%d",5/i);
  6. }
  7.  
this gave runtime divide error!....
ultimately we are carrying out same thing right?
Expand|Select|Wrap|Line Numbers
  1.  
  2. void main()
  3. {
  4.   printf("%d",010);
  5. }
  6.  
here the output comes as 8!.... so may be its taking some octet base because for 0101 the value came as 65... a bit explainable.. but for 1010 output comes as 1010 itself...
Expand|Select|Wrap|Line Numbers
  1. printf/*("sun*/ computer*/ education");*/
comment is taken only till computer and if i remove * of computer it will be taken till education; but it takes the farthest one if m not wrong.
Whats the logic behind this?
Expand|Select|Wrap|Line Numbers
  1. sizeof(3.14)
comes as 8,..but that's the value for double and not for float...
Expand|Select|Wrap|Line Numbers
  1. sizeof(float)
is 4, then how do we deciede which is float and double?
Expand|Select|Wrap|Line Numbers
  1.  
  2. printf("%d",printf("sun"));
  3.  
output for this is sun and 3..
I got how 3 came, because sun has 3 charachters in it and i had also done lot many changes in sun and accordingly got answers depending on length of word.. but why that happens.. after inner printf has printed the things; does it returns no. of charachters or sth?
Jan 8 '09 #1
7 1446
Banfa
9,065 Expert Mod 8TB
OK so ignoring that all you examples which include main have void main() which is undefined behaviour.

Expand|Select|Wrap|Line Numbers
  1.  
  2.   printf("%c",'%d');
  3.  
The fact you compiler compiles this line without a warning just shows your compiler up as not very good. You can not have a multi-byte character like this, your compiler is creating a single byte character using the first supplied character in your constant and then printing it in the normal way using %c.

Expand|Select|Wrap|Line Numbers
  1.   int i=1,,j=0,k=i--&&++j||--i;
  2.  
The fact that this produces different results under different conditions almost certainly points to undefined behaviour and therefore analysis of it is pointless. Needless to say I would expect never to find something like this in a real life project.

Expand|Select|Wrap|Line Numbers
  1.  
  2.     printf("%d%d%d%",8);
  3.  
output for this 8,0 ,garbage.. how come 0 and not garbage at second place?

The second value is garbage too, it just randomly has the value 0

Expand|Select|Wrap|Line Numbers
  1.  
  2.   printf("%d%d",scanf("%c",&no),no); 
  3.  
You have provide a int * to scanf where you should have had a char *. However the order of function parameter evaluation is not guaranteed so it may have evaluated no before scanf("%c",&no) on your platform.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  printf("%d",5/0);
  3.  
  4. int i=0;
  5. printf("%d",5/i);
  6.  
These both give exceptions on my computer, divide by 0 is undefined behaviour so anything may happen.

Expand|Select|Wrap|Line Numbers
  1.  
  2.   printf("%d",010);
  3.  
The leading 0 means treat the value as octal (base 8) so 010 has a decimal value of 8 where as 10 has a decimal value of 10. Try compiling 090 you should get an error because 9 is not a valid octal digit.


Expand|Select|Wrap|Line Numbers
  1. printf/*("sun*/ computer*/ education");*/
Whats the logic behind this? Who nows just don't nest comment markers.
Expand|Select|Wrap|Line Numbers
  1. sizeof(3.14)
comes as 8,..but that's the value for double and not for float...

Of course you provide a constant double, if you want to provide a constant float you should have put 3.14f

Expand|Select|Wrap|Line Numbers
  1.  
  2. printf("%d",printf("sun"));
  3.  
You have 2 printfs, the outper printf prints the result of the inner printf. You have guess at the meaning of the return value of printf now go and read the documentation to see if you are right.
Jan 8 '09 #2
donbock
2,426 Expert 2GB
@Parul Bagadia
In the first case, the divide-by-zero occurs at compile-time. Your compiler should have reported an error. In the second case, the divide-by-zero occurs at run-time. Whether or not you get a divide-by-zero exception depends on the capabilities of the target environment. A compiler with a really savvy static analyzer might be able to recognize the second divide-by-zero at compile-time.

Most of these questions probe the implementation-dependent-behavior and undefined-behavior of your particular compiler. What's the point of that? I hope you are not intending to write programs that depend on the behavior you observed!

No amount of observation can make undefined-behavior predictable. It can vary with the phase of the moon.
Jan 8 '09 #3
Parul Bagadia
188 100+
[quote=Banfa;3451346]
Expand|Select|Wrap|Line Numbers
  1.  
  2.   printf("%d%d",scanf("%c",&no),no); 
  3.  
You have provide a int * to scanf where you should have had a char *. However the order of function parameter evaluation is not guaranteed so it may have evaluated no before scanf("%c",&no) on your platform.

Thanks for all ,but i didnt understand this one, how the output is 1 and 3 , am not understanding 1 part, it should give output as the no. accepted by user.,if am not wrong.
Jan 9 '09 #4
Banfa
9,065 Expert Mod 8TB
scanf returns the number of items if converted, which in this case is 1, and printf prints that first.

1 is the return value of scanf, 3 is the value of no when it was interpreted to be placed in the parameter list for the printf call.
Jan 9 '09 #5
JosAH
11,448 Expert 8TB
@Banfa
It's just indefined behaviour because you don't know the order of evaluation of the arguments of the printf function: scanf might or might not have scanned a value for the 'no' variable when the value of that variable is needed.

kind regards,

Jos
Jan 9 '09 #6
Banfa
9,065 Expert Mod 8TB
It is not undefined behaviour, true you do not know the order the parameters of the printf call are made in but there is a sequence point at the end of the call to scanf so all the side effects of the function call are resolved.

The unknown parameter evaluation sequence just means that you can not know if printf will receive the value of no before or after the scanf call but when no is evaluated it will have 1 of these 2 values (note in the original example no is initialised).

The behaviour is not undefined, it is very clearly defined, and specifically on a given platform will always give the same result (since parameter evaluation order rarely changes). That result is just not at all useful in a portable program.
Jan 9 '09 #7
JosAH
11,448 Expert 8TB
At least it is "unspecified behavior":

[#1] unspecified behavior
behavior where this International Standard provides two or
more possibilities and imposes no requirements on which is
chosen in any instance
but I think this particular case is still undefined behavior because of this:

[#4] The order of evaluation of the operands is unspecified.
If an attempt is made to modify the result of an assignment
operator or to access it after the next sequence point, the
behavior is undefined.
... but I'll think about it a bit more (it is silly without doubt ;-)

kind regards,

Jos
Jan 9 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Zip Code | last post by:
"The large print giveth, and the small print taketh away.", so said Tom Waites in his classic rap, "Step Right Up", a paean about come ons and rip offs. Now, we have all explored the fact that...
761
by: Neo-LISPer | last post by:
Hey Recently, I researched using C++ for game programming and here is what I found: C++ game developers spend a lot of their time debugging corrupted memory. Few, if any, compilers offer...
59
by: Alan Silver | last post by:
Hello, This is NOT a troll, it's a genuine question. Please read right through to see why. I have been using Vusual Basic and Classic ASP for some years, and have now started looking at...
5
by: Zip Code | last post by:
"The large print giveth, and the small print taketh away.", so said Tom Waites in his classic rap, "Step Right Up", a paean about come ons and rip offs. Now, we have all explored the fact that...
131
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.