-1

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 3
    The behaviour of the program is *undefined*. There's lot's of undefined behaviour in both C and C++. Your best bet is to use your favourite search engine tool, and read some good books. – Bathsheba Sep 02 '20 at 06:39
  • 1
    There is no point in explaining undefined behaviour, because it's undefined - anything might happen. Here you promise to pass two parameters to `printf` but don't provide any, what did you expect to happen? – Lukas-T Sep 02 '20 at 06:41
  • To illustrate why the "garbage in, garbage out" concept appllies here: What values did you expect? From where should they be taken? Apart from `printf` instead of `print`. Please double check that you provide a [mre] for each question here. – Yunnosch Sep 02 '20 at 06:42
  • 2
    What is `print`? – PaulMcKenzie Sep 02 '20 at 06:45
  • Concerning the concept you ask about, are you aware of https://en.cppreference.com/w/cpp/io/c/fprintf ? – Yunnosch Sep 02 '20 at 06:47
  • 3
    If you are beginner in 2 different languages, maybe you should focus on one of them. Unless you are explicitely asking about differences between C and C++ or about combining parts in both languages, you should only add the tag of the language in use. – Gerhardh Sep 02 '20 at 06:50
  • Assuming you meant `printf`, have a look at : [Why doesn't a missing parameter in printf produce an error](https://stackoverflow.com/questions/31790728/why-doesnt-a-missing-parameter-in-printf-produce-an-error) ; [Behaviour of printf when printing a %d without supplying variable name](https://stackoverflow.com/questions/437816/behaviour-of-printf-when-printing-a-d-without-supplying-variable-name) – Sander De Dycker Sep 02 '20 at 07:16
  • What values were you expecting? – user253751 Sep 02 '20 at 10:49

2 Answers2

2

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;
}
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
1

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

JJ Guitar
  • 33
  • 6