Get to know the Integer Types - C Language Basics

C examples for Language Basics:Variable

Introduction

There are four integer types depending on how large a number you need the variable to hold.

Typical ranges on 32-bit and 64-bit systems are given below.

char  myChar  = 0; /* -128   to +127 */
short myShort = 0; /* -32768 to +32767 */
int   myInt   = 0; /* -2^31  to +2^31-1 */
long  myLong  = 0; /* -2^31  to +2^31-1 */

C99 added support for the long long data type, which is guaranteed to be at least 64 bits in size.

long long myLL = 0; /* -2^63 to +2^63-1 */

To determine the size of a data type, use the sizeof operator.

sizeof operator returns the number of bytes that a type occupies.

The type returned from sizeof operator is size_t.

size_t is an alias for an integer type.

The specifier %zu was introduced in C99 to format this type with printf.

Visual Studio does not support this specifier and we can use %Iu instead.

Demo Code

#include <stdio.h>

int main(void) {
  size_t s = sizeof(int);
  printf("%zu", s); /* "4" (C99) */
  printf("%Iu", s); /* "4" (Visual Studio) */
}

Result

integers can also be assigned by using octal or hexadecimal notation.

The following values all represent the same number, which in decimal notation is 50.

Demo Code

#include <stdio.h>

int main() {//w  w  w  .  j a v  a  2s  .c o  m

  int myDec = 50;   /* decimal notation */
  int myOct = 062;  /* octal notation (0) */
  int myHex = 0x32; /* hexadecimal notation (0x) */

  printf("%d", myDec);
  printf("%d", myOct);
  printf("%d", myHex);
}

Result


Related Tutorials