For some reason, adding \n to printf() changes the behaviour of below code. The code without \n prints (null) whereas the code with \n leads to Segmentation fault.
Printf.c
#include <stdio.h>
int main(int argc, char* argv[]){
printf("%s", argv[1]);
}
Printf.c - Output
$ gcc -o Printf Printf.c
$ ./Printf
(null)
Printf_Newline.c
#include <stdio.h>
int main(int argc, char* argv[]){
printf("%s\n", argv[1]);
}
Printf_Newline.c - Output
$ gcc -o Printf_Newline Printf_Newline.c
$ ./Printf_Newline
Segmentation fault (core dumped)
I am curious to understand the reason behind this.
NULLpointer for a"%s"specifier, that's strictly true but why then did it print(null)in the other case if there is no such string passed toprintf()? I think this might be an actual bug somewhere.