Cheatsheet programming 1
Cheatsheet programming 1
variable is char
so the RHS is casted to char
then the LHS is casted to the RHS
LHS is then char type
int main() {
int rows = 3, cols = 4;
return 0;
}
#include <stdio.h>
int main() {
int (*operation)(int, int); // Declare
a function pointer
operation = add; // Assign
the `add` function to the pointer
printf("Sum: %d\n", operation(5, 3));
return 0;
}
#include <stdio.h>
// Function definitions
int add(int a, int b) {
return a + b;
}
int main() {
Operation operation; // Use the typedef
instead of declaring function pointers
explicitly
operation = add;
printf("Sum: %d\n", operation(5, 3));
operation = multiply;
printf("Product: %d\n", operation(5,
3));
return 0;
}
#include <stdio.h>
#ifdef DEBUG
#define LOG(x) printf("DEBUG: %s\n", x)
#else
#define LOG(x)
#endif
int main() {
LOG("This is a debug message");
printf("Program is running\n");
return 0;
}
Program is running
//if you compile using this
gcc -DDEBUG -c example.c
gcc example.o -o example
./example
**#if**
#if
#ifdef #ifndef
#if
#if constant_expression
// Code to include if expression
evaluates to true
#else
// Optional: Code to include if
expression evaluates to false
#endif
#include <stdio.h>
#define VERSION 2
int main() {
#if VERSION >= 2
printf("Version 2 or later.\n"); //
This will be included
#else
printf("Older version.\n");
#endif
t 0
Version 2 or later.
**#if**
#if
#include <stdio.h>
#define DEBUG 1
#define FEATURE 0
int main() {
#if DEBUG
printf("Debug mode is ON.\n");
#if FEATURE
printf("Feature is enabled.\n");
#else
i tf("F t i di bl d \ ")
**#ifdef** **#ifndef**
#ifdef
#ifndef
#if
#if
#include <stdio.h>
// Define platform
#define PLATFORM 1 // 1 = Windows, 2 =
Linux
int main() {
#if PLATFORM == 1
printf("Running on Windows.\n");
#elif PLATFORM == 2
printf("Running on Linux.\n");
# l
**PLATFORM** **1**
Running on Windows.
**#if**
#if
+ - * / %
&& || !
**argc**
argc 1
**argv**
argv[0]
argv[1] argv[argc-1]
#include <stdio.h>
Number of arguments: 3
Argument 0: ./program
Argument 1: Hello
Argument 2: World
#include <stdio.h>
#include <stdlib.h> // For atoi()
int sum = 0;
Sum of arguments: 60
if (strcmp("abc", "abc") == 0)
printf("Strings are equal.\n");
else
printf("Strings are not equal.\n");
%d
%f
%s
%c
%lu
fclose()