How can I tell what standard my C is in?

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.

1

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”.)

1

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.

2

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *