12

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.

11
  • 8
    Unrelated, but the .o extension is usually used for object files, not for the final executable. Commented Mar 10, 2019 at 11:23
  • 3
    There's no extension for executables in *nix systems. The filesystem doesn't use that to determine the type of a file. So usually executables just don't have extensions and practice is correct. Check your /usr/bin directory and you'll see that the programs there don't have an extension either. Commented Mar 10, 2019 at 11:37
  • 2
    About the fact that its properties say "shared library" is probably because of your desktop environment. If I do file practice from the command line I get practice: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=928002f23b27d5c9bc55a15bf769edfaf3e62c23, not stripped Commented Mar 10, 2019 at 11:40
  • 1
    The file utility can show shared object for an ELF executable if it is a position-independent executable. Some distributions configure GCC so that it creates position-independent executables by default (usually this requires using -pie and -fPIC options). Commented Mar 10, 2019 at 15:06
  • 1
    @MrLister: Although traditional BASIC implementations used $ as a suffix for string-variable names, I wouldn't expect a trailing $ to have similar meanings in C. If I was examining code that used such a suffix, I'd expect that the programmer was exploiting some special way that the target implementation would process identifiers with such a suffix. For example, a compiler targeting a platform which can access objects near the frame pointer faster than those which are further away might place all objects whose names have a trailing $ after those that don't. Commented Mar 10, 2019 at 17:41

2 Answers 2

11

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

Sign up to request clarification or add additional context in comments.

1 Comment

Yes. This is the reference: gcc.gnu.org/onlinedocs/gcc-8.2.0/cpp/…
2

According to this : GCC Documentation

In GNU C, you may normally use dollar signs in identifier names. This is because many traditional C implementations allow such identifiers. However, dollar signs in identifiers are not supported on a few target machines, typically because the target assembler does not allow them.

So, $ is valid, but it's not a conforming way to code in C.

4 Comments

It's not valid C, only on GCC C. Try compiling with -ansi or -std=C11 and the warnings will start appearing.
@Spidey with -std=c11 no warning appears
@Spidey nope, still no warnings.
@Spidey: The Standard has no definition for "valid C". Its definition for a "conforming C program" encompasses any blob of text that is acceptable to at least one conforming C implementation. A source text that uses dollar signs in identifiers could not be a "strictly conforming proogram", but the authors of the Standard recognize that much C's usefulness stems from the ability to write non-portable programs that will be usefully processed by some C implementations even if not by all of them.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.