I am trying to print some characters on screen. I have used a big variation of wrong implementations. For example:
Example 1:
#include <stdio.h>
int main(int argc, char const *argv[]) {
char hello[6] = {'h', 'e', 'l', 'l', 'o', '\n'};
char bye[5] = {'b', 'y', 'e', '\n', '\0'};
char end[] = "end";
char dot = '.';
char oops[] = {'o', 'o', 'p', 's'};
printf("%s\n", hello);
printf("%s\n", bye);
printf("%s\n", end);
printf("%s\n", dot);
printf("%s\n", oops);
return 0;
}
Output 1:
Questions:
Why the dot
character has been printed before bye
?
What are these rubbish after the dot
character?
Example 2 (removed dot
declaration/definition, print):
#include <stdio.h>
int main(int argc, char const *argv[]) {
char hello[6] = {'h', 'e', 'l', 'l', 'o', '\n'};
char bye[5] = {'b', 'y', 'e', '\n', '\0'};
char end[] = "end";
char oops[] = {'o', 'o', 'p', 's'};
printf("%s\n", hello);
printf("%s\n", bye);
printf("%s\n", end);
printf("%s\n", oops);
return 0;
}
Output 2:
Questions
The rubbish are still there! I am feeling good for them but why the end
was printed twice?
I managed to do it correctly using termination character whenever it should be used, but why do I have this kind of inconsistency in printing? I am coming from a Java background and I am already feeling weird!
printf("%s\n", dot)
). That is also undefined behavior.