1

First,I paste the source code :

#include <stdio.h>
int main()
{
    printf("%c",'abcdefg');
    return 0;
}

I know %c is used to print one character,but I want to know for this source code why the terminal print the last character of the string.

thanks,everyone, Here are similar question: Please explain this result please. printf("%c", 'abcd')

sepp2k
  • 363,768
  • 54
  • 674
  • 675
beaTc
  • 11
  • 4
  • 3
    That code is invalid. Turn on your compiler warnings. – John Zwinck May 27 '17 at 03:42
  • It's compiling `g` mostly by a random mistake... it's using the string pointer information, casts the pointer data to a `char` and prints it's value. – Myst May 27 '17 at 03:47
  • duplicate of [What do single quotes do in C++ when used on multiple characters?](https://stackoverflow.com/q/7459939/995714) – phuclv May 27 '17 at 03:51
  • It's compiling g has its reason,not a random mistake, for general, it's always print the last character of a string if you use the informal code – beaTc May 27 '17 at 04:11
  • 1
    @myst: nope, there is no string pointer there (well, except for the format) and the behaviour is probably deterministic (it is implementation-dependent and the implementation​ produces a warning, so who knows, really. But I had no trouble reproducing it. Read the warning carefully. http://coliru.stacked-crooked.com/a/80268f03c4220d76 – rici May 27 '17 at 04:14
  • 1
    @beatc: it is not a string. – rici May 27 '17 at 04:15
  • 5
    See (ISO/IEC 9899:2011) §6.4.4.4 Character constants ¶10 _…The value of an integer character constant containing more than one character (e.g., `'ab'`), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined…_ What you get from a 7-character literal `'abcdefg'` is therefore dependent on the compiler. – Jonathan Leffler May 27 '17 at 04:17
  • And here, for the record, is the required implementation-dependent documentation for one popular compiler: http://gcc.gnu.org/onlinedocs/cpp/Implementation-defined-behavior.html#Implementation-defined-behavior (fourth bullet point). – rici May 27 '17 at 04:22
  • @rici - Nice call catching the single quotes... I guess from your link that you always catch the last "byte". BTW, if it were a string (rather than a having a compiler error), it would still be deterministic... after all, the data will be in the `data` segment which has a persistent pointer location in relation to the executable root (which is page aligned). Hence, the "delta" for the data segment from the root pointer, reduced to the last byte of the information, will always be the same. – Myst May 27 '17 at 04:54
  • 1
    voted to reopen so it can be closed as duplicate as suggested by OP – M.M May 27 '17 at 09:05

1 Answers1

3

%c only prints one character, you should use %s instead. single quotes denote a single character, double quotes denote strings. so you should be writing printf("%s","abcdefg");

e2298
  • 145
  • 1
  • 1
  • 7