C – Constants and Variables

       www.eshikshak.co.in
C - Constants

                Constants



    Primary                           Secondary
   Constants                          Constants


Integer Constants                       Array
 Real Constants                         Pointer
    Character                          Structure
    Constants                           Union
                                       Enum. etc

                www.eshikshak.co.in
Rules for Constructing Integer Constants
   An integer constant must have at least one digit.
   It must not have a decimal point.
      1, 2, -3, 99, 1000 are valid integer constant
      1.3, 2.00, 0.01, -99, 100.9 are invalid integer constant
   It can be either positive or negative.
   If no sign precedes an integer constant it is assumed to be
    positive.
   No commas or blanks are allowed within an integer constant.
   The allowable range for integer constants is -32768 to 32767.
   Ex.: 426
       +782
       -8000
       -7605




                          www.eshikshak.co.in
Rules for Constructing Real Constants
  A real constant must have at least one digit.
  It must have a decimal point.
  It could be either positive or negative.
  Default sign is positive.
  No commas or blanks are allowed within a
   real constant.
 • Ex.: +325.34
         426.0
          -32.76
          -48.5792
Rules for Constructing Character Constants


 • A characterspecial symbolsingle alphabet, asingle digit
   or a single
               constant is a
                             enclosed within
                                               single

   inverted commas.
 • Both the inverted commas should point to the left. For
   example, ’A’ is a valid character constant whereas ‘A’
   is not.
 • The maximum length of a character constant can be 1
   character.
 Ex.: 'A'
      'I'
      '5'
      '='
Variable
 Variables in C have the same meaning as variables in
  algebra. That is, they represent some unknown, or
  variable, value.


   •x=a+b
   • z + 2 = 3(y - 5)
 Remember that variables in algebra are represented by a
  single alphabetic character




                      www.eshikshak.co.in
Variable
 A variable is a used to store values. It has memory
  location and can store single value at a time.
 A variable is a data name used for storing a data
  value. Its value may be changed during the
  program execution.
 The variable value keeps on changing during the
  execution of the program.




                    www.eshikshak.co.in
Naming Variables
• Variables in C may be given representations
  containing multiple characters. But there are
  rules for these representations.
• Variable names (identifiers) in C
  o May only consist of letters, digits, and
    underscores
  o May be as long as you like, but only the first 31
    characters are significant
  o May not begin with a digit
  o May not be a C reserved word (keyword)
Naming Conventions
 C programmers generally agree on the
  following conventions for naming variables.
 o   Begin variable names with lowercase letters
 o
   Use   meaningful            identifiers
 o Separate “words” within identifiers with
   underscores or mixed upper and lower case.
 o Examples: surfaceArea surface_Area
   surface_area
 o Be consistent!
Rules for Defining Variables
•   A variable must begin with a character or an underscore without spaces.
    The underscore is treated as one type of characters.
     – It is advised that the variable names should not start with underscore because
       library routines mostly use such variable names.
•   The length of the variable varies from compiler to compiler. Generally,
    most of the compilers support eight characters excluding extension.
     – ANSI standard recognizes the maximum length of a variable up to 31
       characters.
•   The variable should not be a C keyword.
•   The variable names may be a combination of uppercase and lowercase
    characters.
•   The variable name should not start with a digit.
•   Blanks and commas are not permitted within a variable name.

                                   www.eshikshak.co.in
C Data Types
                             C Data Types


Derived Data Type         Basic Data Type              User Defined Data Type


           Pointers                                     Pointers
           Functions                                    Functions
           Arrays                                       Arrays




               Integer                Floating Point                  void


            char                   float
            int                    double

                              www.eshikshak.co.in
