A variable is defined as a meaningful name given to a data storage location in computer memory. When using a variable, we actually refer to address of the memory where the data is stored.
VARIABLES
A
variable is defined as a meaningful
name given to a data storage location in computer memory. When using a
variable, we actually refer to address of the memory where the data is stored.
C language supports two basic kinds of variables-numeric and character.
Numeric
variables can be used to store either integer values or floating point values.
While an integer value is a whole number without a fraction part or decimal
point, a floating point value can have a decimal point.
Numeric
variables may also be associated with modifiers like short, long, signed, and unsigned.
The difference between signed and unsigned numeric variables is that signed
variables can be either negative or positive but unsigned variables can only be
positive. Therefore, by using an unsigned variable we can increase the maximum
positive range. When we do not specify the signed/
unsigned modifier, C language automatically takes it as a signed variable.
To declare an unsigned variable, the unsigned modifier must be explicitly added
during the declaration of the variable.
Character
variables are just single characters enclosed within single quotes. These
characters could be any character from the ASCII character set-letters ('a',
'A'), numerals ('2'), or special characters ('&'). In C, a number that is
given in single quotes is not the same as a number without them. This is
because 2 is treated as an integer value but '2' is a considered character not
an integer.
Each
variable to be used in the program must be declared. To declare a variable,
specify the data type of the variable followed by its name. The data type
indicates the kind of values that the variable will store. Variable names
should always be meaningful and must reflect the purpose of their usage in the
program. The memory location of the variable is of importance to the compiler
only and not to the programmer. Programmers must only be concerned with
accessing data through their symbolic names. In C, variable declaration always
ends with a semicolon, for example:
int emp_num;
float salary;
char grade;
double balance_amount;
unsigned short int acc_no;
In
C variables can be declared at any place in the program but two things must be
kept in mind. First, variables should be declared before using them. Second,
variables should be declared closest to their first point of use to make the
source code easier to maintain.
C
allows multiple variables of the same type to be declared in one statement. So
the following statement is absolutely legal in C.
float temp_in_celsius, temp_in_farenheit;
In
C variables are declared at three basic places as follows:
• When a variable is declared inside a function it is known as a local variable.
• When a variable is declared in the definition of function parameters it is known
as formal parameter (we will study
this in the chapter on Functions). of boon
• When the variable is declared outside all functions, it is known as a global variable.
Note
A
variable cannot be of type void.
While
declaring the variables, we can also initialize them with some value. For
example,
int emp_num = ?;
float salary = 2156.35;
char grade = 'A';
double balance_amount = 100000000;
The
initializer applies only to the variable defined immediately before it. Therefore,
the statement
int count, flag = 1;
initializes
the variable flag and not count. If you want both the
variables to be declared in a single statement then write,
int count = 0, flag = 1;
When
variables are declared but not initialized they usually contain garbage values
(there are exceptions to this that we will study later).
Programming in C: Unit I (b): Introduction to C : Tag: : C Program - Variables
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation
Professional English II
HS3251 2nd Semester 2021 Regulation | 2nd Semester Common to all Dept 2021 Regulation
Statistics and Numerical Methods
MA3251 2nd Semester 2021 Regulation M2 Engineering Mathematics 2 | 2nd Semester Common to all Dept 2021 Regulation
Engineering Graphics
GE3251 eg 2nd semester | 2021 Regulation | 2nd Semester Common to all Dept 2021 Regulation
Physics for Electrical Engineering
PH3202 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation
Basic Civil and Mechanical Engineering
BE3255 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation
Electric Circuit Analysis
EE3251 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation
Physics for Electronics Engineering
PH3254 - Physics II - 2nd Semester - ECE Department - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation
Electrical and Instrumentation Engineering
BE3254 - 2nd Semester - ECE Dept - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation
Circuit Analysis
EC3251 - 2nd Semester - ECE Dept - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation
Materials Science
PH3251 2nd semester Mechanical Dept | 2021 Regulation | 2nd Semester Mechanical Dept 2021 Regulation
Basic Electrical and Electronics Engineering
BE3251 2nd semester Mechanical Dept | 2021 Regulation | 2nd Semester Mechanical Dept 2021 Regulation
Physics for Civil Engineering
PH3201 2021 Regulation | 2nd Semester Civil Dept 2021 Regulation
Basic Electrical, Electronics and Instrumentation Engineering
BE3252 2021 Regulation | 2nd Semester Civil Dept 2021 Regulation
Physics for Information Science
PH3256 2nd Semester CSE Dept | 2021 Regulation | 2nd Semester CSE Dept 2021 Regulation
Basic Electrical and Electronics Engineering
BE3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation