Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information.
Their sole purpose is to label and store data in memory. This data can then be used throughout your program.
Assigning Value to Variables
Naming variables is known as one of the most difficult tasks in computer programming. When you are naming variables, think hard about the names. Try your best to make sure that the name you assign your variable is accurately descriptive and understandable to another reader. Sometimes that other reader is yourself when you revisit a program that you wrote months or even years earlier.
When you assign a variable, you use the = symbol. The name of the variable goes on the left and the value you want to store in the variable goes on the right.
Variables can be initialized (assigned an initial value) in their declaration. The initializer consists of an equal sign followed by a constant expression as follows –
type variable_name = value;
Some examples are –
extern int d = 3, f = 5; // declaration of d and f.
int d = 3, f = 5; // definition and initializing d and f.
byte z = 22; // definition and initializes z.
char x = 'x'; // the variable x has the value 'x'.
For definition without an initializer: variables with static storage duration are implicitly initialized with NULL (all bytes have the value 0); the initial value of all other variables are undefined.
There are 2 types of variable
1. Global-->globally accepted throughout the program(Declare outside all the function)
2.Local-->Just can be used inside the program means it can be used inside small functions closed by braces({}).
Simple Program Code to understand variables:
#include<stdio.h>
int var=10; /* Assigning 10 to Var means instead of writing 10 use Var..and int means its of int type*/
void message();
void main()
{
int var=20;
{
int var = 30;
printf("%d ",var); /* 30 is local to this function */
}
printf("%d ",var);/* 20 is local to this */
message();
}
void message()
{
printf("%d ",var); /* 10 is local to this */
}
So, output will be 30 20 10
⇒C Compiler to run the above Code:https://goo.gl/Z24q5c
Lvalues and Rvalues in C
There are two kinds of expressions in C −
· lvalue − Expressions that refer to a memory location are called "lvalue" expressions. An lvalue may appear as either the left-hand or right-hand side of an assignment.
· rvalue − The term rvalue refers to a data value that is stored at some address in memory. An rvalue is an expression that cannot have a value assigned to it which means an rvalue may appear on the right-hand side but not on the left-hand side of an assignment.
Variables are lvalues and so they may appear on the left-hand side of an assignment. Numeric literals are rvalues and so they may not be assigned and cannot appear on the left-hand side. Take a look at the
following valid and invalid statements −
int g = 20; // valid statement
10 = 20; // invalid statement; would generate compile-time error
Then, how to access global variable inside main function?
ReplyDeleteJust declare global variable outside all function.
Deletevoid message()
{
printf("%d ",var); /* 10 is local to this */
}
here var variable is not declared inside the function.So,its taking 10 in var as var=10 is declared outside all the function
online coding classes for kids We are really grateful for your blog post. You will find a lot of approaches after visiting your post. I was exactly searching for. Thanks for such post and please keep it up. Great work.
ReplyDeleteThanks Kinza... if you find this helpful please share with others
Delete