Getting Started
2 snippetsBasic C program structure
Hello World
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}Comments
// Single line comment
/* Multi-line
comment */Variables & Types
5 snippetsData types and declarations
Integer
int age = 25;
long count = 1000000;
short num = 32000;Float & Double
float price = 19.99f;
double pi = 3.14159265359;Char
char letter = 'A';
char name[] = "John";Constants
const int MAX = 100;
#define PI 3.14159Type Casting
int x = 10;
float y = (float)x;
int z = (int)3.14;Operators
4 snippetsArithmetic and logic
Arithmetic
int a = 5 + 3; // 8
int b = 5 - 3; // 2
int c = 5 * 3; // 15
int d = 5 / 3; // 1
int e = 5 % 3; // 2Comparison
5 == 5 // 1 (true)
5 != 3 // 1 (true)
5 > 3 // 1 (true)
5 < 3 // 0 (false)Logical
(5 > 3) && (2 < 4) // 1 (true)
(5 > 3) || (2 > 4) // 1 (true)
!(5 > 3) // 0 (false)Increment/Decrement
int x = 5;
x++; // 6 (post-increment)
++x; // 7 (pre-increment)
x--; // 6 (post-decrement)
--x; // 5 (pre-decrement)Tired of looking up syntax?
DocuWriter.ai generates documentation and explains code using AI.
Control Flow
5 snippetsConditionals and loops
If/Else
if (x > 0) {
printf("positive");
} else if (x < 0) {
printf("negative");
} else {
printf("zero");
}Switch
switch (day) {
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
default:
printf("Other");
}For Loop
for (int i = 0; i < 10; i++) {
printf("%d\n", i);
}While Loop
int i = 0;
while (i < 10) {
printf("%d\n", i);
i++;
}Do-While
int i = 0;
do {
printf("%d\n", i);
i++;
} while (i < 10);Arrays
4 snippetsFixed-size collections
Declaration
int numbers[5];
int nums[] = {1, 2, 3, 4, 5};Access
int first = nums[0];
nums[1] = 10;2D Array
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};String
char name[] = "John";
char greeting[20] = "Hello";Functions
3 snippetsReusable code blocks
Function
int add(int a, int b) {
return a + b;
}
int result = add(5, 3); // 8Void Function
void greet() {
printf("Hello!\n");
}Function Prototype
// Declare before use
int multiply(int a, int b);
int main() {
int result = multiply(3, 4);
return 0;
}
int multiply(int a, int b) {
return a * b;
}Pointers
4 snippetsMemory addresses and references
Pointer Declaration
int x = 10;
int *ptr = &x; // ptr stores address of xDereference
int value = *ptr; // Get value at address
*ptr = 20; // Modify valueNull Pointer
int *ptr = NULL;Pointer Arithmetic
int arr[] = {1, 2, 3};
int *p = arr;
p++; // Move to next elementStructs
4 snippetsCustom data types
Define Struct
struct Person {
char name[50];
int age;
float height;
};Create & Use
struct Person person1;
strcpy(person1.name, "John");
person1.age = 30;Initialize
struct Person person2 = {
"Jane",
25,
5.6
};Typedef
typedef struct {
char name[50];
int age;
} Person;
Person p; // No 'struct' needed