Variables

Variables are used to store data which can be used later for doing any computation.

Syntax

In this example, we will initialize a variable and store some integer value in it.

Printing Values of Variables

For printing integer variable, we write "%d" inside printf() and then "," then name of the variable.

Rules for assigning names to variables

Names of variables are called identifiers. Use descriptive names for your variables to make your code readable.

  1. Variable names in C can consist of letters (both uppercase and lowercase), digits, and underscores.
  2. Variable name must start with a letter or an underscore.
  3. C is case-sensitive, so variable names like "myVar", and "MYVAR" are considered different variables.
  4. No space is allowed in the name of the variable
  5. Keep the names of variables descriptive and meaningful.
  6. Should not be a reserved keyword like "int", "main" etc.

Scope

  • Local Variables - declared inside the function.
  • Global Variables - declared outside the function.

You can also declare variable without assigning value and assign value later, like in the following example;

You can also change the value of variable later in your code.

Another Example

In this example, we will see how to print text and variable together.

Operations on variables

You can apply any mathematical operation on variables, but condition is that the data types of these variables must be compatible. You can not add int type variable to string. You can only add those variables whose data types are compatible.

Other types of variables

Like integer there are other different datatypes of variables.

  • Int stores numbers without decimal like 10, 20, 3000.
  • Float and double stores numbers with decimal like 2.1, 30.12 etc. The difference is that precision of double in 2x than float.
  • Char stores single character
  • String stores text (collection of characters) inside double quotes like, "My age is 20".
  • Boolean stores true or false. bool answer = true;
  • Void is used when you do not want to assign any type to variable.

Using other data types

In this section, we will discuss about how we can print variables with different data types.

Constant Variables

You can not chnage value of a constant variable after initialization. You will have to initialize the variable at the time of declaration.

Styles for naming variables

  1. Camel Case: In camel case convention, we start with small letter and if there are more than one words, we convert first letter of other words to capital. For example, myAge, myName, johnMarks.
  2. Pascal Case: In pascal case, we capitalize the first letter of every word. For example, MyAge, MyName.
  3. Snake Case: In snake case, we separate words by underscores (_). For example, my_age, my_name.

Note: We have learnt about primitive datatypes like int, double, float, string, char and bool etc. There are other datatypes derived datatypes like array, pointer and user defined datatypes like class, structure, enum etc which are not in scope of this chapter.

Practice Exercises

Complete these exercises to reinforce your learning and earn XP

Sign in to track your progress and earn XP!
Exercise 1 of 2Easy

Which of the following is a valid variable name?

10 XP~3 min
Exercise 2 of 2Easy

To declare an integer variable named 'age', we write: ___ age;

10 XP~2 min