C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

C language Basic Data Types and their Sizes

Data types define the size and type of values to be stored in the computer memory, Basic Data Types are also known as "primitive data types" here are the few basic data types with their sizes in C language:

  1. char
  2. int
  3. float

1) char

char represents character, it can be used to declare a character type variable, constant in C language. It takes only one byte (8 bits) in the computer memory and it stores a single character. The value range of char data type is -128 to +127.

'char' variable declaration

char gender; 	/* to store gender*/

'char' constant declaration

const char IS_MARRIED= 'N'; /*to store, marital status as "No"*/

2) int

int represents integer, it can be used to declare an integer type variable, constant in C language. It takes either 2 bytes (16 bits) or 4 bytes (32 bits) according to compiler architecture in the computer memory. It stores only integer numbers (numbers without precision). The value range of 2 bytes integer is -32,768 to +32,767 and 4 bytes integer is -21474836478 to +21474836477.

'int' variable declaration

int number; 	/*to store any integer number*/

'int' constant declaration

const int MAX_LENGTH=100; 	/*to store maximum length as 100*/

3) float

float represents single precision float type value, it can be used to declare a float type variable, constant in C language. It takes 4 bytes (32 bits) in the computer memory. The value range of float data type is 1.2E-38 to 3.4E+38 with 6 decimal places.

'float' variable declaration

float weight;	/*to store weight in kilogram and gram*/

'float' constant declaration

const float PI=3.14f;	/*to store value of PI as 3.14*/

These are the basic/primitive data types in C language, there are other data types too, they are the enhanced version of these data types, by using some qualifiers we can change their storage capacity and value range, some of the most useful qualifiers are:

  1. unsigned
  2. short
  3. long

A) unsigned

Consider the given declaration

char value;

Here, value is the signed char type variable and it can store value between -128 to +127, but if we have only positive values we can define the value as unsigned char.

unsigned defines that variable will store only positive values and the value range of unsigned type variable is: 0 to ((MAX_RANGE*2)+1).

unsigned char - 0 to 255
unsigned int - 0 to 65,535 (for 2 bytes integer)
unsigned int - 0 to 4294967295 (for 4 bytes integer)

B) short

short can also be written as short int, it takes 2 bytes (16 bits) in the computer memory and its value range is -32,768 to +32767 (which is similar to 2 bytes int data type).

short can also be used as unsigned short, in that case the value range will be 0 to 65,535.

C) long

long can be used with int and double data types, here is the sizes that variable will take in the computer memory.

According to 32 bits compiler architecture

long int - 8 bytes
long double - 16 bytes

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.