comp.lang.c FAQ list Table of Contents 
Author Message
 comp.lang.c FAQ list Table of Contents

[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 abridged 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 (directories
pub/usenet/news.answers/C-faq/ and pub/usenet/comp.lang.c/ ) and (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.

Section 1. Null Pointers
1.1:    What is this infamous null pointer, anyway?
1.2:    How do I "get" a null pointer in my programs?
1.3:    What is NULL and how is it #defined?
1.4:    How should NULL be #defined on a machine which uses a nonzero
        bit pattern as the internal representation of a null pointer?
1.5:    If NULL were defined as "((char *)0)," wouldn't that make
        function calls which pass an uncast NULL work?
1.6:    I use the preprocessor macro "#define Nullptr(type) (type *)0"
        to help me build null pointers of the correct type.
1.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?
1.8:    If "NULL" and "0" are equivalent, which should I use?
1.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?
1.10:   I'm confused.  NULL is guaranteed to be 0, but the null pointer
        is not?
1.11:   Why is there so much confusion surrounding null pointers?  Why
        do these questions come up so often?
1.12:   I'm still confused.  I just can't understand all this null
        pointer stuff.
1.13:   Given all the confusion surrounding null pointers, wouldn't it
        be easier simply to require them to be represented internally by
1.14:   Seriously, have any actual machines really used nonzero null
1.15:   What does a run-time "null pointer assignment" error mean?

Section 2. Arrays and Pointers
2.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.2:    But I heard that char a[] was identical to char *a.
2.3:    So what is meant by the "equivalence of pointers and arrays" in
2.4:    Why are array and pointer declarations interchangeable as
        function formal parameters?
2.5:    How can an array be an lvalue, if you can't assign to it?
2.6:    Why doesn't sizeof properly report the size of an array which is
        a parameter to a function?
2.7:    Someone explained to me that arrays were really just constant
2.8:    What is the real difference between arrays and pointers?
2.9:    I came across some "joke" code containing the "expression"
        5["abcdef"] .  How can this be legal C?
2.10:   My compiler complained when I passed a two-dimensional array to
        a routine expecting a pointer to a pointer.
2.11:   How do I write functions which accept 2-dimensional arrays when
        the "width" is not known at compile time?
2.12:   How do I declare a pointer to an array?
2.13:   What's the difference between array and &array?
2.14:   How can I dynamically allocate a multidimensional array?
2.15:   How can I use statically- and dynamically-allocated
        multidimensional arrays interchangeably when passing them to
2.16:   Can I simulate a non-0-based array with a pointer?
2.17:   I passed a pointer to a function which initialized it, but the
        pointer in the caller was unchanged.
2.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?
2.19:   Can I use a void ** pointer to pass a generic pointer to a
        function by reference?

Section 3. Memory Allocation
3.1:    Why doesn't the code "char *answer; gets(answer);" work?
3.2:    I can't get strcat to work.  I tried "char *s1 = "Hello, ",
        *s2 = "world!", *s3 = strcat(s1, s2);" but I got strange
3.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?
3.4:    I have a function that is supposed to return a string, but when
        it returns to its caller, the returned string is garbage.
3.5:    Why does some code carefully cast the values returned by malloc
        to the pointer type being allocated?
3.6:    You can't use dynamically-allocated memory after you free it,
        can you?
3.7:    How does free() know how many bytes to free?
3.8:    So can I query the malloc package to find out how big an
        allocated block is?
3.9:    When I free a dynamically-allocated structure containing
        pointers, do I have to free each subsidiary pointer first?
3.10:   Why doesn't my program's memory usage go down when I free
3.11:   Must I free allocated memory before the program exits?
3.12:   Is it legal to pass a null pointer as the first argument to
3.13:   Is it safe to use calloc's zero-fill guarantee for pointer and
        floating-point values?
3.14:   What is alloca and why is its use discouraged?

Section 4. Expressions
4.1:    Why doesn't the code "a[i] = i++;" work?
4.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?
4.3:    How could the code "int i = 2; i = i++;" ever give 4?
4.4:    I just tried some allegedly-undefined code on an ANSI-conforming
        compiler, and got the results I expected.
4.5:    Don't precedence and parentheses dictate order of evaluation?
4.6:    But what about the &&, ||, and comma operators?
4.7:    If I'm not using the value of the expression, should I use i++
        or ++i to increment a variable?
4.8:    Why doesn't the code "int a = 1000, b = 1000;
        long int c = a * b;" work?

Section 5. ANSI C
5.1:    What is the "ANSI C Standard?"
5.2:    How can I get a copy of the Standard?
5.3:    Does anyone have a tool for converting old-style C programs to
        ANSI C, or for automatically generating prototypes?
5.4:    How do I keep the ANSI "stringizing" preprocessing operator from
        stringizing the macro's name rather than its value?
5.5:    Why can't I use const values in initializers and array
5.6:    What's the difference between "char const *p" and
        "char * const p"?
5.7:    Why can't I pass a char ** to a function which expects a
        const char **?
5.8:    My ANSI compiler complains about a mismatch when it sees
                extern int func(float);
                int func(x)
                float x;
5.9:    Can you mix old-style and new-style function syntax?
5.10:   Why does the declaration "extern f(struct x {int s;} *p);" give
        me a warning message?
5.11:   I'm getting strange syntax errors inside code which I've
        #ifdeffed out.
5.12:   Can I declare main as void, to shut off these annoying "main
        returns no value" messages?
5.13:   Is exit(status) truly equivalent to returning status from main?
5.14:   Why does the ANSI Standard not guarantee more than six monocase
        characters of external identifier significance?
5.15:   What is the difference between memcpy and memmove?
5.16:   My compiler is rejecting the simplest possible test programs,
        with all kinds of syntax errors.
5.17:   Why are some ANSI/ISO Standard library routines showing up as
        undefined, even though I've got an ANSI compiler?
5.18:   Why won't frobozz-cc, which claims to be ANSI compliant, accept
        this code?
5.19:   Why can't I perform arithmetic on a void * pointer?
5.20:   Is char a[3] = "abc"; legal?
5.21:   What are #pragmas and what are they good for?
5.22:   What does "#pragma once" mean?
5.23:   What's the difference between implementation-defined,
        unspecified, and undefined behavior?

Section 6. C Preprocessor
6.1:    How can I write a generic macro to swap two values?
6.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.
6.3:    What's the best way to write a multi-statement cpp macro?
6.4:    Is it acceptable for one header file to #include another?
6.5:    Does the sizeof operator work in preprocessor #if directives?
6.6:    How can I use a preprocessor #if expression to detect
6.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.
6.8:    How can I preprocess some code to remove selected conditional
        compilations, without preprocessing everything?
6.9:    How can I list all of the pre#defined identifiers?
6.10:   How can I write a cpp macro which takes a variable number of

Section 7. Variable-Length Argument Lists
7.1:    How can I write a function that takes a variable number of
7.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?
7.3:    How can I discover how many arguments a function was actually
        called with?
7.4:    I can't get the va_arg macro to pull in an argument of type
7.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)?
7.6:    How can I call a function with an argument list built up at run

Section 8. Boolean Expressions and Variables
8.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?
8.2:    What if a built-in boolean or relational operator "returns"
        something other than 1?

Section 9. Structs, Enums, and Unions
9.1:    What is the difference between an enum and a series of
        preprocessor #defines?
9.2:    I heard that structures could be assigned to variables and
        passed to and from functions, but K&R I says not.
9.3:    How does struct passing and returning work?
9.4:    I have a program which works correctly, but dumps core after it
        finishes.  Why?
9.5:    Why can't you compare structs?
9.6:    How can I read/write structs from/to data files?
9.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?
9.8:    How can I determine the byte offset of a field within a
9.9:    How can I access structure fields by name at run time?
9.10:   Why does sizeof report a larger size than I expect for a
        structure type, as if there was padding at the end?
9.11:   How can I turn off structure padding?
9.12:   Can I initialize unions?
9.13:   Can I pass constant values to routines which accept struct

Section 10. Declarations
10.1:   How do you decide which integer type to use?
10.2:   What should the 64-bit type on new, 64-bit machines be?
10.3:   I can't seem to define a linked list node which contains a
        pointer to itself.
10.4:   How do I declare an array of N pointers to functions returning
        pointers to functions returning pointers to characters?
10.5:   How can I declare a function that returns a pointer to a
        function of its own type?
10.6:   My compiler is complaining about an invalid redeclaration of a
        function, but I only define it once and call it once.
10.7:   What's the best way to declare and define global variables?
10.8:   What does extern mean in a function declaration?
10.9:   How do I initialize a pointer to a function?
10.10:  I've seen different methods used for calling through pointers to
10.11:  What's the auto keyword good for?

Section 11. Stdio
11.1:   What's wrong with the code "char c; while((c = getchar()) != EOF)..." ?
11.2:   How can I print a '%' character with printf?
11.3:   Why doesn't the code scanf("%d", i); work?
11.4:   Why doesn't the code double d; scanf("%f", &d); work?
11.5:   Why won't the code "while(!feof(infp)) {
        fgets(buf, MAXLINE, infp); fputs(buf, outfp); }" work?
11.6:   Why does everyone say not to use gets()?
11.7:   Why does errno contain ENOTTY after a call to printf?
11.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.
11.9:   When I read from the keyboard with scanf, it seems to hang until
        I type one extra line of input.
11.10:  I'm trying to update a file in place, by using fopen mode "r+",
        but it's not working.
11.11:  How can I read one character at a time, without waiting for the
        RETURN key?
11.12:  Will fflush(stdin) flush unread characters from the standard
        input stream?
11.13:  How can I redirect stdin or stdout from within a program?
11.14:  Once I've used freopen, how can I get the original stdout back?
11.15:  How can I recover the file name given an open file descriptor?

Section 12. Library Subroutines
12.1:   Why does strncpy not always write a '\0'?
12.2:   I'm trying to sort an array of strings with qsort, using strcmp
        as the comparison function, but it's not working.
12.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
12.4:   How can I convert numbers to strings?
12.5:   How can I get the time of day in a C program?
12.6:   How can I convert a struct tm or a string into a time_t?
12.7:   How can I perform calendar manipulations?
12.8:   I need a random number generator.
12.9:   How can I get random integers in a certain range?
12.10:  Each time I run my program, I get the same sequence of numbers
        back from rand().
12.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.12:  I'm trying to port this old program.  Why do I get "undefined
        external" errors for some library routines?
