[Last modified April 16, 1994 by scs.]

This article is a table of contents for the comp.lang.c frequently-asked questions (FAQ) list, listing the questions which the abridged and full versions of the FAQ list answer. (Both lists answer all questions; the wordings of the questions in this article are taken from the abridged list.)

If you have only just come across this article, you will naturally be wondering where the lists which it indexes can be found.  The unabridged version is normally posted on the first of each month, and the unabridged version twice per month, both with Expires: lines which should keep them around all month.  They can also be found in the newsgroups comp.answers and news.answers .  Several sites archive news.answers postings and other FAQ lists, including comp.lang.c's: two sites are rtfm.mit.edu (directories pub/usenet/news.answers/C-faq/ and pub/usenet/comp.lang.c/ ) and ftp.uu.net (directory usenet/news.answers/C-faq/ ) The archie server should help you find others; query it for "prog C-faq". See the meta-FAQ list in news.answers for more information.

Frequently Asked Questions in comp.lang.c
Table Of Contents

1. Null Pointers

  1. What is this infamous null pointer, anyway?
  2. How do I "get" a null pointer in my programs?
  3. What is NULL and how is it #defined?
  4. How should NULL be #defined on a machine which uses a nonzero bit pattern as the internal representation of a null pointer?
  5. If NULL were defined as "((char *)0)," wouldn't that make function calls which pass an uncast NULL work?
  6. I use the preprocessor macro "#define Nullptr(type) (type *)0" to help me build null pointers of the correct type.
  7. Is the abbreviated pointer comparison "if(p)" to test for non-null pointers valid? What if the internal representation for null pointers is nonzero?
  8. If "NULL" and "0" are equivalent, which should I use?
  9. But wouldn't it be better to use NULL (rather than 0) in case the value of NULL changes, perhaps on a machine with nonzero null pointers?
  10. I'm confused. NULL is guaranteed to be 0, but the null pointer is not?
  11. Why is there so much confusion surrounding null pointers? Why do these questions come up so often?
  12. I'm still confused. I just can't understand all this null pointer stuff.
  13. Given all the confusion surrounding null pointers, wouldn't it be easier simply to require them to be represented internally by zeroes?
  14. Seriously, have any actual machines really used nonzero null pointers?
  15. What does a run-time "null pointer assignment" error mean?

2. Arrays and Pointers

  1. I had the definition char a[6] in one source file, and in another I declared  extern char *a. Why didn't it work?
  2. But I heard that char a[] was identical to char *a.
  3. So what is meant by the "equivalence of pointers and arrays" in C?
  4. Why are array and pointer declarations interchangeable as function formal parameters?
  5. How can an array be an lvalue, if you can't assign to it?
  6. Why doesn't sizeof properly report the size of an array which is a parameter to a function?
  7. Someone explained to me that arrays were really just constant pointers.
  8. What is the real difference between arrays and pointers?
  9. I came across some "joke" code containing the "expression" 5["abcdef"] How can this be legal C?
  10. My compiler complained when I passed a two-dimensional array to a routine expecting a pointer to a pointer.
  11. How do I write functions which accept 2-dimensional arrays when the "width" is not known at compile time?
  12. How do I declare a pointer to an array?
  13. What's the difference between array and &array?
  14. How can I dynamically allocate a multidimensional array?
  15. How can I use statically- and dynamically-allocated multidimensional arrays interchangeably when passing them to functions?
  16. Can I simulate a non-0-based array with a pointer?
  17. I passed a pointer to a function which initialized it, but the pointer in the caller was unchanged.
  18. I have a char * pointer that happens to point to some ints, and I want to step it over them. Why doesn't "((int *)p)++;" work?
  19. Can I use a void ** pointer to pass a generic pointer to a function by reference?

