codegurukool.com · Free coding tutorials & online compiler

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

↗ C get started

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

↗ C get started

main()

Definition: main() is where execution starts.

Syntax:

main()

Example:

int main() {

↗ C syntax

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));

↗ C data types

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));

↗ C data types

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));

↗ C data types

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));

↗ C data types

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));

↗ C data types

const

Definition: Use const to protect a variable.

Syntax:

const

Example:

const float PI = 3.14;

↗ C constants

#define

Definition: #define makes a text replacement.

Syntax:

#define

Example:

#define MAX 100

↗ C constants

stdbool.h

Definition: Include stdbool.h for bool , true and false .

Syntax:

stdbool.h

Example:

#include <stdbool.h>

↗ C booleans

bool

Definition: Include stdbool.h for bool , true and false .

Syntax:

bool

Example:

bool ok = true;

↗ C booleans

true

Definition: C treats 0 as false and any other value as true.

Syntax:

true

Example:

bool ok = true;

↗ C booleans

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");

↗ C if...else

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;

↗ C break and continue

string.h

Definition: Use string.h : strlen , strcpy , strcat , strcmp .

Syntax:

string.h

Example:

#include <string.h>

↗ C strings

strcat

Definition: Use string.h : strlen , strcpy , strcat , strcmp .

Syntax:

strcat

Example:

strcat(s, " C");

↗ C strings

strcmp

Definition: Use string.h : strlen , strcpy , strcat , strcmp .

Syntax:

strcmp

Example:

printf("%s %zu %d\n", s, strlen(s), strcmp("a", "a"));

↗ C strings

fgets

Definition: Use fgets to read a whole line safely.

Syntax:

fgets

Example:

fgets(name, 30, stdin);

↗ C user input

x

Definition: &x gives the address of x .

Syntax:

x

Example:

int x = 5;
printf("%d at %p\n", x, (void*) &x);

↗ C memory address

math.h

Definition: Include math.h : sqrt , pow , ceil , floor , fabs .

Syntax:

math.h

Example:

#include <math.h>

↗ C math functions

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));

↗ C math functions

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));

↗ C math functions

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));

↗ C math functions

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));

↗ C math functions

fopen(name, "w")

Definition: fopen(name, "w") creates or overwrites.

Syntax:

fopen(name, "w")

Example:

FILE *f = fopen("data.txt", "w");

↗ C create and write files

fprintf

Definition: Write with fprintf and always fclose .

Syntax:

fprintf

Example:

fprintf(f, "Hello file\n");

↗ C create and write files

fclose

Definition: Write with fprintf and always fclose .

Syntax:

fclose

Example:

fclose(f);

↗ C create and write files

struct

Definition: A struct bundles related fields.

Syntax:

struct

Example:

struct Student { char name[20]; int age; };
struct Student s = {"Ada", 21};

↗ C structures

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;

↗ C typedef

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));

↗ C allocate and access memory

realloc

Definition: realloc grows or shrinks a block and may move it.

Syntax:

realloc

Example:

int *t = realloc(a, 4 * sizeof(int));

↗ C reallocate memory

free

Definition: Every malloc needs one free .

Syntax:

free

Example:

free(p);

↗ C deallocate memory

perror

Definition: perror and errno describe the problem.

Syntax:

perror

Example:

if (f == NULL) { perror("fopen"); return 1; }

↗ C NULL and errors

time(NULL)

Definition: time(NULL) returns seconds since 1970.

Syntax:

time(NULL)

Example:

#include <time.h>
time_t now = time(NULL);

↗ C date and time

localtime

Definition: localtime and strftime format it.

Syntax:

localtime

Example:

strftime(b, 40, "%Y-%m-%d", localtime(&now));

↗ C date and time

strftime

Definition: localtime and strftime format it.

Syntax:

strftime

Example:

strftime(b, 40, "%Y-%m-%d", localtime(&now));

↗ C date and time

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));

↗ C random numbers

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);

↗ C random numbers

static

Definition: A static local keeps its value between calls.

Syntax:

static

Example:

void count() { static int n = 0; n++; printf("%d ", n); }

↗ C storage classes

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;

↗ C fixed-width integers

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]);

↗ C command-line arguments

Example code

#include <stdio.h>

int main() {
  printf("Hello, C!\n");
  return 0;
}

Run this code in the online editor

← Welcome to the C courseC introduction →