B.Bhuvaneswaran, AP (SG) / CSE
9791519152
bhuvaneswaran@rajalakshmi.edu.in
Programming Logic
Variables
Variables
Variables Rajalakshmi Engineering College 2
Rajalakshmi Engineering College 2
Variables
 A variable is a data name used for storing a data value. Its value
may be changed during the program execution. It has memory
location and can store a single value at a time.
Variables
Variables Rajalakshmi Engineering College 3
Rajalakshmi Engineering College 3
Examples
Value 143 A 18.75
Variable Name count letter temperature
Type
Integer
(int)
Character
(char)
Floating Point
(float)
Variables
Variables Rajalakshmi Engineering College 4
Rajalakshmi Engineering College 4
Rules for Naming Variables
 A variable name is any combination of alphabets, digits or underscores.
 A variable must begin with a character without spaces but an
underscore is permitted.
 Maximum characters recognized is 31 (compiler dependent). The length
of the variable varies from compiler to compiler.
 Keywords cannot be used as variable names.
 Variable names are case sensitive.
 The variable names may be a combination of uppercase and lowercase
characters. For example, suM and sum are not the same.
 Blanks and commas are not permitted within a variable name.
 No special characters (other than an underscore) are used in a variable
name.
Variables
Variables Rajalakshmi Engineering College 5
Rajalakshmi Engineering College 5
Examples
 Valid Examples:
AVERAGE height subject1 n1
City male_sum fact n
 Invalid Examples:
char price$ group one 786
Variables
Variables Rajalakshmi Engineering College 6
Rajalakshmi Engineering College 6
Declaration of variables
 The declaration of variables must be done before they are used in
the program.
 It tells the compiler what the variable name is and it specifies
what type of data the variable will hold.
 A variable declaration consists of a data type name followed by a
list of one or more variables of that type separated by commas.
 Syntax:
datatype var1, var2, . . . varN;
 where var1, var2, . . .varN are the names of the variables.
Variables
Variables Rajalakshmi Engineering College 7
Rajalakshmi Engineering College 7
Examples
 Valid Examples:
int sum;
int mark1, mark2, mark3;
float basicpay, netpay;
char gender;
 Where int, float and char are the keywords to represent integer,
real and character data values respectively.
Variables
Variables Rajalakshmi Engineering College 8
Rajalakshmi Engineering College 8
Points to Remember
 The variables will contain some garbage value when they are
declared.
Value -123 -75 14246 258 -31.26 28.11 X
Name sum mark1 mark2 mark3 basicpay netpay gender
Variables
Variables Rajalakshmi Engineering College 9
Rajalakshmi Engineering College 9
Assigning Values to Variables
 Values can be assigned to variables using the assignment operator
=.
 Syntax:
variable_name = value;
 An assignment statement implies that the value of the variable on
the left of the ‘equal sign’ is set equal to the value of the quantity
(or expression) on the right.
Variables
Variables Rajalakshmi Engineering College 10
Rajalakshmi Engineering College 10
Assigning the Values
 sum = 100;
 mark1 = 75;
 mark2 = 80;
 mark3 = 85;
 basicpay = 30000.00;
 netpay = basicpay – 5000.00;
 gender = 'M';
Value 100 75 80 85 30000.000000 25000.000000 M
Name sum mark1 mark2 mark3 basicpay netpay Gender
Variables
Variables Rajalakshmi Engineering College 11
Rajalakshmi Engineering College 11
Assigning the Values
 It is also possible to assign a value to a variable at the time the
variable is declared. This takes the following form:
datatype variable_name = value (or) variable (or) expression;
Variables
Variables Rajalakshmi Engineering College 12
Rajalakshmi Engineering College 12
Examples
 int sum = 100;
 int mark1 = 75;
 int mark2 = 80;
 int mark3 = 85;
 float basicpay = 30000.00;
 float netpay = basicpay – 5000.00;
 char gender = 'M';
Value 100 75 80 85 30000.000000 25000.000000 M
Name sum mark1 mark2 mark3 basicpay netpay Gender
Variables
Variables Rajalakshmi Engineering College 13
Rajalakshmi Engineering College 13
Initialization of Variables
 The process of giving an initial value to variables is called
initialization.
 C permits the initialization of more than one variable in one
statement using multiple assignment operators.
Variables
Variables Rajalakshmi Engineering College 14
Rajalakshmi Engineering College 14
Initialization of Variables
 Example:
a = b = c = 0;
x = y = z = 1;
 Assigning the values:
Value 0 0 0 1 1 1
Name a b c x y z
Variables
Variables Rajalakshmi Engineering College 15
Rajalakshmi Engineering College 15
Data Types and Keywords
Data type Keyword
Character char
Unsigned character unsigned char
Signed character signed char
Signed integer signed int (or) int
Signed short integer signed short int (or) short int (or) short
Signed long integer signed long int (or) long int (or) long
Unsigned integer unsigned int (or) unsigned
Unsigned short integer unsigned short int (or) unsigned short
Unsigned long integer unsigned long int (or) unsigned long
Floating point float
Double-precision floating point double
Extended double-precision floating point long double
Variables
Variables Rajalakshmi Engineering College 16
Rajalakshmi Engineering College 16
Points to Remember
 When an adjective (qualifier) short, long or unsigned is used with
a basic data type specifier, C compilers treat the data type as int.
 If we want to declare a character variable as unsigned, then we
must do so using both the terms unsigned char.
Thank You