3. Memory Allocation

  1. Why doesn't the code "char *answer; gets(answer);" work?
  2. I can't get strcat to work. I tried "char *s1 = "Hello, ", *s2 = "world!", *s3 = strcat(s1, s2);" but I got strange results.
  3. But the man page for strcat says that it takes two char *'s as arguments.  How am I supposed to know to allocate things?
  4. I have a function that is supposed to return a string, but when it returns to its caller, the returned string is garbage.
  5. Why does some code carefully cast the values returned by malloc to the pointer type being allocated?
  6. You can't use dynamically-allocated memory after you free it, can you?
  7. How does free() know how many bytes to free?
  8. So can I query the malloc package to find out how big an allocated block is?
  9. When I free a dynamically-allocated structure containing pointers, do I have to free each subsidiary pointer first?
  10. Why doesn't my program's memory usage go down when I free memory?
  11. Must I free allocated memory before the program exits?
  12. Is it legal to pass a null pointer as the first argument to realloc()?
  13. Is it safe to use calloc's zero-fill guarantee for pointer and floating-point values?
  14. What is alloca and why is its use discouraged?

4. Expressions

  1. Why doesn't the code "a[i] = i++;" work?
  2. Under my compiler, the code "int i = 7; printf("%d\n", i++ * i++);" prints 49. Regardless of the order of evaluation, shouldn't it print 56?
  3. How could the code "int i = 2; i = i++;" ever give 4?
  4. I just tried some allegedly-undefined code on an ANSI-conforming compiler, and got the results I expected.
  5. Don't precedence and parentheses dictate order of evaluation?
  6. But what about the &&, ||, and comma operators?
  7. If I'm not using the value of the expression, should I use i++ or ++i to increment a variable?
  8. Why doesn't the code "int a = 1000, b = 1000; long int c = a * b;" work?


  1. What is the "ANSI C Standard?"
  2. How can I get a copy of the Standard?
  3. Does anyone have a tool for converting old-style C programs to ANSI C, or for automatically generating prototypes?
  4. How do I keep the ANSI "stringizing" preprocessing operator from stringizing the macro's name rather than its value?
  5. Why can't I use const values in initializers and array dimensions?
  6. What's the difference between "char const *p" and "char * const p"?
  7. Why can't I pass a char ** to a function which expects a const char **?
  8. My ANSI compiler complains about a mismatch when it sees

    extern int func(float);

    int func(x)
    float x;

  9. Can you mix old-style and new-style function syntax?
  10. Why does the declaration "extern f(struct x {int s;} *p);" give me a warning message?
  11. I'm getting strange syntax errors inside code which I've #ifdeffed out.
  12. Can I declare main as void, to shut off these annoying "main returns no value" messages?
  13. Is exit(status) truly equivalent to returning status from main?
  14. Why does the ANSI Standard not guarantee more than six monocase characters of external identifier significance?
  15. What is the difference between memcpy and memmove?
  16. My compiler is rejecting the simplest possible test programs, with all kinds of syntax errors.
  17. Why are some ANSI/ISO Standard library routines showing up as undefined, even though I've got an ANSI compiler?
  18. Why won't frobozz-cc, which claims to be ANSI compliant, accept this code?
  19. Why can't I perform arithmetic on a void * pointer?
  20. Is char a[3] = "abc"; legal?
  21. What are #pragmas and what are they good for?
  22. What does "#pragma once" mean?
  23. What's the difference between implementation-defined, unspecified, and undefined behavior?

6. C Preprocessor

  1. How can I write a generic macro to swap two values?
  2. I have some old code that tries to construct identifiers with a macro like "#define Paste(a, b) a/**/b ", but it doesn't work any more.
  3. What's the best way to write a multi-statement cpp macro?
  4. Is it acceptable for one header file to #include another?
  5. Does the sizeof operator work in preprocessor #if directives?
  6. How can I use a preprocessor #if expression to detect endianness?
  7. I've got this tricky processing I want to do at compile time and I can't figure out a way to get cpp to do it.
  8. How can I preprocess some code to remove selected conditional compilations, without preprocessing everything?
  9. How can I list all of the pre#defined identifiers?
  10. How can I write a cpp macro which takes a variable number of arguments?

