Variables, Constants, Identifiers and C-Data Types

Variables, Constants, Identifiers and C-Data Types
Topics covered here are:

In C/C++ programs, the most fundamental element is expression. Expression can be formed by combining data and operators. Data can be represented by variables, constants etc. whereas operators are defined in every programming languages. C/C++ languages supports various data types. All these topics are explained below: -

Variable

A variable is a named location in memory. It's value keeps changing during execution according to the program. Variable is used to hold a value during the execution of program. All variables should be declared before they used. Variable makes program easy to understand. Every variable has a data type associated with it which shows the type of data it can hold. To declare a variable, we can use as below:

data_type var_name;

data_type is a valid data type and modifier and var_name is the name of the variable declared.

Constants

Constants refer to the fixed values that a program may not change. Constants can be of any basic data types. Way in which each constant is represented depends upon its type. Constants are also called as literals.

Character constants are enclosed between single quotes. For example, 'a' and '%' are both character constants. Integer constants are described as numbers without fractional components. For example, 10 and –100 are integer constants. Floating-point constants require the decimal point accompany by the number's fractional component. For example, 11.123 is a floating-point constant. C language allows you to use scientific notation for floating-point numbers. For example:-

        C                                                                      

 

         C++


Output:-
      C
 

     C++                             


Constant value starting with 0, is considered as octal number. So if we use as,

int a = 070;

and then want to print the value of a in decimal it is not 70, it is the decimal equivalent of 70 base 8 which is 56. Also following is a syntax error,

int a = 078;

as you are writing an octal constant, but in octal only digits from 0 to 7 can be used, hence 8 is not a valid digit in octal number system.

             C                                                                          

    

            C++   


Output:-
       C

  

     C++                   
  



Identifiers

In C/C++, the names of variables, functions, labels, and various other user-defined items are called identifiers. The length of these identifiers can vary from one to several characters. The first character must be a an underscore or letter, and subsequent characters must be either digitsunderscores, or letters. Here are some correct and incorrect identifier names:


Correct : employee_name, emp_phone, _empid, _number123

Incorrect : 1empno, emp-number, first&last, high..bal


In an identifier, lowercase and uppercase are treated as distinct. Hence COUNTCount and count are three different identifiers.



C - Data Types

C defines four basic primitive data types: character, integer, floating-point, valueless. These are declared using char, int, float, and void respectively. These data types will determine how much space will be allocated to a particular variable in memory of computer while executing a program. Also, they describe how to interpret the bits of a variable, while using them. As everything in memory is binary numbers. But depending on data type specifications, these binary numbers will be interpreted separately.

char: These variables take 1 byte of memory.

int: These variables depend on the word size of the computer. So, generally, they take 16-bits on old 16-bit computers, whereas on modern 32-bit and 64-bit computers they consume 32-bits of memory. you cannot make assumptions about the size of an integer if you want your programs to be portable to the widest range of environments.

float: They are also depend on implementation. Floating point number can be single-precision or double-precision. Both these will be represented by float and double keywords respectively.

void: It is generally used to declare a function returning no value.


We also have four modifiers to these data types: signed, unsigned, long, short. Signed and unsigned are related to negative numbers handling. Long and short are related to use of short length or long length of bits for a variable. The int base type can be modified by signed, short, long, and unsigned. The char type can be modified by unsigned and signed. You may also apply long to double.


If a variable takes 8-bits in memory, it can take all bits from 0000 0000 to all bits 1111 1111, making total of 256 combinations. Which results in range of this variable. Now either this range can hold half negative and half positive numbers or it can hold all positive numbers. So an 8-bit signed variable may hold -128 to 127 values. Which is -2^7 to 2^7 -1 values. So in general if a variable takes n-bits in memory, then its signed representsation will take -2^(n-1) to +2^(n-1)-1 values, and its unsigned representation may take 0 to 2^(n)-1 values.


So all these can be combined and we can declare some variables as below: - 

         C

Output:-


In C, there is a rule called 'implicit int'. Which says, in most cases if something is missing in statement, the C compiler presumes an int there. Although it is not a good practice to believe on it. We need to explicitly specify everything. Following are some equivalent declarations: -

signed     = signed int
unsigned   = unsigned int
long       = long int
short      = short int

Type Qualifiers

In C, we have two type qualifiers, which can allows us to control the variables: const and volatile.

Variables of type const may not be changed by program. They may be assigned an initial value only during declaration. const will tell the compiler that it is forbidden for the program to modify the variable's value. 

const can be used with any data type (char, int, enum, struct, etc.)

For example:-


const int i=10;      // I is an constant integer.
i = 20;         // It will be a error.


volatile tells the compiler that the value of a variable may change outside of the program also, due to some external activity. The system always reads the current value of a volatile object from the memory location.
The compiler should generate object code to perform each and every read from a volatile variable as well as each and every write to a volatile variable.


Post a Comment

If you have any doubts, Please let me know

Previous Post Next Post