User Tools

Site Tools


c_language:overview

C Language

Things i should read up on

Underhanded C Contest

uguu – Anime ascii art

IOCCC

8 phases of translation in C

stackoverflow

                       ANSI C translation phases
                       =========================

          +-------------------------------------------------+
          | map physical characters to source character set |
          |     replace line terminators with newlines      |
          |           decode trigraph sequences             |
          +-------------------------------------------------+
                                   |
                                   V
               +---------------------------------------+
               | join lines along trailing backslashes |
               +---------------------------------------+
                                   |
                                   V
     +-------------------------------------------------------------+
     | decompose into preprocessing tokens and whitespace/comments |
     |                      strip comments                         |
     |                      retain newlines                        |
     +-------------------------------------------------------------+        
                                   |
                                   V
          +------------------------------------------------+
          | execute preprocessing directives/invoke macros |
          |              process included files            |
          +------------------------------------------------+
                                   |
                                   V
   +----------------------------------------------------------------+
   | decode escape sequences in character constants/string literals |
   +----------------------------------------------------------------+
                                   |
                                   V
                +--------------------------------------+
                | concatenate adjacent string literals |
                +--------------------------------------+
                                   |
                                   V
              +------------------------------------------+
              | convert preprocessing tokens to C tokens |
              |       analyze and translate tokens       |
              +------------------------------------------+
                                   |
                                   V
                    +-----------------------------+
                    | resolve external references |
                    |        link libraries       |
                    |      build program image    |
                    +-----------------------------+

Right-to-left rule

SO thread

Reddit

To decode the expression char (*(*x())[5])()

         x          : x is a
         x()        : function
        *x()        : returning pointer to
       (*x())[5]    : a 5-element array of 
      *(*x())[5]    : pointer to
     (*(*x())[5])() : function
char (*(*x())[5])() : returning char

Pointer vs Array

They are NOT the same, despite what people say!

// Example of char array vs char pointer
char *str1 = "Foo"; // Pointer. "Foo" resides in TEXT-section
char str2[] = "Bar"; // Array. "Bar" resides in RAM, copied to DATA section during C-startup
 
str1[2] = 'O'; // Illegal, undefined behaviour
str2[2] = 'R'; // This is fine, it alters content of RAM

Const

uint8_t const *foo; // Points at constant. Can't change the value that foo points at.
const uint8_t *foo; // Same as above.
uint8_t *const foo; // Constant pointer. Can't change the address it is holding.

stdint.h, printf and scanf

stdint.h has definitions of uint8_t etc.

inttypes.h has definitions of PRIu8 etc, for use with printf and scanf.

This site lists all available macros and types.

#include <stdint.h>
#include <inttypes.h>
 
...
 
  printf("Printing a uint8_t: %" PRIu8 " woop woop!", var8bit);

Snippets

Debug macro

#define STRINGIFY_(X) #X
#define STRINGIFY(X) STRINGIFY_(X)
#define PING() debug_print_str(__FILE__ " : " STRINGIFY(__LINE__) "\n")
c_language/overview.txt · Last modified: 2022/09/12 00:30 by 127.0.0.1

Except where otherwise noted, content on this wiki is licensed under the following license: CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki