3

Okies, totally newbie question here. I can read the code, mimic the code and understand the basics to be deadly. But, I never really took the time to understand what ANSI C really meant. I just look and code. Been doing it since I was 15 years old and I'm 33 now.

It's been awhile since I touched C, but getting back into the swing of things with some code on my Linux box made things difficult. My code is old, everything else is new from the Linux distro to GCC. When I go to compile, everything works, but now I have slew of new warnings that I can only assume tie to a new standard?

 gcc (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4

How can I tackle this problem? I never quite understood the difference between C89 and C11. How can I tell what I'm using?

Any help is much appreciated. I want to try to resolve these warnings while also trying to learn what I clearly failed to grasp.

Fastidious
  • 149
  • 6
  • 2
    Are you asking how to tell the compiler what version of the standard to use or how to tell what version the code you've written complies with? Also, the warning message itself should tell you what's (looking) wrong. If you don't understand what a particular warning means, please post minimal but self-contained code to trigger it and the exact text of the warning message itself. – 5gon12eder Mar 26 '16 at 03:52
  • 1
    New warnings can just mean a new compiler, since the standard doesn't say the compiler has to emit warnings, but compilers add them as and when they are found to be useful – user253751 Apr 21 '23 at 13:10

3 Answers3

9

In the case of gcc, you can tell the compiler what C standard to use via the --std option. Running man gcc will explain this, and will list all of the standards that are supported. Note that there are lots of variations including some "standards" that are GCC specific extensions / variations to the official standards.

If you want to understand why certain things in your code are giving you warnings, you would need to trace back to the particular standard that the warning message is referring to, and READ the relevant part of the standard.


But, I never really took the time to understand what ANSI C really meant.

You need to remedy that. Your poor understanding is most likely the root of a lots of mysterious bugs that arise when you change compilers, port code from one platform to another, etcetera. (The kind of thing that lots of people incorrectly blame on "compiler bugs".)

Stephen C
  • 25,180
  • 6
  • 64
  • 87
  • I ended up finding the --std option and applying it to the Makefile. I saw that my version of GCC is default --std=gcc11 or whatever. – Fastidious Mar 26 '16 at 15:00
5

If you were not consciously trying to follow the ANSI standard when writing the code, then most likely your code will not conform to any of the ANSI C standards.
The reason for this is because there are no C compilers that by default enforce an ANSI C standard, but they all accept their own dialect of C.

To add to that, if your program tries to do user-interaction in a more fanciful manner than "print a prompt and read a line of text", then you must already step beyond the facilities provided by ANSI C. If you are unaware of that, then it is easy to pick a solution that is specific for the compiler you are using at that time.


If you want to start following ANSI C strictly, then you must accept that your programs can't do fancy user interaction and can't support most other kinds of connections to the outside world.

If you don't actually care that much about standard conformance, but you want your program to be portable between different platforms and compilers, then you should learn about ANSI C to know what it provides and then deliberately choose portable (3rd-party) libraries for the features that you need beyond what the satndard offers you.

Bart van Ingen Schenau
  • 71,712
  • 20
  • 110
  • 179
  • Just for context. The program is a online game. So, fancing user interactions and connection to the outside world is a must. Does that mean I should steer away from ANSI C? That could be why it's not like it. – Fastidious Mar 26 '16 at 14:58
  • 3
    @Fastidious: The game logic can probably be done in ANSI C, but for the network connections you will need to use facilities that are not available in ANSI C. – Bart van Ingen Schenau Mar 26 '16 at 18:22
0

As Stephen C suggestion above, you could use:
man gcc | grep -e "-std=c++"

With this command line in the Terminal, I received:

           equivalent to -std=c++98.
               GNU dialect of -std=c++98.
               GNU dialect of -std=c++11.  The name gnu++0x is deprecated.
               GNU dialect of -std=c++14.  This is the default for C++ code.
               GNU dialect of -std=c++1z.  Support is highly experimental, and
           -ansi, -std=c++98, -std=c++11, etc.
           default for -std=c++1z.
           under -std=c++14 and above.  The flag -Wsized-deallocation warns
           order, as adopted for C++17.  Enabled by default with -std=c++1z.
           and shift expressions, and is the default without -std=c++1z.
           -std=c++1z.
           GNU dialects: -std=c++98, -std=gnu++98, -std=gnu++11, -std=gnu++14.
           This option is off by default for ISO C++11 onwards (-std=c++11,

The default standard is "c++14" as you see in the comment.

Cloud Cho
  • 101
  • 2