There's some weird bug in either GCC or the shell on my computer

C

CrazedNerd

Guest
I really don't have any idea what's going on, but for some reason GCC sends out errors even after you correct your c code:

Code:
vegas.c:23:37: warning: format ‘%d’ expects a matching ‘int’ argument [-Wformat=]
   23 | printf("In the vegas() function, a=%d\n,a");
      |                                    ~^
      |                                     |
      |                                     int

This came after correcting and saving the file even though the program did what it was supposed to after the error message, and it didn't go away until i deleted the binary that runs the code. This has happened before, where i had to erase "a.out" in order to get it to compile and run without error messages. It's supposed to over-write the file but there's obviously something amiss here.

I don't have the latest gcc update and updating for some reason doesn't work. I'm not too worried about that because i'm going to reformat my desktop sometime soon, so i'm just seeing if anyone knows about this problem or if the linux command line tools team corrected this already...
 


@MattWinter must have been a great teacher at one time. There is a bug in your C code. He is guiding you in the right direction. I will share another hint:

There should be two arguments in your example printf() function above. The format string comes first. The format string includes the %d. The second argument is the int value that the %d references. The warning message is pointing at the %d and saying, "I can't find the int argument (2nd printf argument) for this %d."

Pause here and think about it.

Extra Hint:
The trailing quotation mark should come before the comma. If you move the trailing quotation mark, the final "a" becomes the int variable that the %d expects.
 
Last edited:
Late to the party, but @MattWinter and @sphen are 100% correct.

The printf command takes a format string as its first parameter and then expects one additional parameter for each placeholder you have in your format string.

The closing double quote in that line of code is in the wrong place. It should be between the \n character in your format string and the comma.
The comma is supposed to separate your format string and the variable you want the placeholder to use.

As the code stands, you have one int placeholder in your format string and because the closing quote is in the wrong place, the comma and the variable name a are now part of the format string.

So as far as the compiler is concerned - you have not passed it an int parameter for the placeholder in the format string and it’s giving you a warning.

Your program will compile and build, but it’s giving you a warning that the line in question is not providing a value to substitute into the placeholder.

So at runtime, that particular line of code isn’t going to display a value for that placeholder. So the problem will manifest as a bug at runtime. In other words, you won’t see your expected output when that line of code is ran. So with that particular warning, you’ll see a bug at runtime.

One way to avoid runtime bugs due to warnings like that particular one is to tell gcc to treat all warnings as errors, using gcc’s -Werror flag when you compile. That way if your code generates any warnings, they will be counted as errors and your program will not build successfully until you’ve fixed all errors AND warnings.
 
Late to the party, but @MattWinter and @sphen are 100% correct.
Yeah, the thread is about how i fixed the error but had to erase a.out before the message went away. I saved the file several times before re-compiling.
 

Members online


Latest posts

Top