简体   繁体   中英

Why is this code giving me garbage values?

I am a beginner in C and C++ programming. I know the basics, but not deep down basics.

Here is the code -

#include <stdio.h>

int main()
{
    print("%d %c\n"); //first print statement
    print("%d %c\n"); // second print statement
    return 0;
}

It is outputting the garbage values:

1174315864 h
2147483635 �

Please explain this to me, it will be very helpful.

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), ie 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;
}

the print statement includes the placeholders %d and %c in your string. These values are replaced by the following variables. As you didn't provide any variable after the string, it grabs some part of the succeeding memory - and that memory wasn't initialized. The second print statement looks like the first but has not the same position in memory. This way it comes that the output is different.

I recommend to look for a good beginners book, webside, or video course to learn the basics.

Regards JJ

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