+ 1

When i use %d in place of %s I see this result, can you explain the number in place of string

https://code.sololearn.com/cudv05kdl24a/?ref=app

8th Jun 2018, 2:36 PM
Tarun Mathur
Tarun Mathur - avatar
3 ответов
+ 5
I originally thought that the resulting integer value represents the hex address of the (first character of the) string literal converted to decimal. Upon looking up threads and other resources, it appears that formatter and type mismatch for printf() simply results in undefined behaviour.
8th Jun 2018, 2:59 PM
Hatsy Rei
Hatsy Rei - avatar
+ 3
You want to use %s for string values. To explain it simply, you basically told it to give you a numerical value for "apples" instead of its string value.
8th Jun 2018, 2:41 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 3
Thanks for the info Hatsy. I've played around with it in the past, but the numbers never made any type of sense to me, so I didn't pursue it further since it wasn't necessary. I could conclude that it has nothing to do with the literal itself, as you'll get the same result regardless of the value. It's certainly related to the formatter, maybe referring to its position in the string relative to the other formatters so it knows where to place the value we give? Not certain the logic behind the exact number it gives, but it does seem to reference something in particular and is consistent with the number based upon the formatters relation to the other formatters. -shrug-
8th Jun 2018, 3:12 PM
Fata1 Err0r
Fata1 Err0r - avatar