C Programming-Lecture Notes-03-Variables.pdf

  • 1.
    B.Bhuvaneswaran, AP (SG)/ CSE 9791519152 [email protected] Programming Logic Variables
  • 2.
    Variables Variables Rajalakshmi EngineeringCollege 2 Rajalakshmi Engineering College 2 Variables  A variable is a data name used for storing a data value. Its value may be changed during the program execution. It has memory location and can store a single value at a time.
  • 3.
    Variables Variables Rajalakshmi EngineeringCollege 3 Rajalakshmi Engineering College 3 Examples Value 143 A 18.75 Variable Name count letter temperature Type Integer (int) Character (char) Floating Point (float)
  • 4.
    Variables Variables Rajalakshmi EngineeringCollege 4 Rajalakshmi Engineering College 4 Rules for Naming Variables  A variable name is any combination of alphabets, digits or underscores.  A variable must begin with a character without spaces but an underscore is permitted.  Maximum characters recognized is 31 (compiler dependent). The length of the variable varies from compiler to compiler.  Keywords cannot be used as variable names.  Variable names are case sensitive.  The variable names may be a combination of uppercase and lowercase characters. For example, suM and sum are not the same.  Blanks and commas are not permitted within a variable name.  No special characters (other than an underscore) are used in a variable name.
  • 5.
    Variables Variables Rajalakshmi EngineeringCollege 5 Rajalakshmi Engineering College 5 Examples  Valid Examples: AVERAGE height subject1 n1 City male_sum fact n  Invalid Examples: char price$ group one 786
  • 6.
    Variables Variables Rajalakshmi EngineeringCollege 6 Rajalakshmi Engineering College 6 Declaration of variables  The declaration of variables must be done before they are used in the program.  It tells the compiler what the variable name is and it specifies what type of data the variable will hold.  A variable declaration consists of a data type name followed by a list of one or more variables of that type separated by commas.  Syntax: datatype var1, var2, . . . varN;  where var1, var2, . . .varN are the names of the variables.
  • 7.
    Variables Variables Rajalakshmi EngineeringCollege 7 Rajalakshmi Engineering College 7 Examples  Valid Examples: int sum; int mark1, mark2, mark3; float basicpay, netpay; char gender;  Where int, float and char are the keywords to represent integer, real and character data values respectively.
  • 8.
    Variables Variables Rajalakshmi EngineeringCollege 8 Rajalakshmi Engineering College 8 Points to Remember  The variables will contain some garbage value when they are declared. Value -123 -75 14246 258 -31.26 28.11 X Name sum mark1 mark2 mark3 basicpay netpay gender
  • 9.
    Variables Variables Rajalakshmi EngineeringCollege 9 Rajalakshmi Engineering College 9 Assigning Values to Variables  Values can be assigned to variables using the assignment operator =.  Syntax: variable_name = value;  An assignment statement implies that the value of the variable on the left of the ‘equal sign’ is set equal to the value of the quantity (or expression) on the right.
  • 10.
    Variables Variables Rajalakshmi EngineeringCollege 10 Rajalakshmi Engineering College 10 Assigning the Values  sum = 100;  mark1 = 75;  mark2 = 80;  mark3 = 85;  basicpay = 30000.00;  netpay = basicpay – 5000.00;  gender = 'M'; Value 100 75 80 85 30000.000000 25000.000000 M Name sum mark1 mark2 mark3 basicpay netpay Gender
  • 11.
    Variables Variables Rajalakshmi EngineeringCollege 11 Rajalakshmi Engineering College 11 Assigning the Values  It is also possible to assign a value to a variable at the time the variable is declared. This takes the following form: datatype variable_name = value (or) variable (or) expression;
  • 12.
    Variables Variables Rajalakshmi EngineeringCollege 12 Rajalakshmi Engineering College 12 Examples  int sum = 100;  int mark1 = 75;  int mark2 = 80;  int mark3 = 85;  float basicpay = 30000.00;  float netpay = basicpay – 5000.00;  char gender = 'M'; Value 100 75 80 85 30000.000000 25000.000000 M Name sum mark1 mark2 mark3 basicpay netpay Gender
  • 13.
    Variables Variables Rajalakshmi EngineeringCollege 13 Rajalakshmi Engineering College 13 Initialization of Variables  The process of giving an initial value to variables is called initialization.  C permits the initialization of more than one variable in one statement using multiple assignment operators.
  • 14.
    Variables Variables Rajalakshmi EngineeringCollege 14 Rajalakshmi Engineering College 14 Initialization of Variables  Example: a = b = c = 0; x = y = z = 1;  Assigning the values: Value 0 0 0 1 1 1 Name a b c x y z
  • 15.
    Variables Variables Rajalakshmi EngineeringCollege 15 Rajalakshmi Engineering College 15 Data Types and Keywords Data type Keyword Character char Unsigned character unsigned char Signed character signed char Signed integer signed int (or) int Signed short integer signed short int (or) short int (or) short Signed long integer signed long int (or) long int (or) long Unsigned integer unsigned int (or) unsigned Unsigned short integer unsigned short int (or) unsigned short Unsigned long integer unsigned long int (or) unsigned long Floating point float Double-precision floating point double Extended double-precision floating point long double
  • 16.
    Variables Variables Rajalakshmi EngineeringCollege 16 Rajalakshmi Engineering College 16 Points to Remember  When an adjective (qualifier) short, long or unsigned is used with a basic data type specifier, C compilers treat the data type as int.  If we want to declare a character variable as unsigned, then we must do so using both the terms unsigned char.
  • 17.