Variable Declaration

Before we can use a variable, we must declare it. Variable declaration means we tell the computer:

  • What type of values the variable will contain
  • What the variable will be named

The basic syntax for variable declaration is:

type variable_name;

For example:

int number;        // Declaration of a variable for integers (int) named number
double decimal;    // Declaration of a variable for decimal numbers (double) named decimal
char character;    // Declaration of a variable for one character (char) named character

After declaration, the variable is ready to use, but doesn't have any specific value assigned yet. We can assign a value later.

Rules for variable names:

  • Can contain letters, digits, and underscores
  • Must start with a letter or underscore
  • Cannot contain spaces or special characters
  • Cannot be the same as language keywords (e.g. int, double, if, while)

Instructions

In the main function, declare a variable of type int with the name age.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

2/6

Variable Declaration | Start Coder