7. Variable-Length Argument Lists

  1. How can I write a function that takes a variable number of arguments?
  2. How can I write a function that takes a format string and a variable number of arguments, like printf, and passes them to printf to do most of the work?
  3. How can I discover how many arguments a function was actually called with?
  4. I can't get the va_arg macro to pull in an argument of type pointer-to-function.
  5. How can I write a function which takes a variable number of arguments and passes them to some other function (which takes a variable number of arguments)?
  6. How can I call a function with an argument list built up at run time?

8. Boolean Expressions and Variables

  1. What is the right type to use for boolean values in C? Why isn't it a standard type? Should #defines or enums be used for the true and false values?
  2. What if a built-in boolean or relational operator "returns" something other than 1?

9. Structs, Enums, and Unions

  1. What is the difference between an enum and a series of preprocessor #defines?
  2. I heard that structures could be assigned to variables and passed to and from functions, but K&R I says not.
  3. How does struct passing and returning work?
  4. I have a program which works correctly, but dumps core after it finishes. Why?
  5. Why can't you compare structs?
  6. How can I read/write structs from/to data files?
  7. I came across some code that declared a structure with the last member an array of one element, and then did some tricky allocation to make the array act like it had several elements.  Is this legal and/or portable?
  8. How can I determine the byte offset of a field within a structure?
  9. How can I access structure fields by name at run time?
  10. Why does sizeof report a larger size than I expect for a structure type, as if there was padding at the end?
  11. How can I turn off structure padding?
  12. Can I initialize unions?
  13. Can I pass constant values to routines which accept struct arguments?

10. Declarations

  1. How do you decide which integer type to use?
  2. What should the 64-bit type on new, 64-bit machines be?
  3. I can't seem to define a linked list node which contains a pointer to itself.
  4. How do I declare an array of N pointers to functions returning pointers to functions returning pointers to characters?
  5. How can I declare a function that returns a pointer to a function of its own type?
  6. My compiler is complaining about an invalid redeclaration of a function, but I only define it once and call it once.
  7. What's the best way to declare and define global variables?
  8. What does extern mean in a function declaration?
  9. How do I initialize a pointer to a function?
  10. I've seen different methods used for calling through pointers to functions.
  11. What's the auto keyword good for?

11. Stdio

  1. What's wrong with the code "char c; while((c = getchar()) != EOF)..." ?
  2. How can I print a '%' character with printf?
  3. Why doesn't the code scanf("%d", i); work?
  4. Why doesn't the code double d; scanf("%f", &d); work?
  5. Why won't the code "while(!feof(infp)) { fgets(buf, MAXLINE, infp); fputs(buf, outfp); }" work?
  6. Why does everyone say not to use gets()?
  7. Why does errno contain ENOTTY after a call to printf?
  8. My program's prompts and intermediate output don't always show up on the screen, especially when I pipe the output through another program.
  9. When I read from the keyboard with scanf, it seems to hang until I type one extra line of input.
  10. I'm trying to update a file in place, by using fopen mode "r+", but it's not working.
  11. How can I read one character at a time, without waiting for the RETURN key?
  12. Will fflush(stdin) flush unread characters from the standard input stream?
  13. How can I redirect stdin or stdout from within a program?
  14. Once I've used freopen, how can I get the original stdout back?
  15. How can I recover the file name given an open file descriptor?

12. Library Subroutines

  1. Why does strncpy not always write a '\0'?
  2. I'm trying to sort an array of strings with qsort, using strcmp as the comparison function, but it's not working.
  3. Now I'm trying to sort an array of structures with qsort. My comparison routine takes pointers to structures, but the compiler complains that the function is of the wrong type for qsort. How can I cast the function pointer to shut off the warning?
  4. How can I convert numbers to strings?
  5. How can I get the time of day in a C program?
  6. How can I convert a struct tm or a string into a time_t?
  7. How can I perform calendar manipulations?
  8. I need a random number generator.
  9. How can I get random integers in a certain range?
  10. Each time I run my program, I get the same sequence of numbers back from rand().
  11. I need a random true/false value, so I'm taking rand() % 2, but it's just alternating 0, 1, 0, 1, 0...
  12. I'm trying to port this old program. Why do I get "undefined external" errors for some library routines?
  13. I get errors due to library routines being undefined even though I #include the right header files.
  14. I'm still getting errors due to library routines being undefined, even though I'm requesting the right libraries.
  15. I need some code to do regular expression matching.
  16. How can I split up a string into whitespace-separated arguments?

