C

C is a programming language that is very low-level, compiling down to assembly language.

Hello, World

Code

Create a new file called helloworld.c and add this to the contents:

#include <stdio.h>  /* Imports the standard library for IO */

main()  /* defines the function main containing no parameters */
{
    printf("hello, world\n"); /* prints "hello, world" in stdio */
    /* note that the newline must be placed deliberately. C does */
    /* not add a newline with print like Python does. */
}

Compile

Then you want to compile it. The basic compiler found on UNIX computers is cc. Compile it by typing cc helloworld.c. This will create a file called a.out, which is the compiled version of your helloworld.c.

Run It

Run this by typing ./a.out in your shell. You should see hello, world in your terminal.

Basics

Comments

// Single line comment
/* Multiline
   comment */

Symbolic Constants

Symbolic constants allow the developer to avoid magic numbers and add semantically useful labels to values. This is done using he syntax #define NAME value.

#include <stdio.h>
#define TAXRATE 0.09
#define SERVICECHARGE 2

main()
{
  printf("The tax rate is %1.2f and the service charge is %d", TAXRATE, SERVICECHARGE);
}

/* prints "The tax rate is 0.09 and the service charge is 2"

These statements don't actually do anything in the code. They are handled by the preprocessor and the references to the NAME are simply replaced by the corrseponding value when found. So the above printf line, when eventually seen by the compiler, will be:

printf("The tax rate is %1.2f and the service charge is %d", 0.09, 2);

Types

Since C is strongly typed, certain actions that are intuitive in Javascript or Python may not yield the results you wanted. For instance, if you have variable int x and you try and initialize it with a fractional number, the fractional part will be discarded since x stores an int.

Arrays are intialized as normal, but followed by n items that should reside in the array: int numbers[n].

The following list is only what I've used most and by no means a complete list. Look at the Wikipedia page for more info.

Type Bits Limits Format Description
char 8 -127, +127 %c Used for characters within strings (array of chars)
int 16 -32,767, +32,767 %i/%d Used for numbers and traversing data from stdin

Unsigned numbers are always zero or higher and will have a range of 0 to (2^n) - 1. Signed numbers are negative or positive and have a range of -(2^n-1) to (2^n-1) - 1.

Variables

Variables must be declared before they are used, usually at the beginning before executing your program. They are declared by type and then by name.

int num, step;
char letter;

Variables that exist in and only within a given function are called automatic variables. They come into existence only when the function begins and disappear when the function is finished.

External Variables

External variables are defined exactly once outside of any function. This is so that the computer can set aside storage. Within each function that will use that, you will need to declare that variable, and preface that declaration with extern. For instance if max is an external variable, you would declare it within the function as extern int max.

If multiple source files are used, for instance defining X in one file and wanting to use it in another file, the extern declaration is required. If it is all in one file, they can be omitted.

Common practice is to define all external variables in a header file with a .h extension and then #include it in your source files.

Libraries

stdio.h

printf(string, [format, ...])

If you use a format tag in the string, like %d or %c, you will need to add what will replace it in the list of format variables.

int fahr, celsius;
fahr = 100;
celsius = 37;
printf("%d F = %d C", fahr, celsius);
/* prints "100 F = 37 C" */

The format tags can also be accompanied by the minimum width of characters printed.

printf("%3d F = %3d C", fahr, celsius); /* prints "100 F = 37  C" */
printf("%2d F = %2d C", fahr, celsius); /* prints "100 F = 37 C" */

getchar(), putchar(x)

getchar will resolve to a character from stdin, and putchar(x) will put the value of x in stdout.

Sandboxing

You can send text via stdin using printf "123xyz" | ./program.out, with program.out being the name of the compiled program.

You can also use an online REPL, like replit.com.

References

  1. https://hikage.freeshell.org/books/theCprogrammingLanguage.pdf
  2. https://stackoverflow.com/questions/19379353/symbolic-constants-in-c-define-statement
  3. https://en.wikipedia.org/wiki/C_data_types

Last modified: 202107240303