Beginning C - current source of practical info on POSIX, and related, for a beginner?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Beginning C - current source of practical info on POSIX, and related, for a beginner?
I'm self-studying C, for fun. I'm 58, and have little previous programming experience. Even though I have no plans to write code professionally, I'd like to learn C properly rather than just enough to write a few things that work only on my own desktop which may not be portable.
I'd like to find a source which discusses the relative importance of various standards as applied to current programming practice. For instance, should I pay attention to ANSI, or POSIX, or both? Other important relevant standards I should learn about? Has POSIX all-but-replaced ANSI? Is POSIX.1-2008 a superset of POSIX.1-2001? Which of the many compiler flags should I use (or not use)? Which functions/macros should I prefer (or avoid) for portability and robustness? Etc.
Can you recommend a current source that discusses the practical use of standards for beginning programmers, particularly for C in the *nix environment?
I have a copy of "POSIX Programmer's Guide", by Donald Lewine, which I find somewhat useful, but it's 20 years old...
Is there an online source that I could easily check/browse?
POSIX is Linux's programming API, and one of the things it includes is ANSI C, which is the specification of only the C language and its core functions. ANSI, therefore, is a subset of the whole POSIX specification. Some things that you find on POSIX but not on ANSI C, for example, is threads, mutual exclusion semaphores, pipes and message passing.
Now, that said, the GNU C Library that comes bundled with all Linuxen does more than just implementing the ANSI C standard; it goes all the way to implementing the whole POSIX specification as well as the programming part of the Single UNIX Specification. This is why there's also the µClibc, which is a stripped-down GNU C Library for microcontrollers and embedded systems.
You say ANSI is a "subset of the whole POSIX specification", but this weekend (for example) I found that strdup is POSIX.1-2001 conforming but not ANSI- using the -ansi flag for gcc I get an "implicit declaration" warning. The man page says "strdup() conforms to SVr4, 4.3BSD, POSIX.1-2001". So, evidently, strdup is POSIX but not ANSI... at least that is how it appears to me.
I'd like to find a resource which explains this type of detail.
Keith Ostertag
EDIT: It occurs to me after more thought... using the -ansi compiler flag is not such a great idea, as it limits me to the old ansi, which would only be useful for compiling for (much) older machines. This is likely to be true for a lot of the compiler flags- which is why I'd like to find a source for advice on which flags to use for which standard and/or purposes...
Last edited by keithostertag; 03-12-2012 at 07:44 PM.
I'm self-studying C, for fun. I'm 58, and have little previous programming experience. Even though I have no plans to write code professionally, I'd like to learn C properly rather than just enough to write a few things that work only on my own desktop which may not be portable.
I'd like to find a source which discusses the relative importance of various standards as applied to current programming practice. For instance, should I pay attention to ANSI, or POSIX, or both? Other important relevant standards I should learn about? Has POSIX all-but-replaced ANSI? Is POSIX.1-2008 a superset of POSIX.1-2001? Which of the many compiler flags should I use (or not use)? Which functions/macros should I prefer (or avoid) for portability and robustness? Etc.
Can you recommend a current source that discusses the practical use of standards for beginning programmers, particularly for C in the *nix environment?
I have a copy of "POSIX Programmer's Guide", by Donald Lewine, which I find somewhat useful, but it's 20 years old...
Is there an online source that I could easily check/browse?
Thanks,
Keith Ostertag
If you are programming for Gnu/Linux, you can use pretty much anything that is available in the system header files and be quite safe. The only (theoretical) downside is that some of the functions are GNU specific, meaning that they aren't part of any (other) standard, and therefore may not be available on other unixes (e.g., BSD, Solaris) which conform to the standards.
Read the manual page FEATURE_TEST_MACROS(7), which explains the macro system available under Gnu/Linux which you can use if you want to limit yourself to conforming to a more restrictive standard.
So, typically what you would do in such a scenario is, when you are coding, set GCC to restrict itself to some base standard (for example, the C99 standard with the -std=c99 flag) and GCC won't make available functions from other standards. Then you use the macros to enable functions for whatever additional standards you want to include. For example, if you enable X/Open 7 you'll get pretty much all the functions out there except the Gnu specific ones, and maybe a few BSD or SysV functions here and there.
Speaking practically though: keep in mind that, these days, some of the other unixes have implemented the Gnu specific functions and headers anyway. (For example, OpenBSD has provides a getopt.h header and getopt_long() function even though these are Gnu extensions.) They are trying to be compatible with Gnu/Linux because it has become so popular and they want to be able to run all our application source code. So you'll want to ask yourself it is worth the inconveniences to code for a more restrictive standard.
Thank you, hydraMax - that is very useful info, I appreciate it. I had never before heard of the feature_test_macros and will study the man pages more.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.