- 3

Explain it

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

21st Jun 2021, 4:40 PM
Abhishek Bairwa
Abhishek Bairwa - avatar
19 ответов
+ 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
21st Jun 2021, 8:01 PM
Brian
Brian - avatar
+ 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.
22nd Jun 2021, 4:53 AM
XXX
XXX - avatar
+ 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.
21st Jun 2021, 8:12 PM
Brian
Brian - avatar
+ 1
Bro the code is incorrectly written... Revise the course again...
21st Jun 2021, 5:27 PM
kaif shaikh
kaif shaikh - avatar
+ 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.
21st Jun 2021, 11:56 PM
Brian
Brian - avatar
+ 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.
22nd Jun 2021, 6:48 AM
Brian
Brian - avatar
+ 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".
22nd Jun 2021, 3:16 PM
Brian
Brian - avatar
+ 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.
22nd Jun 2021, 3:33 PM
Brian
Brian - avatar
0
Martin Taylor but it is printing d in Sololearn. Is SoloLearn compiler is different? https://code.sololearn.com/cvcoQIcY6Sex/?ref=app
21st Jun 2021, 7:35 PM
A͢J
A͢J - avatar
0
No
21st Jun 2021, 7:41 PM
Abhishek Bairwa
Abhishek Bairwa - avatar
0
21st Jun 2021, 7:42 PM
A͢J
A͢J - avatar
0
I don't understand why it print d only
21st Jun 2021, 7:47 PM
Abhishek Bairwa
Abhishek Bairwa - avatar
0
Abhishek Bairwa I am also waiting for others answer.
21st Jun 2021, 7:51 PM
A͢J
A͢J - avatar
0
Ok but this answer is correct or not
21st Jun 2021, 8:07 PM
Abhishek Bairwa
Abhishek Bairwa - avatar
0
The code is incorrectly written I think
21st Jun 2021, 8:24 PM
Opel
0
XXX When I write printf("%d"+0) why it shows something else?
22nd Jun 2021, 5:55 AM
Lucifer Samael Morningstar
0
Brian "Bill" When I write ("%d" +1) it shows d then ("%d"+0) is error? Wasn't it kind of an array?
22nd Jun 2021, 7:56 AM
Lucifer Samael Morningstar
- 1
#include<iostream> using namespace std; int main(){ int a=10; printf("%%%%%%d"+1); return 0; }. It gives Output %%d Why
22nd Jun 2021, 11:04 AM
Abhishek Bairwa
Abhishek Bairwa - avatar
- 2
The output is "d" (which you could've checked yourself). This is because "%d" is actually a char pointer to the memory location where the first character ('%') of the string is stored. When you add 1 to it, the pointer is offset by 1 and now points to the character 'd'. So printf() prints the string from 'd' till the end. The variable 'a' is useless. This can be possible
21st Jun 2021, 7:52 PM
Abhishek Bairwa
Abhishek Bairwa - avatar