C Data Types

Advertisements
Advertisements

 17 total views

Data types are a very important concept in programming languages. You can choose the right kind of variable types for your program. The data types depend on the compiler which creates machine codes suitable for 16-bit or 32-bit processors.

Sometimes when the processor has backward compatibility, then a 32-bit processor can run a 16-bit machine code successfully.

Integer Data Types

An integer data type is declared using keyword – int. We will have a separate discussion on keywords so let’s not worry about them right now.

If you think of memory, then think of sequence of bytes with an address called a memory address. A memory address is a hexadecimal value. One byte of information is 8-bit of information.

Advertisements
Integer Data TypesRequired Storage
int2 or 4 bytes
short int2 bytes
long int4 bytes
storage requirement for each integer type

On a 16-bit compiler like turbo C++, the int type has requires two bytes storage and on a 32-bit compiler, storage requirement is 4 bytes.

Short int is always 2 bytes in size and long int is always 4 bytes in size.

Range of the data types

What does it mean to have a range of values ? We will explain that in a minutes. Machine codes are binary strings made of 0s and 1s. They are instructions or data for processor – Opcode or Operand.

So, the range of a data type is nothing, but number of possible binary strings that is allowed using the byte storage. In other words, ” How many numbers can you store in binary format using 2 or 4 bytes of memory space ?”

For 16-bit compiler the range is

\begin{aligned}\left(\frac{2^{16} }{2}\right )- 1 = \left(\frac{65536}{2}\right ) - 1 = -32768 \hspace{2px} to\hspace{2px} +32767\end{aligned}

For 32-bit compiler the range is

\begin{aligned}\left (\frac{2^{32} }{2}\right ) - 1 =\left (\frac{4294967296}{2}\right)- 1 = -2147483648 \hspace{2px} to \hspace{2px} +2147483647\end{aligned} 

Unsigned and Signed Integers

If you want all positive values then declare the int variable as unsigned. An unsigned data type disregard the sign bit, so that all the values are positive starting from 0.unsigned int for 16-bit compiler.

\begin{aligned}2^{16} - 1 = 65536 - 1 = Range \hspace{2px} from \hspace{2px}0\hspace{2px} to \hspace{2px}65535\end{aligned}

unsigned int for 32-bit compiler

\begin{aligned}2^{32} - 1 = 4294967296 - 1 = Range \hspace{2px} from \hspace{2px} 0 \hspace{2px} to \hspace{2px}4294967295.\end{aligned}

Signed data type has both positive and negative values.

For example,

Advertisements
signed long int;
signed short int;

Character Data Types

The character data type takes 1 byte of storage. The signed char and char are the same.

Unsigned and Signed Characters

signed char ; /* Range from -128 to + 127 */
unsigned char; /* Range from 0 to 255 */

Each character has a number and char cannot take values greater than -128 to +127.

Float and Double Data Types

The float and the double data types take 4 bytes and 8 bytes respectively. The long double take 10 bytes of storage.

Data TypesRequired Storage
float4 bytes
double8 bytes
long double10 bytes
storage required for float and double types

An Example Program

#include <stdio.h>
int main()
{
    char p = 'a';
    int s = 10;
    long int q1 = 1000;
    short int q2 = 223;
    float b1 = 23.55;
    double b2 = 33.5545;
    signed char ch = 'h';
    signed int g1 = -33;
    unsigned char ch1 = 'D';
    unsigned int g2 = 234;
    unsigned long int num = 333234;
    unsigned short int num2 = 2442;
    printf("char = %c\n", p);
    printf("int = %d\n",s);
    printf("long int = %ld\n",q1);
    printf("short int =%d\n",q2);
    printf("float = %f\n",b1);
    printf("double = %f\n",b2);
    printf("signed char= %c\n",ch);
    printf("signed int = %d\n", g1);
    printf("unsigned char = %u\n",ch1);
    printf("unsigned int = %u\n",g2);
    printf("unsigned long int = %u\n",num);
    printf("unsigned short int = %u\n",num2);
    system("PAUSE");
    return 0;
}

Output

char = a
int = 10
long int = 1000
short int =223
float = 23.549999
double = 33.554500
signed char = h
signed int = -33
unsigned char = 68
unsigned int = 234
unsigned long int = 333234
unsigned short int = 2442

References

  • Balagurusamy, E. 2000. Programming in ANSI C. Tata McGraw-Hill Education,.
  • Brian W. Kernighan, Dennis M. Ritchie. 1988. C Programming Language, 2nd Edition. Prentice Hall.

Bestseller

Effective C: An Introduction to Professional C Programming

A detailed introduction to the C programming language for experienced programmers.

The world runs on code written in the C programming language, yet most schools begin the curriculum with Python or Java. Effective C bridges this gap and brings C into the modern era–covering the modern C17

You’ll learn:

  • How to identify and handle undefined behavior in a C program
  • The range and representations of integers and floating-point values
  • How dynamic memory allocation works and how to use nonstandard functions
  • How to use character encodings and types
  • How to perform I/O with terminals and filesystems using C Standard streams and POSIX file descriptors
  • How to understand the C compiler’s translation phases and the role of the preprocessor
  • How to test, debug, and analyze C programs

Effective C will teach you how to write professional, secure, and portable C code that will stand the test of time and help strengthen the foundation of the computing world.

Advertisements
Advertisements
Advertisements