Home › C Tutorial › All commands
All commands
Every command of this course: name, detailed description, syntax and example code.
Every command used in this course, with its name, a detailed description, the syntax and an example.
#include
Definition: Copies a header file into your program so you can use its functions, for example printf from stdio.h.
Syntax:
#include <header.h>
Example:
#include <stdio.h>
int main
Definition: The function where every C program starts running. Returning 0 tells the system the program finished without problems.
Syntax:
int main(void) {
/* code */
return 0;
}Example:
int main(void) {
return 0;
}printf
Definition: Prints formatted text. Codes such as %d (integer) and %s (string) are replaced by the values that follow.
Syntax:
printf("format", values);Example:
printf("Age: %d
", 20);scanf
Definition: Reads formatted input from the keyboard into variables. Put & before the variable so scanf gets its address.
Syntax:
scanf("format", &variable);Example:
int n;
scanf("%d", &n);int, float, char
Definition: Basic data types: int stores whole numbers, float stores decimals and char stores a single character.
Syntax:
type name = value;
Example:
int a = 5; float b = 2.5; char c = 'A';
if
Definition: Runs a block only when its condition is true. Add else for the case when it is false.
Syntax:
if (condition) {
...
} else {
...
}Example:
if (a > 3) {
printf("big");
}for
Definition: Repeats a block a fixed number of times using a start value, a condition and a step.
Syntax:
for (init; condition; step) {
...
}Example:
for (int i = 0; i < 3; i++) {
printf("%d ", i);
}while
Definition: Repeats a block for as long as its condition stays true.
Syntax:
while (condition) {
...
}Example:
int i = 0;
while (i < 3) {
i++;
}return
Definition: Ends a function immediately and sends a value back to the code that called it.
Syntax:
return value;
Example:
int add(int a, int b) {
return a + b;
}strlen
Definition: Returns how many characters a string has, not counting the end marker. Needs string.h.
Syntax:
strlen(string);
Example:
int n = strlen("hello"); /* 5 */gcc
Definition: Install gcc (or use an online compiler or an IDE like Code::Blocks).
Syntax:
gcc
Example:
gcc main.c -o main
main.c
Definition: Save as main.c , compile with gcc main.c -o main and run ./main .
Syntax:
main.c
Example:
gcc main.c -o main
char
Definition: char 1 byte, int usually 4, float 4, double 8.
Syntax:
char
Example:
printf("%zu %zu %zu %zu\n", sizeof(char), sizeof(int), sizeof(float), sizeof(double));int
Definition: char 1 byte, int usually 4, float 4, double 8.
Syntax:
int
Example:
int main() {
printf("%zu %zu %zu %zu\n", sizeof(char), sizeof(int), sizeof(float), sizeof(double));float
Definition: char 1 byte, int usually 4, float 4, double 8.
Syntax:
float
Example:
printf("%zu %zu %zu %zu\n", sizeof(char), sizeof(int), sizeof(float), sizeof(double));double
Definition: char 1 byte, int usually 4, float 4, double 8.
Syntax:
double
Example:
printf("%zu %zu %zu %zu\n", sizeof(char), sizeof(int), sizeof(float), sizeof(double));sizeof
Definition: Use sizeof to check on your machine.
Syntax:
sizeof
Example:
printf("%zu %zu %zu %zu\n", sizeof(char), sizeof(int), sizeof(float), sizeof(double));const
Definition: Use const to protect a variable.
Syntax:
const
Example:
const float PI = 3.14;
#define
Definition: #define makes a text replacement.
Syntax:
#define
Example:
#define MAX 100
stdbool.h
Definition: Include stdbool.h for bool , true and false .
Syntax:
stdbool.h
Example:
#include <stdbool.h>
bool
Definition: Include stdbool.h for bool , true and false .
Syntax:
bool
Example:
bool ok = true;
true
Definition: C treats 0 as false and any other value as true.
Syntax:
true
Example:
bool ok = true;
else
Definition: Use if , else if , else , or the short form cond ?
Syntax:
else
Example:
else if (t < 17) printf("Afternoon\n");
else printf("Evening\n");case
Definition: Each case needs break or execution falls through.
Syntax:
case
Example:
case 1: printf("Mon\n"); break;
case 3: printf("Wed\n"); break;↗ C switch
break
Definition: Each case needs break or execution falls through.
Syntax:
break
Example:
case 1: printf("Mon\n"); break;
case 3: printf("Wed\n"); break;↗ C switch
default
Definition: default catches the rest.
Syntax:
default
Example:
default: printf("Other\n");↗ C switch
continue
Definition: continue skips to the next round.
Syntax:
continue
Example:
if (i == 2) continue;
string.h
Definition: Use string.h : strlen , strcpy , strcat , strcmp .
Syntax:
string.h
Example:
#include <string.h>
strcat
Definition: Use string.h : strlen , strcpy , strcat , strcmp .
Syntax:
strcat
Example:
strcat(s, " C");
strcmp
Definition: Use string.h : strlen , strcpy , strcat , strcmp .
Syntax:
strcmp
Example:
printf("%s %zu %d\n", s, strlen(s), strcmp("a", "a"));fgets
Definition: Use fgets to read a whole line safely.
Syntax:
fgets
Example:
fgets(name, 30, stdin);
x
Definition: &x gives the address of x .
Syntax:
x
Example:
int x = 5;
printf("%d at %p\n", x, (void*) &x);math.h
Definition: Include math.h : sqrt , pow , ceil , floor , fabs .
Syntax:
math.h
Example:
#include <math.h>
sqrt
Definition: Include math.h : sqrt , pow , ceil , floor , fabs .
Syntax:
sqrt
Example:
printf("%.1f %.0f %.0f %.1f\n", sqrt(49), pow(2, 5), ceil(4.1), fabs(-3.5));pow
Definition: Include math.h : sqrt , pow , ceil , floor , fabs .
Syntax:
pow
Example:
printf("%.1f %.0f %.0f %.1f\n", sqrt(49), pow(2, 5), ceil(4.1), fabs(-3.5));ceil
Definition: Include math.h : sqrt , pow , ceil , floor , fabs .
Syntax:
ceil
Example:
printf("%.1f %.0f %.0f %.1f\n", sqrt(49), pow(2, 5), ceil(4.1), fabs(-3.5));fabs
Definition: Include math.h : sqrt , pow , ceil , floor , fabs .
Syntax:
fabs
Example:
printf("%.1f %.0f %.0f %.1f\n", sqrt(49), pow(2, 5), ceil(4.1), fabs(-3.5));fopen(name, "w")
Definition: fopen(name, "w") creates or overwrites.
Syntax:
fopen(name, "w")
Example:
FILE *f = fopen("data.txt", "w");fprintf
Definition: Write with fprintf and always fclose .
Syntax:
fprintf
Example:
fprintf(f, "Hello file\n");
fclose
Definition: Write with fprintf and always fclose .
Syntax:
fclose
Example:
fclose(f);
struct
Definition: A struct bundles related fields.
Syntax:
struct
Example:
struct Student { char name[20]; int age; };
struct Student s = {"Ada", 21};union
Definition: A union holds one member at a time; its size is the largest member.
Syntax:
union
Example:
union U { int i; float f; };
union U u; u.i = 7;↗ C unions
typedef
Definition: typedef removes the need to write struct each time.
Syntax:
typedef
Example:
typedef struct { int x, y; } Point;enum
Definition: enum assigns names to numbers starting at 0 unless you set values.
Syntax:
enum
Example:
enum Level { LOW, MEDIUM = 5, HIGH };
enum Level l = HIGH;↗ C enums
malloc(bytes)
Definition: malloc(bytes) returns a pointer, or NULL on failure.
Syntax:
malloc(bytes)
Example:
int *a = malloc(3 * sizeof(int));
realloc
Definition: realloc grows or shrinks a block and may move it.
Syntax:
realloc
Example:
int *t = realloc(a, 4 * sizeof(int));
perror
Definition: perror and errno describe the problem.
Syntax:
perror
Example:
if (f == NULL) { perror("fopen"); return 1; }time(NULL)
Definition: time(NULL) returns seconds since 1970.
Syntax:
time(NULL)
Example:
#include <time.h> time_t now = time(NULL);
localtime
Definition: localtime and strftime format it.
Syntax:
localtime
Example:
strftime(b, 40, "%Y-%m-%d", localtime(&now));
strftime
Definition: localtime and strftime format it.
Syntax:
strftime
Example:
strftime(b, 40, "%Y-%m-%d", localtime(&now));
srand(time(NULL))
Definition: Seed once with srand(time(NULL)) , then use rand() % n for 0 to n-1.
Syntax:
srand(time(NULL))
Example:
srand(time(NULL));
rand() % n
Definition: Seed once with srand(time(NULL)) , then use rand() % n for 0 to n-1.
Syntax:
rand() % n
Example:
printf("%d\n", rand() % 6 + 1);static
Definition: A static local keeps its value between calls.
Syntax:
static
Example:
void count() { static int n = 0; n++; printf("%d ", n); }int32_t
Definition: int8_t , uint16_t , int32_t , uint64_t have the same size on every platform.
Syntax:
int32_t
Example:
int32_t y = 100000;
argc
Definition: main(int argc, char *argv[]) : argc counts arguments, argv[0] is the program name.
Syntax:
argc
Example:
int main(int argc, char *argv[]) {
printf("%d args\n", argc);
for (int i = 0; i < argc; i++) printf("%s\n", argv[i]);Example code
#include <stdio.h>
int main() {
printf("Hello, C!\n");
return 0;
}