Variable in C language

A variable is a storage location which has given some name. 

In the world of programming, a variable is like a labeled box where you can store and manipulate information. Just as you label a box "toys" to keep your toys organized, in C language, you create variables to store and manage data within a program.

Declaring Variables:

To use a variable, you first declare it. Declaration is like telling the computer, "Hey, I'm going to need a box to store something, and I'll call it by this name." In C, the declaration follows a specific pattern:

data_type variable_name;

data_type: This is the type of data the variable will hold. It could be an int for whole numbers, float for numbers with decimals, char for characters, and so on.

variable_name: This is the name you give to your box. It should follow the rules we'll discuss shortly.

Let's look at an example:

int age;        // Declaring a variable 'age' that will store whole numbers

float height;   // Declaring a variable 'height' that will store numbers with decimals

char initial;   // Declaring a variable 'initial' that will store a single character

Now, these variables are like empty boxes, waiting for you to put something inside.

Assigning Values to Variables:

After declaring a variable, you can assign a value to it. It's like saying, "Put this specific thing in that labeled box." In C, you use the assignment operator = for this:

age = 25;           // Putting the value 25 into the 'age' box

height = 5.8;       // Putting the value 5.8 into the 'height' box

initial = 'A';      // Putting the character 'A' into the 'initial' box

Now, each variable holds a specific value.

Rules for Declaration of Variables:

Start with a Letter or Underscore:

Variable names must begin with a letter (uppercase or lowercase) or an underscore "_".

Can Have Letters and Numbers:

After the first character, you can use letters, numbers, or underscores.

No Spaces or Special Characters:

Variable names can't have spaces or special characters (except underscores). For example, my_var is okay, but my var is not.

Case Sensitive:

C is case-sensitive, so age and Age are considered different variables.

Avoid Keywords:

Don't use words that C already uses for special things (like int or if). Those are reserved, and C already knows what they mean.





Post a Comment

0 Comments