Notes on the C Programming language, many from the "C programming for beginners" course on Udemy.

C Programming For Beginners


  • is two steps: pre-processing and the actual compilation.

  • the compiler translates assembly lang statements into actual machine instructions

  • the output from the compiler is known as object code (.obj or o extension)

  • common command to compile is : cc -c myprog.c or gcc -c myprog.c (the latter is the GNU c compiler.)


  • after creating the object code, it can be linked.

  • linking gets all dependencies in place and builds the executable file. All .o files will be linked.

  • This usually happens at the same time as compiling when you use an IDE.

  • Linux will spit out a myfile.out

Core concepts

The Preprocessor

  • it's fairly unique to C, and is advantageous

  • makes programs easier to read, modify and port.

  • it's part of the compilation process; happens before the program is analyzed.

  • preprocessor commands can be anywhere in your code but are usually at the top.

  • Preprocessor statements are identified by the # sign (a "preprocessor directive".)

  • Preprocessors can be used to build your own library files, constants, macros,

  • can make more powerful programs with conditionals: #ifdef, #endif, #else, #ifndef etc.

The #include statement

  • is a preprocessor directive used in all C programs.

  • include files are usually header files b ecause it is usually included at the head of the document.

  • has a .h extension.

  • using #include "json.h" means look in the current direcotry.

  • using #includ <json.h> means look in the standard system directories.

Header files

  • they define information about some of the functions that are provided by that file.

  • header files usually have function prototypes (names of functions), typedefs, and constants.

  • executable code doesn't (usually) go into a header file.

Format specifiers

  • used for displaying variables as output.

  • they specify the type of the data that you want to display.

  • ex: int sum = 89; printf("The sum is %d\n", sum)

  • for each type, printf expects a certain kind of datatype (%i, %e, %c) etc.

Format specifierDatatype
%dSigned integer
%e / %EScientific notation of floats
%fFloat values
%g / %GSimilar as %e or %E
%hiSigned integer (short)
%huUnsigned Integer (short)
%iUnsigned integer
%l / %ld / %liLong
%LfLong double
%luUnsigned int or unsigned long
%lli / %lldLong / long
%lluUnsigned long long
%oOctal representation
%pPoi nter
%uUnsigned int
%x or %XHexadecimal representation
%nPrints nothing
%%Prints % character

Bitwise Operators

  • AND, &, ~OR~;, tests and set individual bits in an integer variable.

  • These operate on the bits in integer values.

  • less commonly used.

  • Why? - it can store a lot of data in it.

  • You could use a single integer variable to store several characteristics of an "object"

    • you could use one bit, in an integer (which is made of multiple bits) to indicate on/off data.

    • a byte is 8 bits; an integer is usually 4 bytes (and thus, 32 bits)


Constants can be created using #define, const (in c90) or enums.

// Using the preprocessor (note, no use of "=")
#define FOO 0.015

// using  const
const int foo 15


  • there is no standard datatype for strings in C.

  • strings are stored in an array of type char.

  • Remember to add an extra length when declaring the string length - to make room for a null terminator (\0)

# you can put "7" in the array, but you should avoid this. Let the compiler set the size for you.
# bad
char word[7] = {"Hello"}

# better
char word[] = {"Hello"}
  • because strings are char arrays, you can't use operators on then.

    • thus, determing if two strings were the same would require checking each char at each point in the array.

    • and so, there are string functions in the standard library to avoid having to do this manually.

  • Use #include <string.h> to include string library functions.

    • strcpy, strncopy- to change or modify a string. (try and use strncopy, it's safer)

    • strlen


  • pointers are a variable that store an address (in hexadecimal, usually)

  • the value of a pointer, is the address of another location in memory, that can contain a value.

  • A lot of string functions return pointers.