Skip to main content

Data Types and Qualifiers in C



There are four data types in C language. They are,
Types
Data Types
  Basic data types   int, char, float, double
  Enumeration data type   enum
  Derived data type   pointer, array, structure, union
  Void data type   void

Here we will discuss only basic data types.

Each variable in C has an associated data type. Each data type requires different amounts of memory and has some specific operations which can be performed over it. Let us briefly describe them one by one:
Following are the examples of some very common primitive data types used in C:
char: The most basic data type in C. It stores a single character and requires a single byte of memory in almost all compilers.
int: As the name suggests, an int variable is used to store an integer.
float: It is used to store decimal numbers (numbers with floating point value) with single precision.
double: It is used to store decimal numbers (numbers with floating point value) with double precision.
void:value less

Qualifiers are used to qualify the data type.The following are the qualifiers used with primitive data type.
unsigned, signed(default)
short,long
const
volatile

The unsigned qualifier avoids sign bit.
The qualifier const can be applied to the declaration of any variable to specify that its value will not be changed directly.
Value of any variable can be changed either by program or external device.


Different data types also have different ranges upto which they can store numbers. These ranges may vary from compiler to compiler. Below is list of ranges along with the memory requirement and format specifiers on 32 bit gcc compiler.


Data Type             Memory(bytes)    Range              Format Specifier
short int                   2       -32,768 to 32,767                   %hd                   
unsigned short int          2       0 to 65,535                         %hu
unsigned int                4       0 to  4,294,967,295                 %u
int                         4      -2,147,483,648 to 2,147,483,647      %d or %i
long int                    4      -2,147,483,648 to 2,147,483,647      %ld
unsigned long int           4       0 to 4,294,967,295                  %lu
long long int               8       -(2^63) to (2^63)-1                 %lld
unsigned long long int      8       0 to 18,446,744,073,709,551,615     %llu
signed char                 1       -128 to 127                         %c 
unsigned char               1       0 to 255                            %c
float                       4       3.4E-38  to 3.4E+38                 %f
double                      8       1.7E-308 to 1.7E-30                 %lf
long double                12      3.4E-4932 to 1.1E+4932               %Lf
For TURBO C
Data type size (in byte)
char 1
Short int 2
int 2
long int or long 4
float 4 (6 decimal places of precision)
double 8 (15
decimal places of precision) 
long double 10 (19 decimal places of precision)

We can use the sizeof() operator to check the size of a variable. See the following C program for the usage of the various data types:

#include <stdio.h>
int main()
{int a = 1;
char b ='G';
double c = 3.14;
//printing the variables defined above along with their sizes
printf("character. value is %c and size is %lu byte.\n",b,sizeof(char));
printf(" integer. value is %d and  size is %lu  bytes.\n", a,sizeof(int));
printf(" double value is %lf and  size is %lu ytes.\n",c,sizeof(double));
return 0;
}
 The header file limits.h defines limits of integer types and the user can use the constants defined in it.
#include <limits.h>
#include <stdio.h>
int main()
{
printf("The max value of integer=%d",INT_MAX);
printf("The min value of integer=%d",INT_MIN);
return 0;
} The header file float.h defines macros that allow you to use these values and other details about the binary representation of real numbers in your programs. The following example prints the storage space taken by a float type and its range values −

#include <stdio.h>
#include <float.h>
int main() {
   printf("Storage size for float : %d \n", sizeof(float));
   printf("Minimum float positive value: %E\n", FLT_MIN );
   printf("Maximum float positive value: %E\n", FLT_MAX );
   printf("Precision value: %d\n", FLT_DIG );
   return 0;
}


Comments

Popular posts from this blog

Files in C

A file is a repository of data that is stored in a permanent storage media, mainly in secondary memory. In order to use the files we should learn how to read information from a file and how to write information into a file. A very important concept in C is the stream.The stream is a common logical interface to the various devices( files).A stream is linked to a file while using an open operation. A stream is disassociated from a file while using a close operation. The current location, also referred to as the current position, is the location in a file where the next fie access will occur.There are two types of streams text and binary. The following are some difference between text and binary files ·Text file is human readable because everything is stored in terms of text. In binary file everything is written in terms of 0 and 1, therefore binary file is not human readable. ·A newline(\n) character is converted into the carriage return-linefeed combination before being written to the d…

KTU C programming question paper and evaluation scheme

APJ ABDUL KALAM TECHNOLOGICAL UNIVERSITY SECOND SEMESTER B.TECH DEGREE EXAMINATION, MAY 2017 CS 100 COMPUTER PROGRAMMING (CS, IT) SCHEME OF EVALUATION
PART A 1 An identifier is a sequence of characters invented by the programmer or to identify or name a specific object. The sequence of characters may be letters, digits, and special character ‘_’known as an underscore Rules: i)Identifiers should start with alphabets. ii)Identifiers are case sensitive iii)A numeric digit should not be the first character iv)Identifier name should not be a keyword v)Identifier may be of any reasonable length 1mark






2mark 2 Associativity defines the direction, left to right or right to left in which operator act upon its operands Unary operators have associativity is from right to left. For examplein the expression &--x, pre decrement works first and then address of operator works Direction + example<

Strings in C

Strings in C Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character ‘\0’. Declaration of strings: Declaring a string is as simple as declaring a one dimensional array. Below is the basic syntax for declaring a string. charstr_name[size]; In the above syntax str_name is any name given to the string variable and size is used define the length of the string, i.e the number of characters strings will store. Please keep in mind that there is an extra terminating character which is the Null character (‘\0’) used to indicate termination of string which differs strings from normal character arrays. Initializing a String: A string can be initialized in different ways. We will explain this with the help of an example. Below is an example to declare a string with name as str and initialize it with “cek”. 1. charstr[] = "cek"; 2. charstr[50] = "cek"; 3. charstr[] = {'c','…