What is a variable?

A variable is a small piece of your memory where you can store informations which are needed for your program.

Example:

#include <stdio.h>

void main()
{
 int Number;
 Number=5;
 printf("%d\n", Number);
}

When you run the program, you should see that screen:

5
_

int Number; Here is the declaration of the variable (Attention: case sensitive!!)
Number=5; means "set Number to 5"
...("%d\n", Number)... \n is known, %d is replaced with the number on the right side.

int declares the variable to be an integer. Here are some types:

char one byte, takes one character
int an integer
float a floating point number
double a more precicios floating point number

What is a constant?

A constant is used like a variable, except that it cannot changed at runtime.

There are two ways to define constants. Example 1:

#include <stdio.h>

void main()
{
 const int YES=1;
 printf("%d\n", YES);
}

Example 2:

#include <stdio.h>
#define YES 1

void main()
{
 printf("%d\n", YES);
}

The const-command is preferred.

You can use algebraic commands as normal (like with a calculator) in definitions. (i.e.: Number=3*5;)

  • Write a program that calculates 7*4 using a variable and a constant. Solution
    Chapter 3
    Contents
    Starting page
    1