Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is gcc not showing a warning message for using $ in a variable name?

Tags:

c

gcc

gcc-warning

I'm new to C and am learning C from Programming in C, 4th ed. by Stephen Kochan. On page 29, he writes $ is not a valid character for variable names. He is using the C11 standard.

I wrote the following code

#include <stdio.h>

int main (void)
{
    int a$ = 1;

    printf ("%i", a$);

    return 0;
}

and ran it with the command gcc -std=c11 -pedantic practice.c -o practice.o && ./practice.o. My filename is practice.c.

The output is 1. Shouldn't the compiler give me a warning for using $? Isn't using $ sign for identifiers an extension that GCC provides?

I'm using GCC 8.2.0 in Ubuntu 18.10.

Edit:

Also, doesn't GCC not use the GNU extensions when I use -std=c11? That is what is written in the Appendix of the book (pg. no. 497).

I am getting an warning by using -std=c89 though.

like image 386
Apoorv Potnis Avatar asked Mar 10 '19 11:03

Apoorv Potnis


People also ask

How do I enable warnings in GCC?

You can request many specific warnings with options beginning with ' -W ', for example -Wimplicit to request warnings on implicit declarations. Each of these specific warning options also has a negative form beginning ' -Wno- ' to turn off warnings; for example, -Wno-implicit .

How do I make GCC warnings as errors?

You can use the -Werror compiler flag to turn all or some warnings into errors.

Which GCC flag is used to enable all compiler warnings?

gcc -Wall enables all compiler's warning messages. This option should always be used, in order to generate better code.

What does the werror flag do?

The "-Werror" compiler flag treats all warnings as build errors. By promoting all warnings to errors, it enforces the developers to ensure such build warnings that may otherwise go unnoticed or only loosely concerned about by developers to now treat it with priority given that it will interrupt the build process.


1 Answers

You get a warning with -std=c89 -pedantic. C99 and later allow other implementation-defined characters in identifiers.

like image 173
nwellnhof Avatar answered Oct 26 '22 02:10

nwellnhof