Variable Data Types
In C, you must declare the type of data a variable will hold, such as integers (int), floating-point numbers (float), or single characters (char).
int age = 25;float temperature = 36.5;char grade = 'A';
Hand-picked snippets across TypeScript, Python, Rust, Go, and more — each one comes with a line-by-line breakdown so you can read it like the people who wrote it.
In C, you must declare the type of data a variable will hold, such as integers (int), floating-point numbers (float), or single characters (char).
int age = 25;float temperature = 36.5;char grade = 'A';
Conditional statements allow the program to make decisions and execute different blocks of code based on a boolean expression.
int score = 85;if (score >= 50) {printf("Pass\n");} else {printf("Fail\n");}
This is the simplest C program. It includes the standard input-output library and defines the main entry point where execution begins.
#include <stdio.h>int main() {printf("Hello, World!\n");return 0;}
A for loop repeats a block of code a specific number of times, using a counter variable to track progress.
for (int i = 0; i < 5; i++) {printf("Iteration: %d\n", i);}
Functions are reusable blocks of code that perform specific tasks. They can take parameters and return a value.
int add(int a, int b) {return a + b;}// Usage: int result = add(5, 3);
In C, strings are simply arrays of characters ending with a special null character '\0'.
char greeting[] = "Hello";printf("%s has %lu characters", greeting, strlen(greeting));
A struct is a user-defined data type that allows grouping related variables of different types together.
struct Player {int id;float health;};struct Player p1 = {1, 100.0};
The switch statement is an alternative to long if-else chains. It compares a variable against multiple 'case' values and executes the matching block.
switch (grade) {case 'A':printf("Excellent!\n");break;case 'B':printf("Good job!\n");break;default:printf("Keep trying.\n");}