Some C concepts

Some C concepts

C is widely considered to be a difficult programming language. Here are various code snippets to help make some sense of it.

Hello world

Here is the traditional Hello World in C:

#include <stdio.h>
int main()
{
   // printf() displays the string inside quotation
   printf("Hello, World!");
   return 0;
}

Typedef

Is used to define a new type.

for example:

typedef unsigned int natural;

defines natural as unsigned int

then a natural variable can be declared as:

natural a = 2;

struct

struct Rectangle {
  int width;
  int height;
};

creates a new type called struct Rectangle

struct  Rectangle myRectangle; 

declares a struct Rectangle called myRectangle

Properties accessed as so:

myRectangle.width = 10;

struct Rectangle is annoying to type so we can use a typedef:

typedef struct Rectangle Rectangle;

a shortcut is:

typedef struct Rectangle {
  int width;
  int height;
} Rectangle;