简体   繁体   中英

Adding an integer to a string in C, how to understand the result?

How does the output turns out to be morning.

#include<stdio.h>
void main()
{
    printf(5+"Good Morning");
    return 0;
}

output is Morning

Pointer arithmetic: you added 5 to the char array position, skipping the 5 first chars, ie Good and space.

It is legal as long as you don't exceed the size of the string, in that case, you have undefined behaviour

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM