Essential C Programming Syntax and Memory Management
Operators
- Arithmetic: +, -, *, /, %
- Relation: ==, !=, >, <, >=, <=
- Logic: &&, ||, !
- Assignment: =, +=, -=, *=, /=, ?=
- Increment: ++, --
- Order of operations: (), *, /, %, +, -, <>, ==, !=
ASCII Conversions
- Lowercase to uppercase: 'x' - 32
- Uppercase to lowercase: 'x' + 32
Data Types
| Data Types | Example | Size | Format |
|---|---|---|---|
| int | int age = 19; | 4 bytes | %d |
| float | float a = 3.7; | 4 bytes | %f |
| double | double pi = 3.1415; | 8 bytes | %lf |
| char | char grader = 'a'; | 1 byte | %c |
| string | char 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
| Function | Purpose | Example |
|---|---|---|
strlen(str) | Get length of string | strlen("Hello") → 5 |
strcpy(dest, src) | Copy string | strcpy(dest, src) |
strcat(dest, src) | Concatenate strings | strcat(str1, str2) |
strcmp(str1, str2) | Compare strings | strcmp("a","b") → <0 |
strchr(str, char) | Find character | strchr("Hello",'e') → pointer |
strstr(str, substr) | Find substring | strstr("Hello World","World") |
sprintf(str, format, ...) | Write formatted string | sprintf(str,"Num=%d",x) |
atoi(str) / atof(str) | Convert string → int/float | atoi("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.
English with a size of 62.93 KB