Signed and Unsigned Data Types - C Language Basics

C examples for Language Basics:Variable

Introduction

By default, all integer types in C are signed.

Signed data type may contain both positive and negative values.

Signed data type can be explicitly declared using the signed keyword.

Demo Code

#include <stdio.h>

int main() {//from   w  w w .j a  va  2  s  .c  o  m
    signed char  myChar;   /* -128   to +127 */
    signed short myShort;  /* -32768 to +32767 */
    signed int   myInt;    /* -2^31  to +2^31-1 */
    signed long  myLong;   /* -2^31  to +2^31-1 */
    signed long long myLL; /* -2^63  to +2^63-1 */
}

The integer types can be declared as unsigned to double their upper range.

Demo Code

#include <stdio.h>

int main() {//  ww w  . java 2s . c o m
    unsigned char  uChar;   /* 0 to 255 */
    unsigned short uShort;  /* 0 to 65535 */
    unsigned int   uInt;    /* 0 to 2^32-1 */
    unsigned long  uLong;   /* 0 to 2^32-1 */
    unsigned long long uLL; /* 0 to 2^64-1 */
}

In printf the specifier %u is used for the unsigned char, short, and int types.

The unsigned long type is specified with %lu and unsigned long long with %llu.

Demo Code

#include <stdio.h>

int main() {// w w  w .ja  v a2 s .  c o m
    unsigned int uInt = 0;
    printf("%u", uInt); /* "0" */
}

Result

The signed and unsigned keywords may be used as types on their own, in which case the int type is assumed by the compiler.

Demo Code

#include <stdio.h>

int main() {//from ww  w.java2  s .  c  om
    unsigned uInt; /* unsigned int */
    signed sInt;   /* signed int */
}

In the same way, the short and long data types are abbreviations of short int and long int.

Demo Code

#include <stdio.h>

int main() {/*from   ww w .  j av  a 2 s .co m*/

    short myShort; /* short int */
    long myLong;   /* long int */
}

Related Tutorials