13. Lint

  1. I just typed in this program, and it's acting strangely. Can you see anything wrong with it?
  2. How can I shut off the "warning: possible pointer alignment problem" message lint gives me for each call to malloc?
  3. Where can I get an ANSI-compatible lint?

14. Style

  1. Is the code "if(!strcmp(s1, s2))" good style?
  2. What's the best style for code layout in C?
  3. Where can I get the "Indian Hill Style Guide" and other coding standards?

15. Floating Point

  1. My floating-point calculations are acting strangely and giving me different answers on different machines.
  2. I keep getting "undefined: _sin" compilation errors.
  3. Where is C's exponentiation operator?
  4. How do I round numbers?
  5. How do I test for IEEE NaN and other special values?
  6. I'm having trouble with a Turbo C program which crashes and says something like "floating point formats not linked."

16. System Dependencies

  1. How can I read a single character from the keyboard without waiting for a newline?
  2. How can I find out if there are characters available for reading (and if so, how many)? Alternatively, how can I do a read that will not block if there are no characters available?
  3. How can I clear the screen?
  4. How do I read the mouse?
  5. How can my program discover the complete pathname to the executable file from which it was invoked?
  6. How can a process change an environment variable in its caller?
  7. How can I check whether a file exists?
  8. How can I find out the size of a file, prior to reading it in?
  9. How can a file be shortened in-place without completely clearing or rewriting it?
  10. How can I implement a delay, or time a user's response, with sub-second resolution?
  11. How can I read in an object file and jump to routines in it?
  12. How can I invoke an operating system command from within a program?
  13. How can I invoke an operating system command and trap its output?
  14. How can I read a directory in a C program?
  15. How can I do serial ("comm") port I/O?

17. Miscellaneous

  1. What can I safely assume about the initial values of variables which are not explicitly initialized?
  2. What's wrong with
    f() { char a[] = "Hello, world!"; }
  3. How can I write data files which can be read on other machines with different data formats?
  4. How can I insert or delete a line from the middle of a file?
  5. How can I return several values from a function?
  6. How can I call a function, given its name as a string?
  7. I seem to be missing the system header file <sgtty.h>. Can someone send me a copy?
  8. How can I call FORTRAN (C++, BASIC, Pascal, Ada, LISP) functions from C?
  9. Does anyone know of a program for converting Pascal or FORTRAN to C?
  10. Can I use a C++ compiler to compile C code?
  11. I'm looking for C development tools (cross-reference generators, code beautifiers, etc.).
  12. Where can I get copies of all these public-domain programs?
  13. When will the next Obfuscated C Code Contest be held? How can I get a copy of the previous winning entries?
  14. Why don't C comments nest? Are they legal inside quoted strings?
  15. How can I get the ASCII value corresponding to a character?
  16. How can I implement sets and/or arrays of bits?
  17. What is the most efficient way to count the number of bits which are set in a value?
  18. How can I make this code more efficient?
  19. Are pointers really faster than arrays? How much do function calls slow things down?
  20. Why does the code "char *p = "Hello, world!"; p[0] = tolower(p[0]);" crash?
  21. This program crashes before it even runs!
  22. What does "Segmentation violation" mean?
  23. My program is crashing, apparently somewhere down inside malloc.
  24. Does anyone have a C compiler test suite I can use?
  25. Where can I get a YACC grammar for C?
  26. I need code to parse and evaluate expressions.
  27. I need to compare two strings for close, but not necessarily exact, equality.
  28. How can I find the day of the week given the date?
  29. Will 2000 be a leap year?
  30. How do you pronounce "char"?
  31. What's a good book for learning C?
  32. Are there any C tutorials on the net?
  33. Where can I get extra copies of this list?

Steve Summit

This article is Copyright 1994 by Steve Summit.
It may be freely redistributed so long as the author's name, and this notice, are retained.