Declaration of Variable and Assigning Value to Variables

Declaration and Assignment of Variables in C

In C, variables must be declared before they can be used. A variable declaration specifies the data type and name of the variable, while assigning a value stores data in the variable.


1. Declaring a Variable

Syntax:

data_type variable_name;
  • data_type: The type of data the variable will store (e.g., int, float, char).
  • variable_name: A unique name for the variable.

Example:

int age; // Declares an integer variable named 'age'
float height; // Declares a floating-point variable named 'height'
char grade; // Declares a character variable named 'grade'

2. Assigning a Value to a Variable

Syntax:

variable_name = value;
  • variable_name: The name of the declared variable.
  • value: The data you want to assign to the variable.

Example:

age = 25; // Assigns the value 25 to the variable 'age'
height = 5.9; // Assigns the value 5.9 to the variable 'height'
grade = 'A'; // Assigns the character 'A' to the variable 'grade'

3. Declaring and Assigning a Variable in One Step

You can declare and assign a variable in the same statement.

Syntax:

data_type variable_name = value;

Example:

int age = 25; // Declares and assigns 25 to 'age'
float height = 5.9; // Declares and assigns 5.9 to 'height'
char grade = 'A'; // Declares and assigns 'A' to 'grade'

4. Declaring Multiple Variables

You can declare multiple variables of the same data type in a single statement.

Syntax:

data_type variable1, variable2, variable3;

Example:

int x, y, z; // Declares three integer variables

You can also initialize multiple variables during declaration:

int x = 10, y = 20, z = 30;

Rules for Declaring Variables

  1. Variable names must start with a letter or underscore (_).
  2. Variable names can only contain letters, digits, and underscores.
  3. Keywords cannot be used as variable names (e.g., int, float).
  4. Variable names are case-sensitive (age and Age are different).

Examples of Variable Declaration and Assignment

 

#include <stdio.h>

int main() {
// Declaration
int age; // Declares an integer variable
float height; // Declares a floating-point variable
char grade; // Declares a character variable

// Assigning values
age = 25; // Assigns value to ‘age’
height = 5.9; // Assigns value to ‘height’
grade = ‘A’; // Assigns value to ‘grade’

// Declaration and Assignment in one step
int score = 95;
float weight = 70.5;
char initial = ‘S’;

// Print the variables
printf(“Age: %d\n”, age);
printf(“Height: %.1f\n”, height);
printf(“Grade: %c\n”, grade);
printf(“Score: %d\n”, score);
printf(“Weight: %.1f\n”, weight);
printf(“Initial: %c\n”, initial);

return 0;
}


Output

Age: 25
Height: 5.9
Grade: A
Score: 95
Weight: 70.5
Initial: S

Key Points

  • Variables must be declared before use.
  • You can assign values during declaration or separately.
  • Use meaningful variable names to improve code readability.

Leave a Comment

Your email address will not be published. Required fields are marked *