C NOTES: QUICK REFERENCE

  1. Books
  2. BASICS of LANGUAGE
  3. SPECIAL TOPICS
  4. FAQs
GO TO BOTTOM


  1. BOOKS:


    BASICS OF LANGUAGE


  2. My First Program
  3. Comment Lines
  4. Main(), argc, argv, envp and Command line arguments
  5. Constants
  6. Boolean Tests and Expression
    • Note that there is no built-in type called "bool".
    • The "if(c){...}" paradigm in C can be confusing!
    • Q: When is {...} executed?
      A: Iff c is non-zero! WHY? Traditionally, TRUE=1 and FALSE=0. Thus this convention generalizes to TRUE !=0.
    • Q: suppose c is a return code/status from a subroutine. Is c=0 considered "normal" or "error"?
      A: it is context dependent.
    • E.g., 0 as error: if you open a file, a return code of 0 seems to be error (otherwise you get the file descriptor). If you write to a file, a return code of 0 seems to be error as well (otherwise you get the number of bytes written).
    • E.g., 0 as normal: when processes terminate, they usually call exit(status) where status=0 means normal termination. So this convention is opposite of the previous example.


    SPECIAL TOPICS


  7. Macros
  8. Runtime Libraries
  9. Shared Libraries
  10. Debugging Tools and Hints: objdump, ldd, etc If you could not compile your program or have mysterious errors, try one of the following options:
    1. To look inside compiled libraries (*.a files), do for example:
      	
      		> objdump -t /lib/libc.a | grep "fork"
      	
      This will print all the library routines that has name containing "fork".
      [Objdump is available in Solaris and Cygwin]
    2. What runtime libraries do you need for an executable file "foo"? Type
      		> ldd foo
      	
      and this information will be displayed.
    3. What is gcc actually doing? Try the verbose modes (-v) to see:
      		> gcc -v test.c
      	
    4. Another version of this, useful for seeing the arguments as discrete units, and which does not actually compile, is
      		> gcc -### test.c
      	
      where "###" is literally what you type! Each argument is written in a quoted string.
    5. Where would gcc find this library? E.g., the standard c library is called "libc.a". But where is the path? To find out, do:
      		> gcc -print-file-name=libc.a
      	
      On my cygwin, it prints:
      		/usr/lib/gcc-lib/i686-pc-cygwin/3.3.3/../../../libc.a
      	
      which is really /usr/lib/libc.a.
    6. Which directories would gcc search? Try this:
      		> gcc -print-search-dirs   2>&1 | tee x
      	
      The following output is stored in file "x", which you can examine:
      	install: /usr/lib/gcc-lib/i686-pc-cygwin/3.3.3/
      	programs: =/usr/lib/gcc-lib/i686-pc-cygwin/3.3.3/
      		:/usr/lib/gcc-lib/i686-pc-cygwin/3.3.3/
      		:/usr/lib/gcc-lib/i686-pc-cygwin/:...
      	libraries: =/usr/lib/gcc-lib/i686-pc-cygwin/3.3.3/:
      		/usr/lib/gcc/i686-pc-cygwin/3.3.3/:...
      	
    7. If you want to know the path for a particular library, say "libs.a". You type
      		> gcc -print-file-name=libc.a test.c
      	
      and gcc will print the full absolute path name of the library that would be used for linking. It does nothing else! See man gcc for more options.
    8. Check out the environmental variables used by gcc:
      		GCC_EXEC_PREFIX
      		COMPILER_PATH
      		LIBRARY_PATH
      		LANG
      	
    9. If you could not find a standard library like "/usr/libc.a" you could try to give the prefix using "-B" flag:
      		> gcc -print-file-name=libc.a
      
      		/usr/lib/gcc-lib/i686-pc-cygwin/3.3.3/../../../libc.a
      
      		> gcc -B/usr -print-file-name=libc.a
      
      		/lib/libc.a
      	
      	Note that the -B flag changed the search path for "libc.a".
      	

      NOTE: in cygwin, the directories /usr/lib and /lib are identical. E.g., create a dummy file in /usr/lib, and it shows in /lib; delete in one and it is also deleted in the other!

  11. Files
  12. System Calls


    FAQs



    GO TO TOP Please send comments to yap at cs dot nyu dot edu.