C Deep Dive

The C language up close, built and tested with GCC on Linux: syntax, types, operators, pointers, memory, and files.

Type
Topic overview
Updated
Corrected

Contents

Read the overview

Unmarked notes are working notes: public, not yet reviewed.

Here, we will go through all the nitty-gritty details to know about C. No algorithms, just a deep dive into how the language works.

Versions of C

C89 and C99 are two prominent standards for the C programming language, each introducing features and defining the language's behavior.

C23 (ISO/IEC 9899:2024) the current open standard for C, published in October 2024. It includes enhancements such as improved const support, new and updated library functions, and further compiler optimizations.

Running C

Here is the command to run a simple C program:

bash
> gcc hello.c -o hello 
> ./hello

The first line compiles and creates an executable file named hello. The second line runs the program. In Windows, the second line should just be hello.

gcc is the C compiler driver of the GNU Compiler Collection (GCC), a compiler system from the GNU Project that supports several languages. The driver runs the preprocessor, compiler, assembler, and linker in turn; the file named by -o is the linked executable.

You can use this trick to both compile and run your code with one command:

c
gcc zork.c -o zork && ./zork

Note: add -Wall to enable a large set of commonly useful warnings. Despite the name, it does not turn on every warning: the GCC warning options manual lists further warnings enabled by -Wextra and others, such as -Wconversion, that must be requested individually. A reasonable default is gcc -Wall -Wextra zork.c -o zork.

Note: If -o is not provided, then the default name of the compiled file is a.out.

C to assembly

It is possible to have a look at the output of the C compiler using the objdump command line utility.

objdump is primarily a reverse engineering and debugging tool that helps programmers understand how their C source code is translated into machine code. 

For example, when the compiled file is Tester then we can use the following command:

bash
objdump Tester -d

to see the code presented in assembly. The output will display the assembly code with the relevant lines from your hello.c file interspersed, providing detailed insight into the compilation process. 

Here are the flags that can be used with this command:

bash
-f          # display header information for the entire file
-s          # display per-section summary information
-d          # disassemble sections containing code
--source    # (implies -d) show source code, if available, along with disassembly

To do this in a web interface, you can use https://godbolt.org/.

C debugger (gdb)

To load the code into the debugger, use gdb <fileName>. Then, to get the debugger running, use:

bash
start # start debugging
break n # set breakpoint at line n
continue # Continue the execution till the next breakpoint
step # for step by step execution
layout src # to see source code in separate window
layout asm # to see compiled assembly instructions
print <var> # to see the value of the variable
stop # stop debugging
quit # exit gdb

The first thing to know about C is that is is defined to create small, fast solutions. It is used to write code that’s a lot closer to what machines really understand.

Syntax in CExplainer
Operators in CExplainer
Statements in CExplainer
Pointers in CExplainer
Dynamic Memory Allocation in CExplainer
Types in CExplainer
Data Structures in CExplainer
Functions in CExplainer
File handling in CExplainer

Type to search every note and essay.