#include <stdio.h>
int main()
{
printf(5 + "Good Morning\n");
return 0;
}
The code prints Morning. Should the code print Morning or should it show undefined behavior?
The printf() function sends a formatted string to the standard output (the display). This string can display formatted variables and special control characters, such as new lines ('\n'), backspaces ('\b') and tabspaces ('\t'); these are listed in Table 2.1.
Example 1: C Output The printf() is a library function to send formatted output to the screen. The function prints the string inside quotations. To use printf() in our program, we need to include stdio.h header file using the #include <stdio.h> statement.
% indicates a format escape sequence used for formatting the variables passed to printf() . So you have to escape it to print the % character.
It should show 'Morning'.
You are using pointer arithmetic - though you appear not to know it! "Good Morning\n"
is a char *
pointer to a constant string. You are then adding 5 to this pointer, which advances it by 5 characters. Hence the pointer now points to the 'M' of 'Morning'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With