what is a variable and how to declare variable in C Programming

In this tutorial we’re gonna see, what is a variable and how to declare variable in C, how we can assign the value to that variable and how we can retrieve the values from that variable.

what is a variable and how to declare variable in C Programming ?

A variable is a name given to a memory location. In C if you want to use a variable you have to declare it so first
we’re gonna see how we can declare a variable. First we have to write the type of data you want to store in that
variable. If you want to store an integer data in that variable then you have to write int or if you are storing a floating-point number then you have to write float and if are adding a character data or a character then you have to write char, that is called the data type. It just indicates what type of data will be stored in that variable.

how to declare variable in c

Look at the above image, let me show you the syntax first it’s gonna be data type, then the name of the variable then a semicolon. That’s it this is how you can declare a variable.

How to Declare Variable in C ?

Refer below figure while studying below paragraphs

how to declare varibale in C

Here for the demonstration purpose we’re gonna create three variables. First I want to store my age so my age is a whole number. You know it’s an integer value that’s why I’m gonna write ‘int’. That’s because you know I’m storing integer data in that variable then the way of name so I’m gonna call it H if you want to give  it any other name you can do that no problem.

Then I want to store my weight so my weight is fifty point five. It’s a floating point data, that’s why I’m gonna write ‘float’ then weight

The next one is I want to show my sex that’s why I’m gonna write ‘char’. The name of the variable is sex. Now we have three variables or we have defined three variables here.

How to assign Value to Variables  ?

Now what we wanna do is we want to add  in some value to these variables.  We are going to use the assignment operator. Please refer below image.

assign value to variable in c

First you have to write the variable, here first we want to assign a value to this variable, ‘age’ so write age then the assignment operator then the value you want to store in that variable my age that is 28 that’s it a semicolon. Next we want to store my weight it’s going to be weight and as an operator then 68.5 . next I am describe my sex, it’s gonna be sex equal to in single quotes  M that’s because you know as a character constant is written in single quotes.

print statement in C

The last thing we want to do in this tutorial is we just want to print out the values stored in its variables. For that we use printf statement and in printf statement we can use the format specifier, which specifies the type of data is referenced.

First we want to print the data or value stored in this age variable so that’s why you know this ad sub type
integer you have to write the format specifiers %d then leave a space.

Then we want to print the data which is stored in this variable weight and this weight variable is type float that’s why we have to write the format specifier %f then  leave a space.

Then we want to print the data stored in this variable sex and this variable sets is of type character that’s why we need to specify format specifiers %c so then we need to refer the variables corresponding to this format specifiers.

This is it this is about the variable and how to declare variable in C Programming.

Write First Program in C Language

Constants Variables and Keywords in C programming

Computer Programming and types of Programming Languages

 

Comments

comments