Sei sulla pagina 1di 4

Variables and Data Types in C Programming

Variables are used to store the value during the execution of a program. The name itself means, the
value of variable can be changed hence the name “Variable“. The variables are stored in Main Memory
i.e. RAM (size depending on the data type). In C Programming we always have to declare variable before
we can use them. Note that the space is allocated to variable in memory during execution or run-time.
C is a strongly typed language. What this means it that, the type of a variable cannot be changed.
Suppose we declared an integer type variable so we cannot store character or a decimal number
in that variable.
There are set of rules to be followed while declaring variables and data types in C Programming:
• The 1st letter should be alphabet.

• Variables can be combination of alphabets and digits.

• Underscore (_) is the only special character allowed.

• Variables can be written in both Uppercase and Lowercase or combination of both.

• Variables are Case Sensitive.

• No Spaces allowed between Characters.

• Variable name should not make use to the C Reserved Keywords.

• Variable name should not start with a number.

Data Types in C Programming Language


EXAMPLE 1: Integer Variable in C
The %d is to tell printf() function to format the integer i as a decimal number. The output from this
program would be This is my integer: 10.
EXAMPLE 2: Float Variable in C
The %f is to tell printf() function to format the float variable f as a decimal floating number. The output
from this program would be This is my float: 3.1415. Now if we want to see only the first 2 numbers after
the floating point, we would have to modify printf() function call to be as given below:
After replacing printf function in a given example. The output from this program would be This is my
float: 3.14.
EXAMPLE 3: Character Variable in C
The %c is to tell printf() function to format the variable “c” as a character. The output from this program
would be This is my character: b.
Data types in C Programming
All the data types defined by C are made up of units of memory called bytes. On most computer
architectures a byte is made up of eight bits, each bit stores a one or a zero. These eight bits with two
states give 256 combinations (28). So an integer which takes up four bytes can store a number between 0
to 4,294,967,295 (0 and 232). However, integer variables use the first bit to store whether the number is
positive or negative so their value will be between -2,147,483,648 and + 2,147,483,647.
As we mentioned, there are eight basic data types defined in the C language. Five types for storing
integers of varying sizes and three types for storing floating point values (values with a decimal point). C
doesn’t provide a basic data type for text. Text is made up of individual characters and characters are
represented by numbers. In the last example we used one of the integer types: int. This is the most
commonly used type in the C language.

Data Type Size Value Range

Char 1 byte -128 to 127 or 0 to 255

unsigned char 1 byte 0 to 255

signed char 1 byte -128 to 127

Int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647

unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295

Short 2 byte -32,768 to 32,767

unsigned short 2 byte 0 to 65,535

Long 4 byte -2,147,483,648 to 2,147,483,647

unsigned long 4 byte 0 to 4,294,967,295

The char data type is usually one byte, it is so called because they are commonly used to store single
characters. The size of the other types is dependent on machine. Most desktop machines are “32-bit”,
this refers to the size of data that they are designed for processing. On “32-bit” machines the int data type
takes up 4 bytes (232). The short is usually smaller, the long can be larger or the same size as
an int and finally the long long is for handling very large numbers.
The type of variable you use generally doesn’t have a big impact on the speed or memory usage of your
application. Unless you have a special need you can just use int variables. We will try to point out the few
different cases like. A decade ago, most machines had 16-bit processors, this limited the size
of intvariables to 2 bytes. At the time, short variables were usually also 2 bytes and long would be 4
bytes. Nowadays, with 32-bit machines, the default type (int) is usually large enough to satisfy what used
to require a variable of type long. The long long type was introduced more recently to handle very large
numeric values.
EXAMPLE 3: Calculate Size of Data type on Your Machine
To find out the size of each data type on your machine compile and run this program. It uses one new
language construct sizeof(). This tells us how many bytes a data type takes up.
[crayon-5b86eba1cbadd597692340/] The output from this program would be:

Output of Sizeof Data Types


Some computers are better at handling really big numbers, so the size of the data types will be bigger on
these machines. www.eazynotes.com Gursharan Singh Tatla Page No. 1

Data Types in C Language


A programming language is proposed to help programmer to process certain kinds of data and to
provide useful output. The task of data processing is accomplished by executing series of
commands
called program. A program usually contains different types of data types (integer, float, character
etc.) and need to store the values being used in the program. C language is rich of data types. A
C
programmer has to employ proper data type as per his requirements.
C has different data types for different types of data and can be broadly classified as:
1. Primary Data Types
2. Secondary Data Types
Primary Data Types:
Integer Data Types:
Integers are whole numbers with a range of values, range of values are machine dependent.
Generally an integer occupies 2 bytes memory space and its value range limited to -32768 to
+32767(that is, -215 to +215-1). A signed integer use one bit for storing sign and rest 15 bits for
number.
To control the range of numbers and storage space, C has three classes of integer storage namely
short int, int and long int. All three data types have signed and unsigned forms. A
short int requires half the amount of storage than normal integer. Unlike signed integer,
unsigned integers are always positive and use all the bits for the magnitude of the number.
Therefore, therange of an unsigned integer will be from 0 to 65535. The long integers are used
to declare a longerrange of values and it occupies 4 bytes of storage space.
Syntax:
int <variable name>;
int num1;
short int num2;
long int num3;
Example: 5, 6, 100, 2500.
Integer Data Type Memory Allocation:
Floating Point Data Types:
The float data type is used to store fractional numbers (real numbers) with 6 digits of precision.
Floating point numbers are denoted by the keyword float. When the accuracy of the floating
point number is insufficient, we can use the double to define the number. The double is same
as float but with longer precision and takes double space (8 bytes) than float. To extend the
precision further we can use long double which occupies 10 bytes of memory space.
Syntax:
float <variable name>;
float num1;
double num2;
long double num3;
Example: 9.125, 3.1254.
Floating Point Data Type Memory Allocation:
Character Data Type:
Character type variable can hold a single character and are declared by using the keyword char.
As there are singed and unsigned int (either short or long), in the same way there are signed
and unsigned chars; both occupy 1 byte each, but having different ranges. Unsigned characters
have values between 0 and 255, signed characters have values from –128 to 127.
Syntax:
char <variable name>;
char ch = ‘a’;
Example: a, b, g, S, j.
Void Type:
The void type has no values therefore we cannot declare it as variable as we did in case of
integer
and float. The void data type is usually used with function to specify its type.

Potrebbero piacerti anche