Challenges :P | Sololearn: Learn to code for FREE!

+10

Challenges :P

Additionals ones cause it's boring to have the sames over and over ! Result in 5 days ----------------- 1st Chall: What is the output ? int main () { char omg; for (omg = 0; omg < 128; omg++) { printf ("%d", omg); } return 0; } A - Infinite Loop B - Print 0 to 127 C - Print 0 to 128 D - Error ------------------- 2nd Chall : What is the output of the following program ? #define square(×) ×*× int main() { int i; i = 20 / square(2); printf("%d", i); return 0; } A - Undefined behavior B - Compiler Error C - 20 D - 5 ----------------------- 3rd Chall : What is the output of the following program ? void main () { int i = 5; printf ("%d%d%d%d%d", i++, i--, ++i, --i, i); } A - Undefined behaviour B - 45545 C - 54421 D - 65655 ------------------------- 4th Chall : What is the output ? int main () { int wtf = 1; printf("%d", wtf+(--wtf)-++wtf); } A -> -1 B -> 1 C -> 0 D -> Undefined behaviour -------------------------- 5th Chall : What is the output of the following program ? void main () { int const * p = 5; printf("%d", ++(*p)); } A - Compiler Error B - 6 C - 5 D - Answer D (haha) ----------------------- That's all for now ! Any upvote is greatly appreciated since it will help to be seen by a lot of our community ! PLEASE DO NOT EXPLAIN WHY YOU ANSWER A OR B OR C OR D (NO SPOIL)

8/15/2016 1:46:15 PM

Romain Koszyk

14 Answers

New Answer

+7

Since there will most likely no more players I release the results :) The maximum amount of good answers per player is.... 1 ! Yeah it was a vicious serie :D All code will be posted in the code playground named as 1stCustomChall InfiniteLoopChar, 2ndCustomChall BroDidYouSaySquare, 3rdCustomChall ComputerBrainDead, 4thCustomChall ComputerBrainDead2, 5thCustomChall IAmConstant Enough talking, here are the explanations : 1st Chall Solution : The answer is A, the maximum value of char is CHAR_MAX which is 127, it can't go further, so since omg will not exceed 128, the for loop go infinite ("To infinity.... and beyond !") 2nd Chall Solution : The answer is C, the define call sets square(x) to x*x, so when the square function is called, it's substituted by x*x... In this case -> 20 / square(2) becomes 20 / 2 * 2, then you just apply the priority order and you have 20 / 2 -> 10 ; 10 * 2 -> 20

+6

3rd Chall Solution : The answer is A, you can't modify a value more than once in a single statement. Every change of value has to be separated by sequence points ";". However if you still does it results with an undefined behaviour, you just can't say what will be the output ! Per example, when I run the following here is my output -> 45552686816 (there are no explanations to that it's not a question about stack or left to right, it is just undefined...) 4th Chall Solution : The answer is D for the same reasons as in the 3rd, my output for the following is -1, however that doesn't mean that it is a result that we can expect everytime ! 5th Chall Solution : The answer is A, p is a constant pointer of an integer. So you can't change its value through p ! However if you consider : int c = 5; int const * p = &c; c++; printf("%d",(*p)); Then there is no Compiler Error, and the output is 6, because you change the value through c ! Here is a quick explanation about data types : char const * c -> c is a constant pointer of a character (you can modify the character but not the pointer or through the pointer) const char * c -> c is a pointer to a constant character (you can modify the pointer address but not the character itself even through the pointer) const char * const c -> c is a constant pointer to a constant character (well you're just stuck here, you can't modify the pointer address and the character's content) Fiou did you brain survived ? Have fun :)

+1

1 ans is ...print 0 to 127

+1

B B A C B... definitely unsure of 4 xD

+1

2 copmiler Error

+1

B D B B A

+1

One thing is sure, you'll learn things once the results will be released :)

+1

B D D A A

+1

l m sorry but I didn't understand the explanation to the first challenge. Can you please explain in detail what's with this CHAR_MAX??

+1

@kamal joshi: Further explanations about 1st Chall : CHAR_MAX is a macro that holds a certain value, which is 127, you can't go over, and you can't under -128 because of CHAR_MIN ! If you declare a signed char then it works the same way, the macros that will handle the stuff are SCHAR_MAX ( -> +127) and SCHAR_MIN( -> -128). However if you declare an unsigned char then the macro is UCHAR_MAX (-> 255). But when you don't precise what type of char it is, it's handled by CHAR_MAX and CHAR_MIN. All these things are part of <limits.h> library which you can find on Internet for more details about the others macros :) In that case the macros CHAR_MAX and CHAR_MIN were there, so you can't ask you char to hold a value > 127, but the loop is trying to, as it just can't the char value stay lower than 128 and keep looping ! Hope it helps !

0

Keep it up, not that much good answers until now :p

0

not sure of the the fourth one

0

thanks.... it really helped

0

You're welcome :)