Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

C – Variable

Lecture Four
C – Variable
C variable is a named location in a memory where a
program can manipulate the data. This location is
used to hold the value of the variable.
The value of the C variable may get change in the
program.
C variable might be belonging to any of the data type
like int, float, char etc.
Variable Definition in C

A variable definition tells the compiler where and how much


storage to create for the variable. A variable definition specifies a
data type and contains a list of one or more variables of that type
as follows −
type variable_list;
Here, type must be a valid C data type including char, w_char,
int, float, double, bool, or any user-defined object;
and variable_list may consist of one or more identifier names
separated by commas. Some valid declarations are shown here −
int i, j, k;
char c, ch;
 float f, salary;
double d;
Variable Definition in C

The line int i, j, k; declares and defines the variables i,


j, and k; which instruct the compiler to create variables
named i, j and k of type int.
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 −
int d = 3, f = 5; // definition and initializing d and f.
char x = 'x'; // the variable x has the value 'x'.
RULES FOR NAMING C VARIABLE:
Variable name must begin with letter or underscore.
Variables are case sensitive
They can be constructed with digits, letters.
No special symbols are allowed other than underscore.
sum, height, _value are some examples for variable
name
Variable Declaration in C
A variable declaration provides assurance to the
compiler that there exists a variable with the given type
and name so that the compiler can proceed for further
compilation without requiring the complete detail
about the variable.
A variable declaration is useful when you are using
multiple files and you define your variable in one of the
files which will be available at the time of linking of the
program. You will use the keyword extern to declare a
variable at any place. Though you can declare a variable
multiple times in your C program, it can be defined only
once in a file, a function, or a block of code.
Variable Declaration in C
Example
Try the following example, where variables have been
declared at the top, but they have been defined and
initialized inside the main function −

#include <stdio.h>
// Variable declaration:
 extern int a, b; extern int c;
extern float f;
int main ()
{
DECLARING & INITIALIZING C VARIABLE:

Variables should be declared in the C program before


to use.
Memory space is not allocated for a variable while
declaration. It happens only on variable definition.
Variable initialization means assigning a value to the
variable.
DECLARING & INITIALIZING C VARIABLE:

Type  Syntax
data_type variable_name;
Variable declaration Example: int x, y, z; char flat, ch;
data_type variable_name = value;
Variable initialization Example: int x = 50, y = 30; char flag = ‘x’,
ch=’l’;
TYPES OF VARIABLES IN C PROGRAM

TWO TYPES OF VARIABLES IN C PROGRAM


THEY ARE,

Local variable
Global variable
DIFFERENCE BETWEEN VARIABLE DECLARATION &
DEFINITION IN C:

Variable definition
Variable declaration

Declaration tells the compiler about Definition allocates memory for the
data type and size of the variable. variable.
Variable can be declared many times in It can happen only one time for a
a program. variable in a program.
The assignment of properties and Assignments of storage space to a
identification to a variable. variable.
Operators and expressions
The symbols which are used to perform logical and
mathematical operations in a C program are called C
operators.
These C operators join individual constants and
variables to form expressions.
Operators, functions, constants and variables are
combined together to form expressions.
Consider the expression A + B * 5. where, +, * are
operators, A, B  are variables, 5 is constant and A + B * 5
is an expression.
TYPES OF C OPERATORS:
C language offers many types of operators. They are,

 Arithmetic operators
 Assignment operators
 Relational operators
 Logical operators
 Bit wise operators
 Conditional operators (ternary operators)
 Increment/decrement operators
 Special operators
C – Arithmetic Operators
ARITHMETIC OPERATORS IN C:
C Arithmetic operators are used to perform
mathematical calculations like addition, subtraction,
multiplication, division and modulus in C programs.

Arithmetic Operators/Operation Example

+ (Addition) A+B
– (Subtraction) A-B
* (multiplication) A*B
/ (Division) A/B
% (Modulus) A%B
C – Assignment Operators
ASSIGNMENT OPERATORS IN C:
In C programs, values for the variables are assigned
using assignment operators.
For example, if the value “10” is to be assigned for the
variable “sum”, it can be assigned as “sum = 10;”
There are 2 categories of assignment operators in C
language. They are,
1. Simple assignment operator ( Example: = )
2. Compound assignment operators ( Example: +=, -=,
*=, /=, %=, &=, ^= )
ASSIGNMENT OPERATORS IN C

Operators Example/Description
sum = 10;
= 10 is assigned to variable sum
sum += 10;
+= This is same as sum = sum + 10

-= sum -= 10;
This is same as sum = sum – 10
sum ^= 10;
^= This is same as sum = sum ^ 10
ASSIGNMENT OPERATORS IN C (CONT’D)

*= sum *= 10;
This is same as sum = sum * 10

/= sum /= 10;
This is same as sum = sum / 10
sum %= 10;
%= This is same as sum = sum % 10

&= sum&=10;
This is same as sum = sum & 10
C – Relational Operators
RELATIONAL OPERATORS IN C:
Relational operators are used to find the relation
between two variables. i.e. to compare the values of
two variables in a C program.
C – Relational Operators

Operators Example/Description
> x > y (x is greater than y)
< x < y (x is less than y)
>= x >= y (x is greater than or equal to y)
<= x <= y (x is less than or equal to y)
== x == y (x is equal to y)
!= x != y (x is not equal to y)
C – Logical Operators
LOGICAL OPERATORS IN C:

These operators are used to perform logical operations


on the given expressions.
There are 3 logical operators in C language. They are,
logical AND (&&),
logical OR (||)
 logical NOT (!).

You might also like