Thread: question - linux - gcc - c standards

  1. #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.

  2. #2
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    More to the point, gnu89/gnu99 aren't standards, they are the GCC compiler extensions to the standard.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #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.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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..."
    Last edited by MK27; 06-14-2009 at 02:29 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #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

  8. #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.

  9. #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.

  10. #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.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Apr 2009
    Posts
    2

    extern should be the better option

    @MK27:
    should not we use
    extern char * strcasestr(const char *, const char *);

    rather than using
    char * strcasestr (const char *haystack, const char *needle);

    Thanks and Regards,
    Sandeep Patra

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by sandeep_patra
    should not we use
    extern char * strcasestr(const char *, const char *);

    rather than using
    char * strcasestr (const char *haystack, const char *needle);
    Arguably yes, but I do not think that declaring function declarations as extern really makes any difference.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

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

Tags for this Thread