Keyboard shortcuts

Press โ† or โ†’ to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

๐Ÿ  Back to Blog

C Programming Notes

Overview

  • C is a general-purpose, procedural computer programming language supporting structured programming, lexical variable scope, and recursion, with a static type system.
  • Every C program has a main() function that is the entry point of the program.
  • C is a compiled language, meaning that the source code is compiled into machine code before it is executed.
  • C is a low-level language, meaning that it is closer to machine code than high-level languages like Python or JavaScript.
  • C does not support object-oriented programming
  • C is a statically typed language, meaning that the type of a variable must be declared before it is used.

Comments

  • Single line comments are denoted by //
  • Multi-line comments are denoted by /* */

Importing Libraries

  • Libraries are imported using the #include directive

Variables

  • A variable scope is the region of code where a variable can be accessed.
  • In C, all variables must be declared before they are used.
  • Variables must be declared with a type and an optional initial value.
  • To declare a variable:
int x = 5;
unsigned int y = 10;

char c = 'a';

float f = 3.14;

double d = 3.14159;

int x[5] = {1, 2, 3, 4, 5};

struct Point {
    int x;
    int y;
};

Structs

  • A struct is a user-defined data type that groups related data together.
  • To declare a struct:
struct Point {
    int x;
    int y;
};
  • To create an instance of a struct:
struct Point p;
p.x = 10;
p.y = 20;
  • To create a pointer to a struct:
struct Point *ptr = &p;
  • To access a member of a struct using a pointer:
ptr->x = 30;
ptr->y = 40;

Strings

  • C does not support strings as a primitive type. Instead, strings are represented as arrays of characters. You can import the string.h library to use string functions.
  • To declare a string:
char str[10] = "Hello\0";
  • In the example above, we declare a character array str with a size of 10. The string โ€œHelloโ€ is stored in the array, and the null character \0 is used to terminate the string. We use the null character because we cannot assume that the string is the same size as the array. Arrays may be larger than the string they contain.
  • C provides a string library with common functions for manipulating strings

Data Types

  • Basic data types in C include:
    • int: integer
    • char: character
    • float: floating-point number
    • double: double-precision floating-point number
    • void: no value
  • Modifiers can be used to modify the basic data types:
    • short: short integer
    • long: long integer
    • signed: signed integer
    • unsigned: unsigned integer
  • The sizeof() function can be used to determine the size of a data type in bytes.
  • The typedef keyword can be used to create custom data types.
  • C does not include boolean types by default. Instead, 0 is considered false and any other value is considered true.

Operators

  • Arithmetic operators: +, -, *, /, %
  • Relational operators: ==, !=, >, <, >=, <=
  • Logical Operators: &&, ||, !
  • Bitwise Operators: &, |, ^, ~, <<, >>

Line and Spacing Conventions

  • C is not whitespace sensitive, but it is good practice to use whitespace to make code more readable.
  • Statements in C are terminated by a semicolon ;
  • Blocks of code are enclosed in curly braces {}

Input and Output

  • The printf() function is used to print output to the console.
  • The scanf() function is used to read input from the console.
  • The getchar() function is used to read a single character from the console.
  • The putchar() function is used to print a single character to the console.
  • The gets() function is used to read a string from the console.
  • The puts() function is used to print a string to the console.

Conditionals

  • The if statement is used to execute a block of code if a condition is true.
  • The else statement is used to execute a block of code if the condition is false.
  • The else if statement is used to execute a block of code if the previous condition is false and the current condition is true.
  • Example:
int x = 10;
if (x > 5) {
    printf("x is greater than 5\n");
} else if (x == 5) {
    printf("x is equal to 5\n");
} else {
    printf("x is less than 5\n");
}

Loops

  • The for loop is used to execute a block of code a fixed number of times.
  • The while loop is used to execute a block of code as long as a condition is true.
  • The do while loop is similar to the while loop, but the condition is checked after the block of code is executed.

Examples

Hello World using a function from the math library

#include <stdio.h>
#include <math.h>

int main() {
    printf("Hello, World!\n");
    printf("The square root of 16 is %f\n", sqrt(16));
    return 0;
}
  • The main function returns a value of type int. By convention, a return value of 0 indicates that the program executed successfully.

Reading and printing an integer

#include <stdio.h>

int main() {
    int x;
    printf("Enter an integer: ");
    scanf("%d", &x);
    printf("You entered: %d\n", x);
    return 0;
}

Unions

  • A union is a user-defined data type that allows storing different data types in the same memory location.
  • The size of a union is determined by the size of its largest member.
  • A union can only store one member at a time.
  • To declare a union:
union Data {
    int i;
    float f;
    char str[20];
};

int main() {}
    union Data data;
    data.i = 10;
    printf("data.i: %d\n", data.i);
}