Home > C Programming, C Tutorials > C Tutorial – Constants, Variables and Data Type Modifiers

C Tutorial – Constants, Variables and Data Type Modifiers

Constant

A constant can be defined as “a quantity that does not change during the execution of a program”.

#include <stdio.h>
 
void main(void)
{
        int a = 10;
        printf(“%d”,a);
}

Program Output
10

Here we are declaring a variable a with its initial value 10. 10 here is an integer constant and every time you try to execute this program, the output will always be 10.




Types of Constants

Integer Constant
An integer constant must have atleast one digit and should not have decimal point. It could either be positive or negative.

Real Constant
A real constant must have atleast one digit and must have a decimal point.

Character Constant
A character constant should be only one character and must be enclosed in single quotes e.g. ‘A’, ‘b’, etc.

Variables

A variable is a name give to the space in the memory for holding data such as integers, characters, floating point numbers, strings, etc. Using a variable name is more convenient than using the memory locations.

There are certain rules for naming variables for which you need to refer to the rules for identifiers.

Declaration of variables
To declare a variable means to create a memory space for the variable depending on the data type used and associate the memory location with the variable name. In C all the variables should be declared before it can be used. Also you cannot re-declare a variable once it has been already declared.

The general pattern for declaring a variable is as follows:

, , ;

e.g.:

	int age;
	int age, count, i,j;
	float amount, rateofinterest, totalbalance;
	char ch;

Constant Variables

A constant variable is also called a named constant. A named constant is really a variable whose content is read-only and cannot be changed while the program is running. Here is a declaration of a named constant.

	const float pi = 3.14;

It looks just like a regular variable declaration except that the word const appears before the data type name. const is a qualifier that tells the compiler to make the variable read only. Its value will remain constant throughout the programs execution.

An initialization value must be given when declaring a variable with the const qualifier, or an error will result when the program is compiled. A compiler error will also result if there are any statements in the program that attempt to change the contents of a named constant.




Data type modifiers

We have so far learned about the fundamental data types, why they are used, their default sizes and values. Now let us understand the need for data type modifiers and the way they can be used.

Let us consider an example of a program, which will accept an age from the user to do some processing. Because the age is represented in numbers, so we will have to use the integer data type int. We all know that even under exceptional case an age of a person cannot exceed more than 150. Now, that we are using an integer data type it occupies 2 Bytes of memory, which would not be required to represent the value 150. Instead the value 150 could easily be saved in an integer of 1 Byte in size, but the default size of an integer is 2 Bytes. So we have a problem here.

Well, not that we can’t solve. To override the default nature of a data type, C has provided us with data type modifiers as follows:

signed
By default all data types are declared as signed. Signed means that the data type is capable of storing negative values.

unsigned
To modify a data type’s behavior so that it can only store positive values, we require to use the data type unsigned.

For example, if we were to declare a variable age, we know that an age cannot be represented by negative values and hence, we can modify the default behavior of the int data type as follows:

       unsigned int age;

This declaration allows the age variable to store only positive values. An immediate effect is that the range changes from (-32768 to 32767) to (0 to 65536)

long
Many times in our programs we would want to store values beyond the storage capacity of the basic data types. In such cases we use the data type modifier long. This doubles the storage capacity of the data type being used. E.g. long int annualsalary will make the storage capacity of variable annualsalary to 4 bytes.

The exception to this is long double, which modifies the size of the double data type to 10 bytes. Please note that in some compilers this has no effect.

short
If long data type modifier doubles the size, short on the other hand reduces the size of the data type to half. Please refer to the example of age variable to explain the concept of data type modifiers. The same will be achieved by providing the declaration of age as follows:

	short int age;

This declaration above, will provide the variable age with only 1 byte and its data range will be from -128 to 127.

This was about constants, variables and data type modifiers. In the subsequent C Tutorials you will learn about C Operators.

Subscribe now to receive updates when a new C tutorial is released.


Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41
  1. sreenivasrao.R
    July 9th, 2008 at 11:47 | #1

    the information that u have given is exceptionally sufficient and thanks for the same

  2. neha
    December 31st, 2009 at 10:13 | #2

    thanx for help:at a class time test

  3. swathi
    January 25th, 2012 at 05:54 | #3

    we want some more infermation

  1. No trackbacks yet.
Enter this code to leave comment (Sorry, but bots get me crazy :) )