C Data Types
Data Type       Size(bytes)                  Range            Format String
char                1                     -128 to 127              %c
unsigned char       1                       0 to 255               %c
short or int        2                  -32768 to 32767          %i or %d
unsigned int        2                     0 to 65535               %u
long                4          -2147483648 to 2147483647          %ld
unsigned long       4                 0 to 4294967295             %lu
float               4               3.4 e-38 to 3.4 e+38        %f or %g
double              8              1.7 e-308 to 1.7 e+308          %lf
long double         10           3.4 e-4932 to 1.1 e + 4932        %lf
enum                2*                 -32768 to 32767             %d




                              www.eshikshak.co.in
C Data types
 • Integer Data Type
       – int, short and long
       – All C compilers offers different integer data types.
Short Integer                                  Long Intger
Occupies 2 bytes in memory                     Occupies 4 bytes in memory
Range : -32768 to 32767                        Range : -2147483648 to 2147483647
Program runs faster                            Program runs slower
Format Specifier : %d or %i                    Format Specifier : %ld
Example :                                      Example :
int a = 2;                                     long b = 123456
short int b = 2;                               long int c=1234567
Note : when variable is declared without short or long keyword, the default is short-
signed int.
                                     www.eshikshak.co.in
C Data Types
 Difference between signed and unsigned integers
Signed Integer                         Unsigned Integer
Occupies 2 bytes in memory                     Occupies 4 bytes in memory
Range : -32768 to 32767                        Range : 0 to 65535
Format Specifier : %d or %i                    Format Specifier : %u
By default signed int is short signed int      By default unsigned int is short unsigned
                                               int
There are also long signed integers having     There are also long unsigned int with
range from -2147483648 to 2147483647           range 0 to 4294967295
Example :                                      Example:
int a=2;                                       unsigned long b=567898;
long int b=2;                                  unsigned short int c=223;
                                                When a variable is declared as unsigned
                                                the negative range of the data type is
                                                transferred to positive, i.e. doubles the
                                                largest size of possible value. This is due to
                                                delcaring unsigned int, the 16th bit is free
                                                and not used to store the sign of the
                                       www.eshikshak.co.in
                                                number
C Data Types
Signed Character                          Unsigned Character
Occupies 1 bytes in memory                Occupies 1 bytes in memory
Range : -128 to 127                       Range : 0 to 255
Format Specifier : %c                     Format Specifier : %c
When printed using %d format specifier    When printed using %d format specifier,
prints ASCII character                    pirnts ASCII character
char ch=‘b’                               unsigned char = ‘b’;




                                  www.eshikshak.co.in
C Data Type
Floating                                Double Floating
Occupies 4 bytes in memory              Occupies 8 bytes in memory
Range : 3.4e-38 to +3.4e+38             Range : 1.7 e-308 to +1.73+308;
Format String : %f                      Format String : %lf
Example :                               Example :
float a;                                double y;
                                        There also exist long double
                                        having ranged 3.4 e-4932 to 1.1e +
                                        4932 and occupies 10 bytes in
                                        memory

                                        Example :

                                        long double k;


                              www.eshikshak.co.in
Initialization Variables
•  Variable declared can be assigned operator ῾=᾽. The declaration and
   initialization can also be done in the same line.
Syntax :
   variable_name = constant;
  or
  data_type variable_name = constant;
Example :
   x = 5; where is an integer variable.
Example :
   int y = 4;
Example :
   int x,y,z;

                               www.eshikshak.co.in
Dynamic Initialization
•   The initialization of variable at run time is called dynamic initialization.
    Dynamic refers to the process during execution.
•   In C initialization can be done at any place in the program, however the
    declaration should be done at the declaration part only.
•   Example :
     void main()
     {
         int r=2;
         float area=3.14*r*r;
         clrscr();
          printf(“Area = %g”, area);
     }
     OUTPUT:
     Area=12.56;
                                       www.eshikshak.co.in
Example :

Type Modifiers                   void main()
                                 {
                                    short t = 1;
• The keywords signed,              long k = 54111;
                                     unsigned u = 10;
  unsigned, short and
                                     signed j = -10;
  long are type modifiers.
                                     clrscr();
• A     type     modifier             printf(“n t=%d”,t)
  changes the meaning of              printf(“n k=%ld”, k);
  basic data type and                 printf(“n u=%u, u);
                                      printf(“n j=%d”, j);
  produces a new type.
                                 }
