float value = variable / 5;
variable is char
so the RHS is casted to char
then the LHS is casted to the RHS
LHS is then char type
float value = (float)variable / 5;
variable is casted to float manually
RHS is float
then LHS is float
float value = (float)(variable / 5);
the (variable/5) is evaluated first
then (variable/5) = 19
then the 19 is casted to float
RHS is float
then LHS is float
#include <stdio.h>
#include <stdlib.h>
int main() {
int rows = 3, cols = 4;
// Step 1: Allocate memory for the
array of row pointers
int** array = (int**)malloc(rows *
sizeof(int*));
// Step 2: Allocate memory for each row
(array of columns)
for (int i = 0; i < rows; i++) {
*(array + i) = (int*)malloc(cols *
sizeof(int)); // Dereferencing for row
pointers
}
// Step 3: Initialize the 2D array
using pointers
int value = 1;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
*(*(array + i) + j) = value++;
// Dereferencing to access and assign
values
}
}
// Step 4: Print the 2D array using
pointers
printf("2D Array:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", *(*(array + i) +
j)); // Dereferencing to access values
}
printf("\n");
}
// Step 5: Free the allocated memory
for (int i = 0; i < rows; i++) {
free(*(array + i)); //
Dereferencing to free each row
}
free(array); // Free the array of
pointers
return 0;
}
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int multiply(int a, int b) {
return a * b;
}
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));
operation = multiply; // Assign
the `multiply` function to the pointer
printf("Product: %d\n", operation(5,
3));
return 0;
}
#include <stdio.h>
// Define a typedef for a function pointer
typedef int (*Operation)(int, int);
// Function definitions
int add(int a, int b) {
return a + b;
}
int multiply(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;
}
//if you compile using this
gcc -c example.c
gcc example.o -o example
./example
Program is running
//if you compile using this
gcc -DDEBUG -c example.c
gcc example.o -o example
./example
DEBUG: This is a debug message
Program is running
**#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 \ ")
Debug mode is ON.
Feature is disabled.
**#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
+ - * / %
< <= > >= == !=
&& || !
& | ^ ~ << >>
int main(int argc, char *argv[])
**argc**
argc 1
**argv**
argv[0]
argv[1] argv[argc-1]
#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;
}
$ gcc program.c -o program
$ ./program Hello World
Number of arguments: 3
Argument 0: ./program
Argument 1: Hello
Argument 2: World
#include <stdio.h>
#include <stdlib.h> // For atoi()
int main(int argc, char *argv[]) {
if (argc < 3) {
printf("Usage: %s num1 num2
...\n", argv[0]);
return 1; // Exit with error
}
int sum = 0;
$ gcc program.c -o program
$ ./program 10 20 30
Sum of arguments: 60
printf(const char *format, ...)
printf("Hello, %s! You scored %d%%.\n",
"Alice", 95);
// Output: Hello, Alice! You scored 95%.
scanf(const char *format, ...)
int age;
scanf("%d", &age); // Input: 25
printf("You are %d years old.\n", age);
// Output: You are 25 years old.
fprintf(FILE *stream, const char *format, ...)
FILE *file = fopen("output.txt", "w");
fprintf(file, "Temperature: %.2f°C\n", 36.6);
fclose(file);
fscanf(FILE *stream, const char *format, ...)
FILE *file = fopen("data.txt", "r");
int number;
fscanf(file, "%d", &number);
printf("Number: %d\n", number);
fclose(file);
size_t strlen(const char *str)
char str[] = "Hello";
printf("Length: %lu\n", strlen(str)); //
Output: 5
char *strcat(char *dest, const char *src)
char str1[20] = "Hello, ";
char str2[] = "World!";
strcat(str1, str2);
printf("%s\n", str1); // Output: Hello,
World!
int strcmp(const char *str1, const char *str2)
if (strcmp("abc", "abc") == 0)
printf("Strings are equal.\n");
else
printf("Strings are not equal.\n");
int sprintf(char *str, const char *format, ...)
char buffer[50];
sprintf(buffer, "Pi is approximately %.2f",
3.14159);
printf("%s\n", buffer);
int sscanf(const char *str, const char *format,
...)
char input[] = "42 3.14";
int num;
float pi;
sscanf(input, "%d %f", &num, &pi);
printf("Number: %d, Pi: %.2f\n", num, pi);
%d
%f
%s
%c
%lu
fclose()