Saturday, January 15, 2011

Variables, Datatypes and Constants



Variable:


      For a computer in order to compute something it all needs is some input, some instructions which tells it what to do with that data. The Input can be given in variety of ways. Although every input has to be remembered by the computer, in-order perform some operations on it. Memory devices are used to achieve this. You can specify some statements which instructs the computer to store some value in memory.

    Before we store some value in the memory, its necessary to first reserve some space in memory to store the value and then store the value.

   In C we have concept called variables. Variables is nothing but a container which can hold value. In fact actually the value is stored at some location in memory. Therefore whenever specific value is needed then, we refer to that location using the name of the variable.



   A sample code is given below to declare a variable,

                      int var1;

   Here, var1 is the variable declared of type int(if you don't get it don't worry, keyword int will be discussed later in next few topics)


   General format is : data_type variable name;



Note : You can declare multiple variables in a single statement, this can be done by separating variable names by a comma(,)


   Once the variable is declared as shown above one can store and perform some operations on it. There are few restriction on the variable name, which is discussed below. Also few data types are discussed below. If you are familiar with these then you can move further to my next post.




Variable Name:
  • First letter has to be a character or under score 
  • Its recommended not to begin with under score – Because its normally used by libraries. 
  • Next characters followed by first character can be a normal chars, under score or digits 
  • Note that C is case sensitive language, therefore extra care to be taken while coding 
  • Traditionally variable names are lower case 
  • constant names are usually upper case 
  • keywords are reserved words cant be used 
  • Meaningful names are recommended, useful while analyzing the code and debugging 
  • Usually shorter names are used for local variables(usually loop index), longer names are used for other variables(normal and external variables)




Data-types :   
  • C has only few data types 
  1. char  (single byte) 
  2. int
  3. float  (single precision floating point)
  4. long  (double precision floating point)
  • In addition to these data types, number of qualifiers are present, that can be applied to these basic data types.
Qualifiers :
  • Qualifier : short, long
  • Eg : short int counter;
             long int counter;
  • However keyword int can be omitted,.. I.e, keyword int is optional if these qualifiers are use
  • Intention of using short, long is to provide different length of int..
  • Often short 16 bits, long 32 bits,
  • int either 16 or 32 bits  
  • Compiler is free to choose the size,.. But with few restrictions  
  1. size of short and int are at least 16 bits
  2. size of long is at least 32 bits
  3. size of short is no longer than int,
  4. similarly int is no longer than long, i.e, short  < =  int  < =  long  
Note : Here size is nothing but the allocated memory when variable of that type is declared
  • Qualifier : signed, unsigned
  • This qualifier can be applied to char or any integer 
  • variable of type unsigned can accept values of either positive or zero
  • similarly signed variable can accept either of positive or zero or negative
  • I.e signed char : -128 to +127 (Size of char is 1 byte, that is 8 bits)
  • I.e unsigned char : 0 to 255 (Size of char is 1 byte, that is 8 bits)
  • char are signed or unsigned by default,it is machine dependent.



No comments:

Post a Comment