Relearning C, possible explanation/solutions to some problem statements any developer might encounter

Recently I got accepted to Linux Kernel Mentorship Program - Summer 2022, under LFX Mentorship, so for the purpose of being a competent participant in the program, I took relearning(read revising) C. One of my past learning from various open-source programs I have participated in is that any participant should be as adept as possible with its tool(in my case tools being C, Make, GDB, etc.). So, moving ahead here were some of the learnings I got upon reading this book(Learn C The Hard Way) and watching accompanying videos:

PS: I tried resolving as many warnings as possible because my target was not just to compile and run the code, but to understand how it can be improved.

  • If while using printf in ex1 you get warnings like:
    ex1.c:8:9: warning: multi-character character constant [-Wmultichar]
          printf('You are %d distance away', distance);
                 ^
    ex1.c:8:9: warning: character constant too long for its type
    ex1.c:8:9: warning: incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *' [-Wint-conversion]
          printf('You are %d distance away', distance);
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~
    
    then it's advisable to replace ' quotes in printf with ". Single quotes are used when dealing with a character, whereas double quotes are used when dealing with string
  • While building ex2 I encountered .dSYM file, upon googling I found this helpful: https://stackoverflow.com/a/22460268
  • List of Escape Sequences: https://gist.github.com/AALEKH/162d6c6414d2b85c5e95c52836bb291e and list of format specifiers in C: https://www.freecodecamp.org/news/format-specifiers-in-c/
  • For the purpose of debugging use: GDB/LLDB, Valgrind, AddressSanitizer(alternative to Valgrind), Splint(Lint: Static code analyzer)
  • For a similar value of float and double always remember float(number) <= double(number)
  • In C 0 means False whereas non zero numbers mean True
  • If-Else is used for boolean comparison whereas Switch-Case(a form of jump table) is used for integer matching
  • In the main function, a return that's not 0 indicate to the OS that the program has encountered an error.
  • Don't forget to add the break statement with case in switch-case
  • C does not make a differentiation between an array of bytes or strings
  • Occurrence of the null byte(\0) denotes the end of the string, if it's not present the print command won't be able to judge where the string terminates and print out garbage value along with the string characters.
  • Watch out for off-by-one error
  • Forward declaration solves the problem of the deadly embrace(kind of like the chicken-egg problem), we don't have to be worried about calling/declaring order.
  • assert check if the condition provided to it inside the parameters is correct, if not it ends the program abruptly.
  • In C string is array of char's
  • We can add increment, and decrement operations on pointers.
  • Pointer gives us raw memory block access and let's us perform operation on it.
  • For printing out pointer address we use %p -Some of the Pointer Lexicon picked directly from Learn C Hard Way book are:

    • type *ptr: A pointer of "type" named "ptr"
    • *ptr The value of whatever "ptr" is pointed at
    • *(ptr + i) The value of (whatever ptr is pointed at plus i)
    • &thing The address of thing
    • type *ptr = &thing A pointer of type named "ptr" set to address of "thing"
    • ptr++ Increment where "ptr" points
    • struct ABC *ptr = malloc(sizeof(struct ABC)) refer to element withing it as ptr->element
  • Stack and Heap Memory as learned from this video:

    • Stack Memory:
      1. The memory space stores static variables
      2. When allotted stack memory gets filled, stack overflow occurs
      3. Data saved on the stack can only be accessed by the owner thread making it safer.
      4. Stack frame access is easier, and since Stack memory is stored in cache it is faster
    • Heap Memory:
      1. The memory spaces stores dynamic memory.
      2. When allotted heap memory get filled, heap overflow occurs
      3. Heap memory is not so safe as data stored in Heap memory is visible to all threads
      4. Heap frame access is difficult, and slower as it is scattered all across memory
  • The topic of preprocessor-directives macros was somewhat an enigma for me, I found this video explanation on it very helpful.
  • Static variable is created to maintain the value of that variable, even after it is out of function scope
  • Never use get instead use fget for file standard input operation