c
  1. c-variables

C Variables

Syntax

In C language, a variable declaration has the following syntax:

data_type variable_name;

For example:

int age;
float salary;
char grade;

Example

#include <stdio.h>

int main() {
   int age = 28;
   double height = 1.83;

   printf("I am %d years old and %.2f meters tall.\n", age, height);
   
   return 0;
}

Output

The output of the above example will be:

I am 28 years old and 1.83 meters tall.

Explanation

In C language, a variable is a named storage location in the memory which is used to store a value. Before using a variable, we must declare it first. A variable declaration specifies the data type and name of the variable so that the compiler can allocate an appropriate amount of memory for it.

In the above example, we declared two variables age and height and assigned them values. We then printed the values using the printf function.

Use

Variables are used to store data that may change during the execution of a program. They are used to manipulate and store values obtained from user input, calculations done within the program, or data read from files or other external sources.

Important Points

  • C variable names must begin with a letter or an underscore character.
  • Variable names can only contain letters, numbers, or underscores.
  • C is a statically typed language, which means that variables must be declared with a specific data type.
  • Uninitialized variables in C contain undefined values.
  • Variables can be assigned new values at any time during the execution of a program using the assignment operator =.

Summary

C variables are a fundamental concept in computer programming and play an important role in storing and manipulating data in a program. To use a variable, it must be first declared with a data type and name, and a value can then be assigned to it. Understanding how to declare and use variables is essential for writing effective C programs.

Published on: