Here’s a C cheat sheet:
Hello World
#include <stdio.h>
int main() {
printf("Hello, C!\n");
return 0;
}
Variables and Data Types
int myInt = 42;
float myFloat = 3.14;
char myChar = 'A';
double myDouble = 2.71828;
Input/Output
#include <stdio.h>
int main() {
int userInput;
printf("Enter a number: ");
scanf("%d", &userInput);
printf("You entered: %d\n", userInput);
return 0;
}
Control Flow
if (condition) {
// Code to execute
} else if (anotherCondition) {
// Code to execute if the first condition is false and this one is true
} else {
// Code to execute if all conditions are false
}
for (int i = 0; i < 5; ++i) {
// Code to repeat
}
Functions
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(3, 4);
printf("Result: %d\n", result);
return 0;
}
Arrays
int myArray[5] = {1, 2, 3, 4, 5};
Pointers
int myValue = 42;
int *pointerToValue = &myValue;
Structures
struct Point {
int x;
int y;
};
struct Point p1 = {1, 2};
File I/O
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "w");
fprintf(file, "Hello, File!");
fclose(file);
return 0;
}
Memory Allocation
#include <stdlib.h>
int *dynamicArray = (int *)malloc(5 * sizeof(int));
free(dynamicArray);
Command Line Arguments
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Number of arguments: %d\n", argc);
for (int i = 0; i < argc; ++i) {
printf("Argument %d: %s\n", i, argv[i]);
}
return 0;
}
Preprocessor Directives
#define PI 3.14159
This C cheat sheet covers basic syntax, variables, control flow, functions, arrays, pointers, structures, file I/O, memory allocation, command line arguments, preprocessor directives, and provides additional resources for learning C. Adjust as needed for your specific C programming requirements.