C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-14-2009, 11:44 AM   #1
Registered User
 
Join Date: Jun 2009
Posts: 5
Question question - linux - gcc - c standards

I have just started learning C.
I use Linux with gcc 4.2.3 (I can upgrade the version, no problem)
my problem is with C standards, here is what I know:
there are currently two standards of C implemented c89 and gnu89, and two not yet fully implemented c99 and gnu99
gnu89 is the default now, gnu99 will be the default in the future.

my question:
which standard should I use (I guess it's gnu89)? and what is the standard in which most apps are coded in, for example the linux kernel is coded in which one?

I want my code to be compatible at least under all Linux distros, but I would also like if it compiles under other operating systems too.

also if someone will recommend a standard, what book should I use as a reference for that standard ("C complete reference" is good for C89 and C99, but what about the GNU extensions in gnu89/99 ?)

Thanks in advance.
AMAMH is offline   Reply With Quote
Old 06-14-2009, 11:52 AM   #2
Registered User
 
slingerland3g's Avatar
 
Join Date: Jan 2008
Location: Seattle
Posts: 476
ISO/IEC JTC1/SC22/WG14 - C
slingerland3g is offline   Reply With Quote
Old 06-14-2009, 11:57 AM   #3
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
More to the point, gnu89/gnu99 aren't standards, they are the GCC compiler extensions to the standard.
tabstop is offline   Reply With Quote
Old 06-14-2009, 12:30 PM   #4
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by AMAMH View Post
I want my code to be compatible at least under all Linux distros, but I would also like if it compiles under other operating systems too.
The POSIX Standard is upheld by all linux and unix-based systems:

The Open Group Base Specifications Issue 7

if you look under alphabetical index, this is just the manual pages for all standard C functions as implemented under POSIX. They are pretty good man pages too.

Generally, tho, just use the GNU C Reference
The GNU C Library
since your C library is built by GNU, and they do include explicit notes there about "gnu extensions" that may effect portability.

Portability issues are specific issues. You will become aware of them as they become involved with your programs, don't worry. For now, you do not need to add any compiler flags except this one:

gcc -Wall

That will help to keep your code proper and as portable as possible, eg it will warn you when you use a gnu specific extension like this:

warning: implicit declaration of function ‘strcasestr’

because GNU's strcasestr is GNU specific.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 06-14-2009, 01:47 PM   #5
Registered User
 
Join Date: Jun 2009
Posts: 5
thanks MK27 for ur reply.
u really helped me, especially for pointing that I should use -Wall to disable gnu extensions, but isn't gcc available for all operating systems? I mean why disable its extensions if I know that it's kinda portable too. Anyway, since I see that flag a lot "-Wall" while compiling programs from source it must be important, I will likely use it.
thanks also for the GNU reference and POSIX reference.

I think for now I will just use the default gcc standard (gnu89) and more likely with gnu extensions disabled, I think this is equivalent to -std=c89, but I will just use -Wall.
AMAMH is offline   Reply With Quote
Old 06-14-2009, 02:23 PM   #6
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by AMAMH View Post
thanks MK27 for ur reply.
u really helped me, especially for pointing that I should use -Wall to disable gnu extensions, but isn't gcc available for all operating systems?
This doesn't actually disable them; it is just to let you know. If you are using the GNU C library with your compiler, you can use strcasestr. What that message ("warning: implicit declaration of function") usually means is that you don't have a prototype defined for your function -- which makes it slightly confusing, so try to remember this!

To make a long story short, if you run across that warning because of a library function (not your own*) and you want to get rid of it and still compile with -Wall, just copy the prototype from the manual into your program code at the top:
Code:
char * strcasestr (const char *haystack, const char *needle);
Now you can compile with -Wall and there will be no warnings. I don't know how or why gcc uses this method. It seems to me:

warning: strcasestr is a GNU specific extension to the C standard

would have been fine. In any case, "forcing" you to copy the prototype in does serve as a strong reminder of the fact that that function may have limited portability.

There are actually not many functions like that, strcasestr is the only one I could think of off the top of my head.

And yes, gcc is available for available for more OS's than I've ever heard of. I don't think it has to use the GNU C library to work; certainly MS users use it and they are not using the GNU library. It is good you are thinking pro-actively, but don't sweat about this stuff too much right now.

* if it's because of your own function you forgot to include a prototype: that is the original meaning of "warning: implicit declaration..."
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS

Last edited by MK27; 06-14-2009 at 02:29 PM.
MK27 is offline   Reply With Quote
Old 06-14-2009, 05:06 PM   #7
Registered User
 
Join Date: Jun 2009
Posts: 5
well, I was lazy and didn't read the manual. Actually -Wall is just "warnings all", it's not about gnu extensions specifically.
still it's good to use it, if not to make sure the code has no errors at least it will make me know more about the language,compiler,etc
AMAMH is offline   Reply With Quote
Old 06-14-2009, 07:36 PM   #8
Registered User
 
Join Date: Apr 2008
Location: Haddock, GA, United States
Posts: 13
Good C programming books!

If you're a C beginner, I think you should buy C Primer Plus Fifth Edition by Stephan Prata, and after you finish reading that one, you can also buy The C Programming Language Second Edition by Kernighan and Ritchie. I think the two books mentioned are really good for learning and mastering C. Well, mastering actually comes with a lot of practice, like any other thing. I hope you'll enjoy reading them, if you are going to buy the two books I recommended. Good luck, and if you're willing to have a study partner, I'm also studying C myself, etc. Goodbye for now, and I will talk to you soon.
adrian.mowrey is offline   Reply With Quote
Old 06-15-2009, 08:22 AM   #9
Registered User
 
Join Date: Jun 2009
Posts: 5
I don't think u understand my point
the new c standard (which is actually about 10 years old) is not completely implemented in gcc and a book like "The C Programming Language" was released before c99.
my question is:
should I just ignore the c99 standard and use the default gnu89 or c89? is c99 implemented enough in gcc that I can use it? I know that u can see the current status of c99 in gcc on its website but it's too technical for me. Actually c99 is planned to be the next default in gcc, so I can't just ignore it, besides, I HATE living in the past.
AMAMH is offline   Reply With Quote
Old 06-15-2009, 08:25 AM   #10
Registered User
 
Join Date: Jun 2009
Posts: 5
update:
I think actually my question is just like having a chat, it's not important to begin programming in C.
I will just use c99 as it's currently implemented in gcc (gcc 4.4.0) and I will use "C The Complete Reference" as my reference.
AMAMH is offline   Reply With Quote
Old 06-15-2009, 08:31 AM   #11
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,354
Quote:
Originally Posted by AMAMH
should I just ignore the c99 standard and use the default gnu89 or c89?
If you want to maximise portability, sticking with C89 would be your best bet.

Quote:
Originally Posted by AMAMH
is c99 implemented enough in gcc that I can use it?
What specific language features of C99 do you have in mind?

Quote:
Originally Posted by AMAMH
Actually c99 is planned to be the next default in gcc, so I can't just ignore it, besides, I HATE living in the past.
I believe that any valid C89 program should be a valid C99 program, the use of obsolescent language features and language extensions aside.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Reply

Tags
gcc, linux, question, standard

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Run time differences in Linux gcc versions circuitbreaker C++ Programming 7 02-14-2008 11:09 PM
gcc configuration under linux advice needed vart Tech Board 9 01-10-2007 02:46 PM
Question about Linux Firewall naruto Linux Programming 6 07-25-2004 12:39 PM
question from linux board ( not os dependant ) crypto C Programming 4 11-15-2002 02:09 AM
Question about LINUX River21 A Brief History of Cprogramming.com 0 09-17-2001 06:39 PM


All times are GMT -6. The time now is 12:35 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22