Essential C Programming Syntax and Memory Management

Posted by Anonymous and classified in Computers

Written on in English with a size of 62.93 KB

Operators

  • Arithmetic: +, -, *, /, %
  • Relation: ==, !=, >, <, >=, <=
  • Logic: &&, ||, !
  • Assignment: =, +=, -=, *=, /=, ?=
  • Increment: ++, --
  • Order of operations: (), *, /, %, +, -, <>, ==, !=

ASCII Conversions

  • Lowercase to uppercase: 'x' - 32
  • Uppercase to lowercase: 'x' + 32

Data Types

Data TypesExampleSizeFormat
intint age = 19;4 bytes%d
floatfloat a = 3.7;4 bytes%f
doubledouble pi = 3.1415;8 bytes%lf
charchar grader = 'a';1 byte%c
stringchar name[20] = "pierre";array%s

Constants

#define TAX 0.13
const int Max = 100;

Control Structures

Statements: if, else if, else

Switches

switch(x) {
    case 1: printf("One"); break;
    case 2: printf("Two"); break;
    default: printf("Other");
}

Loops: while, do while, for

For Loop Example

#include <stdio.h>
int main() {
    for (int i = 1; i <= 5; i++) {
        printf("Value of i: %d\n", i);
    }
    return 0;
}

Functions

#include <stdio.h>
int add(int a, int b) {
    return a + b;
}
int main() {
    int result = add(7, 3);
    printf("Result: %d\n", result);
    return 0;
}

C Strings Basics

  • Strings are arrays of characters ending with \0.
  • Include library: #include <string.h>

Common Functions

tGMDwiYvM6WxGKi3Jx994wXo6KGLFhfTGWFebg9t0hvB4TsXnTBgvy86LH5hu3nuHthIgbWzeXYElONq7eCGJyUo7NWyUsWpQF77UnmJLjhmN7KbJ0cYPXJ8Ovzxz46iRDCRkRERERpR+XLImIiIhMxoSMiIiIyGRMyIiIiIhMxoSMiIiIyGRMyIiIiIhMxoSMiIiIyGRMyIiIiIhMxoSMiIiIyGRMyIiIiIhMxoSMiIiIyGRMyIiIiIhMxoSMiIiIyGRMyIiIiIhMxoSMiIiIyGRMyIiIiIhMxoSMiIiIyGRMyIiIiIhMxoSMiIiIyGRMyIiIiIhMxoSMiIiIyGRMyIiIiIhMxoSMiIiIyGRMyIiIiIhMxoSMiIiIyGRMyIiIiIhMxoSMiIiIyGRMyIiIiIhMxoSMiIiIyGRMyIiIiIhMxoSMiIiIyGRMyIiIiIhMxoSMiIiIyGRMyIiIiIhMxoSMiIiIyGRMyIiIiIhMxoSMiIiIyGRMyIiIiIhMxoSMiIiIyGRMyIiIiIhMxoSMiIiIyGRMyIiIiIhMxoSMiIiIyGRMyIiIiIhMxoSMiIiIyGRMyIiIiIhMxoSMiIiIyGRMyIiIiIhMxoSMiIiIyGRMyIiIiIhMxoSMiIiIyGRMyIiIiIhMxoSMiIiIyGRMyIiIiIhMxoSMiIiIyGRMyIiIiIhM9j8noR2I0TP8zQAAAABJRU5ErkJggg==

FunctionPurposeExample
strlen(str)Get length of stringstrlen("Hello") → 5
strcpy(dest, src)Copy stringstrcpy(dest, src)
strcat(dest, src)Concatenate stringsstrcat(str1, str2)
strcmp(str1, str2)Compare stringsstrcmp("a","b") → <0
strchr(str, char)Find characterstrchr("Hello",'e') → pointer
strstr(str, substr)Find substringstrstr("Hello World","World")
sprintf(str, format, ...)Write formatted stringsprintf(str,"Num=%d",x)
atoi(str) / atof(str)Convert string → int/floatatoi("123") → 123

Array Operations

Find the highest element

int numbers[5] = {10, 45, 30, 22, 50};
int max = numbers[0];
for (int i = 1; i < 5; i++) {
    if (numbers[i] > max) max = numbers[i];
}

Remove numbers from char array

void remNums(char a_orig[]) {
    int j = 0;
    for (int i = 0; a_orig[i] != '\0'; i++) {
        if (a_orig[i] < '0' || a_orig[i] > '9') {
            a_orig[j++] = a_orig[i];
        }
    }
    a_orig[j] = '\0';
}

Memory Management

Dynamic array with malloc

int *ptr = (int*)malloc(sizeof(int));
*ptr = 42;
free(ptr);

Pointers

  • & = address of variable
  • * = dereference (access value at pointer)
  • p++ moves to next memory address based on type size.

Assertions

assert(condition);

  • True: Program continues.
  • False: Program stops and prints file/line number.

Structs and Pointers

struct student {
    char name[50];
    unsigned int id;
    unsigned int age;
};

Variable Scopes

  • Global: Visible to the whole file.
  • Local: Visible inside block or function.
  • Static: Local but persistent.
  • Extern: Visible through multiple files.

Call by Value vs Reference

  • Call by Value: Argument is copied to the parameter.
  • Call by Reference: Pointer is passed to modify the original variable.

Related entries: