Variables

Lesson 6
Author : Afrixi
Last Updated : January, 2023
C - Programming Language
This course covers the basics of programming in C.

In C, a variable is a named storage location in memory that is used to store a value that can be changed during the execution of a program.

Variables can be declared using the syntax:

data_type variable_name;

Where data_type is the type of data that the variable can hold, and variable_name is the name given to the variable.

For example, to declare an integer variable named x, we can use the following statement:

int x;

This tells the compiler to allocate memory to store an integer value and to give it the name x. The variable can then be assigned a value using the assignment operator =:

x = 42;

Or, we can declare and initialize a variable in a single statement like this:

int x = 42;

C has several built-in data types, including:

  • int: used to hold integer values
  • float: used to hold floating-point values with single-precision
  • double: used to hold floating-point values with double-precision
  • char: used to hold single characters
  • bool: used to hold Boolean values (true or false)

Variables can also be modified using arithmetic and assignment operators, for example:

x = x + 1;  // increment x by 1
x += 1;     // equivalent to x = x + 1
x++;        // increment x by 1 (shortcut for x = x + 1)

It’s important to note that variables have a scope, which is the part of the program where the variable is visible and can be accessed. The scope of a variable is determined by where it is declared, and it can be local to a function or global to the entire program.