Friday, March 27, 2020

C Variables, Constants and literals


C Variables, Constants and literals 

In this you will learn about variables and rules of naming a variable. You will  learn about different literals in C programming and how to create constant.

Variables

In programming, a variable is a container ( storage data ) to hold data.
To indicate the storage area, each variable should be given a unique name ( identifier ). Variable names are just the symbolic representation of a memory location.
For example
                      int  Play_Score = 94;

Here, Play_score is a variable of  int type. Here, the variable is assigned an integer value 94.
The value of a variable can be changed, hence the name variable.

char ch = 'a';   // some code 
ch = '5';

Rules for for naming a variable 

  • A variable name only have letters ( both uppercase and lowercase), digits and underscore.
  • The first letter of variable should be an either letter or an underscore.
  • There is no rule on how long a variable name (identifier) can be. However you may run into  problems in some compilers if the variables name is longer than 31 character.
Note :    You should always give a meaningful names to variables. 
              For Example  : first_name is better variable name than first_name.

C is a strongly typed language. This means that the variable cannot be changed  once it is declared.
For example :
             int number =5;    // integer variable
             number =5.5;   // Error 
            double number;   // Error
   
Here, The type of number variable is int. You cannot assign a floating-point ( decimal ) value 5.5 to this variable. Also , you cannot redefine the data type of the variable to double. By the way, to store the decimal values in C, you need to declare its types to either double or float.

literals

literals are data used for representing fixed values, They can be used directly in the code.  For example 1, 2.3 'A' etc 

Here, 1, 2.3 and C are literals. Why ? You cannot assign different values to these terms.                      

  • Integers 

An integer is a numeric literal ( associated with numbers ) without any fractional and exponential part. There are three types of integer literal in C programming.

  • Decimal ( base 10 )                                                                                                                          
  • Octal base ( base 8 )
  • Hexadecimal ( base 16) 
For Example 
Decimal : 0, -9, 22 etc
Octal : 021, 077, 033 etc
Hexadecimal : 0x7f, 0x2a, 0x521 etc

In C programming octal starts with 0, and hexadecimal start with a 0x.   

                                                                    

0 comments:

Post a Comment

Recent Posts