For my answer I will assume that the shown code is NOT the code which produced the output your describe. I am convinced that it can't do that.
My assumption is that you have printf()
instead of print()
.
Looking at your code I also assume that you are thinking in C, because it is much closer to C than C++.
So let's discuss how you can avoid weird values in output from this slightly different code:
#include <stdio.h>
int main(void)
{
printf("%d %c\n"); //first print statement
printf("%d %c\n"); // second print statement
return 0;
}
In case you are very much a beginner, you might not be aware of the special meaning of "%d"
and "%c"
in the string of the first parameter to printf()
. This is discussed in the answer by JJ Guitar, see there and also the corresponding documentation:
https://en.cppreference.com/w/cpp/io/c/fprintf
If you did not know about the special meaning you probably wanted the output to be literally:
%d %c
%d %c
That output is achieved by "escaping" (using the special format specifier for outputting a "%" itself), i.e. by modifying the code like this:
#include <stdio.h>
int main(void)
{
printf("%%d %%c\n"); //first print statement
printf("%%d %%c\n"); // second print statement
return 0;
}
But you seem not so much surprised by values to be in the output, only by the strange values and maybe the fact that they change between the two identical statements.
So you probably are aware that the special characters are for outputting non-literal values. In that case I wonder which values you had in mind, because you do not mention them, neither in the question nor to the compiler. Actually there are no variables at all in your code which could be printed.
So in order to make your code output some reliable and non-literal output, quite some more change is needed, introducing variables, initialising them and then the last detail to actually mention them in the print calls.
#include <stdio.h>
int main(void)
{
int MyInteger=42;
char MyChar = 'X';
printf("%d %c\n", MyInteger, MyChar); //first print statement
printf("%d %c\n", MyInteger, MyChar); // second print statement
return 0;
}