12.13:  I get errors due to library routines being undefined even though
        I #include the right header files.
12.14:  I'm still getting errors due to library routines being
        undefined, even though I'm requesting the right libraries.
12.15:  I need some code to do regular expression matching.
12.16:  How can I split up a string into whitespace-separated arguments?

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

Section 14. Style
14.1:   Is the code "if(!strcmp(s1, s2))" good style?
14.2:   What's the best style for code layout in C?
14.3:   Where can I get the "Indian Hill Style Guide" and other coding

Section 15. Floating Point
15.1:   My floating-point calculations are acting strangely and giving
        me different answers on different machines.
15.2:   I keep getting "undefined: _sin" compilation errors.
15.3:   Where is C's exponentiation operator?
15.4:   How do I round numbers?
15.5:   How do I test for IEEE NaN and other special values?
15.6:   I'm having trouble with a Turbo C program which crashes and says
        something like "floating point formats not linked."

Section 16. System Dependencies
16.1:   How can I read a single character from the keyboard without
        waiting for a newline?
16.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?
16.3:   How can I clear the screen?
16.4:   How do I read the mouse?
16.5:   How can my program discover the complete pathname to the
        executable file from which it was invoked?
16.6:   How can a process change an environment variable in its caller?
16.7:   How can I check whether a file exists?
16.8:   How can I find out the size of a file, prior to reading it in?
16.9:   How can a file be shortened in-place without completely clearing
        or rewriting it?
16.10:  How can I implement a delay, or time a user's response, with
        sub-second resolution?
16.11:  How can I read in an object file and jump to routines in it?
16.12:  How can I invoke an operating system command from within a
16.13:  How can I invoke an operating system command and trap its
16.14:  How can I read a directory in a C program?
16.15:  How can I do serial ("comm") port I/O?

Section 17. Miscellaneous
17.1:   What can I safely assume about the initial values of variables
        which are not explicitly initialized?
17.2:   What's wrong with
                f() { char a[] = "Hello, world!"; }
17.3:   How can I write data files which can be read on other machines
        with different data formats?
17.4:   How can I insert or delete a line in the middle of a file?
17.5:   How can I return several values from a function?
17.6:   How can I call a function, given its name as a string?
17.7:   I seem to be missing the system header file <sgtty.h>.  Can
        someone send me a copy?
17.8:   How can I call fortran (C++, BASIC, Pascal, Ada, LISP) functions
        from C?
17.9:   Does anyone know of a program for converting Pascal or FORTRAN
        to C?
17.10:  Can I use a C++ compiler to compile C code?
17.11:  I'm looking for C development tools (cross-reference generators,
        code beautifiers, etc.).
17.12:  Where can I get copies of all these public-domain programs?
17.13:  When will the next Obfuscated C Code Contest be held?  How can I
        get a copy of the previous winning entries?
17.14:  Why don't C comments nest?  Are they legal inside quoted
17.15:  How can I get the ASCII value corresponding to a character?
17.16:  How can I implement sets and/or arrays of bits?
17.17:  What is the most efficient way to count the number of bits which
        are set in a value?
17.18:  How can I make this code more efficient?
17.19:  Are pointers really faster than arrays?  How much do function
        calls slow things down?
17.20:  Why does the code "char *p = "Hello, world!";
        p[0] = tolower(p[0]);" crash?
17.21:  This program crashes before it even runs!
17.22:  What does "Segmentation violation" mean?
17.23:  My program is crashing, apparently somewhere down inside malloc.
17.24:  Does anyone have a C compiler test suite I can use?
17.25:  Where can I get a YACC grammar for C?
17.26:  I need code to parse and evaluate expressions.
17.27:  I need to compare two strings for close, but not necessarily
        exact, equality.
17.28:  How can I find the day of the week given the date?
17.29:  Will 2000 be a leap year?
17.30:  How do you pronounce "char"?
17.31:  What's a good book for learning C?
17.32:  Are there any C tutorials on the net?
17.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.

Mon, 19 May 1997 19:01:18 GMT  
 [ 1 post ] 

 Relevant Pages 

1. comp.lang.c FAQ list Table of Contents

2. comp.lang.c FAQ list Table of Contents

3. comp.lang.c FAQ list Table of Contents

4. comp.lang.c FAQ list Table of Contents

5. comp.lang.c FAQ list Table of Contents

6. comp.lang.c FAQ list Table of Contents

7. comp.lang.c FAQ list Table of Contents

8. comp.lang.c FAQ list Table of Contents

9. comp.lang.c FAQ list Table of Contents

10. comp.lang.c FAQ list Table of Contents

11. comp.lang.c FAQ list Table of Contents

12. comp.lang.c FAQ list Table of Contents


Powered by phpBB® Forum Software