• Each of these type                OUTPUT
  modifiers is applicable           t = 1;
                                     k = 54111;
  to the basic data type
                                     u = 10;
  int.                               j = -10
                       www.eshikshak.co.in
• Type Conversion
• Wrapping Around
• Constant and Volatile
  Variables



             www.eshikshak.co.in

Mesics lecture 3 c – constants and variables

  • 1.
    C – Constantsand Variables www.eshikshak.co.in
  • 2.
    C - Constants Constants Primary Secondary Constants Constants Integer Constants Array Real Constants Pointer Character Structure Constants Union Enum. etc www.eshikshak.co.in
  • 3.
    Rules for ConstructingInteger Constants  An integer constant must have at least one digit.  It must not have a decimal point.  1, 2, -3, 99, 1000 are valid integer constant  1.3, 2.00, 0.01, -99, 100.9 are invalid integer constant  It can be either positive or negative.  If no sign precedes an integer constant it is assumed to be positive.  No commas or blanks are allowed within an integer constant.  The allowable range for integer constants is -32768 to 32767.  Ex.: 426 +782 -8000 -7605 www.eshikshak.co.in
  • 4.
    Rules for ConstructingReal Constants  A real constant must have at least one digit.  It must have a decimal point.  It could be either positive or negative.  Default sign is positive.  No commas or blanks are allowed within a real constant. • Ex.: +325.34 426.0 -32.76 -48.5792
  • 5.
    Rules for ConstructingCharacter Constants • A characterspecial symbolsingle alphabet, asingle digit or a single constant is a enclosed within single inverted commas. • Both the inverted commas should point to the left. For example, ’A’ is a valid character constant whereas ‘A’ is not. • The maximum length of a character constant can be 1 character. Ex.: 'A' 'I' '5' '='
  • 6.
    Variable  Variables inC have the same meaning as variables in algebra. That is, they represent some unknown, or variable, value. •x=a+b • z + 2 = 3(y - 5)  Remember that variables in algebra are represented by a single alphabetic character www.eshikshak.co.in
  • 7.
    Variable  A variableis a used to store values. It has memory location and can store single value at a time.  A variable is a data name used for storing a data value. Its value may be changed during the program execution.  The variable value keeps on changing during the execution of the program. www.eshikshak.co.in
  • 8.
    Naming Variables • Variablesin C may be given representations containing multiple characters. But there are rules for these representations. • Variable names (identifiers) in C o May only consist of letters, digits, and underscores o May be as long as you like, but only the first 31 characters are significant o May not begin with a digit o May not be a C reserved word (keyword)
  • 9.
    Naming Conventions  Cprogrammers generally agree on the following conventions for naming variables. o Begin variable names with lowercase letters o Use meaningful identifiers o Separate “words” within identifiers with underscores or mixed upper and lower case. o Examples: surfaceArea surface_Area surface_area o Be consistent!
  • 10.
    Rules for DefiningVariables • A variable must begin with a character or an underscore without spaces. The underscore is treated as one type of characters. – It is advised that the variable names should not start with underscore because library routines mostly use such variable names. • The length of the variable varies from compiler to compiler. Generally, most of the compilers support eight characters excluding extension. – ANSI standard recognizes the maximum length of a variable up to 31 characters. • The variable should not be a C keyword. • The variable names may be a combination of uppercase and lowercase characters. • The variable name should not start with a digit. • Blanks and commas are not permitted within a variable name. www.eshikshak.co.in
  • 11.
    C Data Types C Data Types Derived Data Type Basic Data Type User Defined Data Type  Pointers  Pointers  Functions  Functions  Arrays  Arrays Integer Floating Point void  char  float  int  double www.eshikshak.co.in
  • 12.
    C Data Types DataType Size(bytes) Range Format String char 1 -128 to 127 %c unsigned char 1 0 to 255 %c short or int 2 -32768 to 32767 %i or %d unsigned int 2 0 to 65535 %u long 4 -2147483648 to 2147483647 %ld unsigned long 4 0 to 4294967295 %lu float 4 3.4 e-38 to 3.4 e+38 %f or %g double 8 1.7 e-308 to 1.7 e+308 %lf long double 10 3.4 e-4932 to 1.1 e + 4932 %lf enum 2* -32768 to 32767 %d www.eshikshak.co.in
  • 13.
    C Data types • Integer Data Type – int, short and long – All C compilers offers different integer data types. Short Integer Long Intger Occupies 2 bytes in memory Occupies 4 bytes in memory Range : -32768 to 32767 Range : -2147483648 to 2147483647 Program runs faster Program runs slower Format Specifier : %d or %i Format Specifier : %ld Example : Example : int a = 2; long b = 123456 short int b = 2; long int c=1234567 Note : when variable is declared without short or long keyword, the default is short- signed int. www.eshikshak.co.in
  • 14.
    C Data Types Difference between signed and unsigned integers Signed Integer Unsigned Integer Occupies 2 bytes in memory Occupies 4 bytes in memory Range : -32768 to 32767 Range : 0 to 65535 Format Specifier : %d or %i Format Specifier : %u By default signed int is short signed int By default unsigned int is short unsigned int There are also long signed integers having There are also long unsigned int with range from -2147483648 to 2147483647 range 0 to 4294967295 Example : Example: int a=2; unsigned long b=567898; long int b=2; unsigned short int c=223; When a variable is declared as unsigned the negative range of the data type is transferred to positive, i.e. doubles the largest size of possible value. This is due to delcaring unsigned int, the 16th bit is free and not used to store the sign of the www.eshikshak.co.in number
  • 15.
    C Data Types SignedCharacter Unsigned Character Occupies 1 bytes in memory Occupies 1 bytes in memory Range : -128 to 127 Range : 0 to 255 Format Specifier : %c Format Specifier : %c When printed using %d format specifier When printed using %d format specifier, prints ASCII character pirnts ASCII character char ch=‘b’ unsigned char = ‘b’; www.eshikshak.co.in
  • 16.
    C Data Type Floating Double Floating Occupies 4 bytes in memory Occupies 8 bytes in memory Range : 3.4e-38 to +3.4e+38 Range : 1.7 e-308 to +1.73+308; Format String : %f Format String : %lf Example : Example : float a; double y; There also exist long double having ranged 3.4 e-4932 to 1.1e + 4932 and occupies 10 bytes in memory Example : long double k; www.eshikshak.co.in
  • 17.
    Initialization Variables • Variable declared can be assigned operator ῾=᾽. The declaration and initialization can also be done in the same line. Syntax : variable_name = constant; or data_type variable_name = constant; Example : x = 5; where is an integer variable. Example : int y = 4; Example : int x,y,z; www.eshikshak.co.in
  • 18.
    Dynamic Initialization • The initialization of variable at run time is called dynamic initialization. Dynamic refers to the process during execution. • In C initialization can be done at any place in the program, however the declaration should be done at the declaration part only. • Example : void main() { int r=2; float area=3.14*r*r; clrscr(); printf(“Area = %g”, area); } OUTPUT: Area=12.56; www.eshikshak.co.in
  • 19.
    Example : Type Modifiers void main() { short t = 1; • The keywords signed, long k = 54111; unsigned u = 10; unsigned, short and signed j = -10; long are type modifiers. clrscr(); • A type modifier printf(“n t=%d”,t) changes the meaning of printf(“n k=%ld”, k); basic data type and printf(“n u=%u, u); printf(“n j=%d”, j); produces a new type. } • Each of these type OUTPUT modifiers is applicable t = 1; k = 54111; to the basic data type u = 10; int. j = -10 www.eshikshak.co.in
  • 20.
    • Type Conversion •Wrapping Around • Constant and Volatile Variables www.eshikshak.co.in