Explain it | Sololearn: Learn to code for FREE!

-3

Explain it

#include<stdio.h> int main(){ int a=10; printf("%d"+1); return 0; }

c++ c

6/21/2021 4:40:26 PM

Abhishek Bairwa

21 Answers

New Answer

+4

Abhishek Bairwa give credit to XXX for giving the answer, which you copied from your duplicate question https://www.sololearn.com/Discuss/2818025/?ref=app

+4

Martin Taylor even my first thought was that the code is incorrect. But the word "explain" in the question made me think that this might have been a trick question somewhere (possibly on SoloLearn). So I tried running the code and it worked, and the explanation to that was simple. Brian "Bill" The compiler gives a warning when +1 is changed to +0, which makes me wonder - did the compiler compute "%d" + 1 at compile time? I know C++ has compile-time operations, but I don't know if C has that. EDIT: apparently, yes. I read the assembly of a simple code ``` int main() { const char c = *("abcd" + 1); } ``` and I found that 98 ('b') is directly moved onto the stack without going through the operations to store the string and get the first character. See the assembly output by running this code (the only important line is line 13 of the output) https://code.sololearn.com/csVKBT4pdkPd/?ref=app I guess every compiled language has that much of optimization. I think this is why gcc doesn't flag a warning.

+2

Abhishek Bairwa the answer from XXX is correct. A similar way to think of "%d"+1 is as indexing a char array. It locates index 1 of {'%', 'd', 0}, which is 'd'. Though XXX gave a more direct explanation of why it works this way.

+2

Brian "Bill", yes that is indeed an excellent response from XXX. I didn't even realise that this was possible with the printf statement.

+1

Bro the code is incorrectly written... Revise the course again...

+1

Martin Taylor as you might expect, some compilers would flag it with a warning or an error because printf() is missing its second argument. This one is more forgiving, evidently.

+1

Lucifer Samael Morningstar using printf("%d"+0) is the same as printf("%d"), which is an error. Recognize that "%d" is a valid format specifier, but there is no numeric argument given after it to format and print. You get garbage output. Try printf("%d"+0, a). It will happily print 10.

+1

Abhishek Bairwa that first argument passed to printf() is the format specifier string. Within the format specifier string a percent symbol % is a metacharacter that denotes a formatting type. But in case you wish to use a literal % within the format specifier, C allows for that by interpreting %% as a single literal %. Therefore, "%%%%%%d" reduces to the literal string "%%%d". None of the percent symbols are used as metacharacters so the string is interpreted as not having any formats within it. As before, adding 1 to the char pointer skips the first character and prints the remainder of the string, which is "%%d".

+1

Lucifer Samael Morningstar in printf() the first argument is a format specifier string. The arguments that follow are the values to be formatted. If the first argument has no format type within it (as is the case with "d"), then gcc allows you to use printf() with only one argument. If a format is given (as is the case with "%d") then an error occurs because the next argument is missing. Edit: After further reflection on your last question, I think I should clarify that you are correct about strings being arrays of characters. A C string is stored within a char array and is terminated by a null character. A pointer to the string gets passed to printf(). So printf() will start at the pointer location and recognize all characters up to the null, not just one character.

0

Your prinf statement has a syntax error. http://www.cplusplus.com/reference/cstdio/printf/

0

Martin Taylor but it is printing d in Sololearn. Is SoloLearn compiler is different? https://code.sololearn.com/cvcoQIcY6Sex/?ref=app

0

0

Abhishek Bairwa What no?

0

I don't understand why it print d only

0

Abhishek Bairwa I am also waiting for others answer.

0

Ok but this answer is correct or not

0

The code is incorrectly written I think

0

XXX When I write printf("%d"+0) why it shows something else?

0

Brian "Bill" When I write ("%d" +1) it shows d then ("%d"+0) is error? Wasn't it kind of an array?

-1

#include<iostream> using namespace std; int main(){ int a=10; printf("%%%%%%d"+1); return 0; }. It gives Output %%d Why