How we define and initialize a variable? In c language
Answers
Answer:
To define a function we must provide the datatype and the variable name. We can even define multiple variables of same datatype in a single line by using comma to separate them. int a; float b, c; Initializing a variable means to provide it with a value.
When we want to store any information(data) on our computer/laptop, we store it in the computer's memory space. Instead of remembering the complex address of that memory space where we have stored our data, our operating system provides us with an option to create folders, name them, so that it becomes easier for us to find it and access it.
Similarly, in C language, when we want to use some data value in our program, we can store it in a memory space and name the memory space so that it becomes easier to access it.
The naming of an address is known as variable. Variable is the name of memory location. Unlike constant, variables are changeable, we can change value of a variable during execution of a program. A programmer can choose a meaningful variable name. Example : average, height.
It tells the compiler what the variable name is.
It specifies what type of data the variable will hold.
Until the variable is defined the compiler doesn't have to worry about allocating memory space to the variable.
Declaration is more like informing the compiler that there exist a variable with following datatype which is used in the program.
A variable is declared using the extern keyword, outside the main() function.
Defining a variable means the compiler has to now assign a storage to the variable because it will be used in the program. It is not necessary to declare a variable using extern keyword, if you want to use it in your program. You can directly define a variable inside the main() function and use it.
To define a function we must provide the datatype and the variable name. We can even define multiple variables of same datatype in a single line by using comma to separate them.
int a;
float b, c;
Initializing a variable means to provide it with a value. A variable can be initialized and defined in a single statement
You must be thinking how does this printf() works, right? Do not worry, we will learn about it along with other ways to input and output datain C language in the next tutorial.
Difference between Variable and Identifier?
An Identifier is a name given to any variable, function, structure, pointer or any other entity in a programming language. While a variable, as we have just learned in this tutorial is a named memory location to store data which is used in the program.
Identifier Variable
Identifier is the name given to a variable, function etc. While, variable is used to name a memory location which stores data.
An identifier can be a variable, but not all indentifiers are variables. All variable names are identifier