C Data Types - C Programming (2024)

Basic Types

Type

Group

Type

Name

Size

[bits]

Hex

Range

void0

-

Characterssigned char

char

80x80..0x7F
unsigned char80x00..0xFF
Integerssigned shortint

short int

short

160x8000..0x7FFF
unsigned short int

unsigned short

160x0000..0xFFFF
signed int

int

32*0x80000000..

0x7FFFFFFF

enum32*0x80000000..

0x7FFFFFFF

unsigned int

unsigned

32*0x00000000..

0xFFFFFFFF

signed long int

longint

long

320x80000000..

0x7FFFFFFF

unsigned long int

unsigned long

320x00000000..

0xFFFFFFFF

longlong int

long long

640x8000000000000000..

0x7FFFFFFFFFFFFFFF

unsignedlong long int

unsignedlong long

640x0000000000000000..

0xFFFFFFFFFFFFFFFF

Floating

Point

float32*1.17549e-38..

3.40282e+38

double64*2.2250e-308..

1.7976e+308

long double64*2.2250e-308..

1.7976e+308

* machine dependent

Derived Types

NameDeclaration SyntaxDescription
Pointer

type *name;

Contains address of another variable
Arraytype name[size];Set of variables of the same type
Structurestruct tag {...} name;Set of variables of different types
Unionunion tag {...} name;Set of variables of different types that are allocatedon the same memory

Type Specifiers and Qualifiers

NameSyntaxDescription
typedeftypedef old_type new_type;Type definition
externextern type name;External variable declaration
staticstatic type name;Static variable has a lifetime until the endof program and a local scope
constconst type name=value;Constant variable - can't be changed
autoauto type name;Declare local variable inside function.

Can be omitted from the code.

registerregister type name;Declare local variable inside function. Suggests compiler to keep variable in CPUregister. Used for optimization.
volatilevolatile type name;Avoid compiler optimization, used when canbe changed externally.

i.e: I/Oaddressed variable.

void

Syntax:

void

Size:
  • 0 bits
  • 0 bytes
Description:
Void type
Example1:

void *p;

p = (void *)(&x);

Example2:

void my_func(int x)

{

:

/* no return value */

}

char

Syntax:

char variable name;

Size:
  • 8 bits
  • 1 bytes
Range:
  • Dec: -128..127

  • Hex: 0x80..0x7F

  • Const: CHAR_MIN..CHAR_MAX

Description:
Signed character
Examples:

char c;

char c=32;

char c='a';

char *msg="Hello there";

char msg[]="Hello there";

signed char

Syntax:

signed char variable name;

Size:
  • 8 bits
  • 1 bytes
Range:
  • Dec: -128..127

  • Hex: 0x80..0x7F

  • Const: SCHAR_MIN..SCHAR_MAX

Description:
Signed character.

Same as char.

Examples:

signed char c;

signed char c=-100;

signed char c='a';

signed char *msg="Hello there";

signed char msg[]="Hello there";

unsigned char

Syntax:

unsigned char variable name;

Size:
  • 8 bits
  • 1 bytes
Range:
  • Dec: 0..255

  • Hex: 0..0xFF

  • Const: 0..UCHAR_MAX

Description:
Unsigned character.
Examples:

unsigned char c;

unsigned char c=200;

unsigned char c='a';

short

Syntax:

short variable name;

Size:
  • 16 bits
  • 2 bytes
Range:
  • Dec: -32768..32767

  • Hex: 0x8000..0x7FFF

  • Const: SHRT_MIN..SHRT_MAX

Description:
Signed short integer
Examples:

short c;

short c=-20000;

short c=20000;

unsigned short

Syntax:

unsigned short variable name;

Size:
  • 16 bits
  • 2 bytes
Range:
  • Dec: 0..65535

  • Hex: 0..0xFFFF

  • Const: 0..USHRT_MAX

Description:
Unsigned short integer.
Examples:

unsigned short c;

unsigned short c=40000;

int

Syntax:

int variable name;

Size:
  • 32 bits (machine dependent)
  • 4 bytes
Range:
  • Dec: -2147483648..2147483647

  • Hex: 80000000..0x7FFFFFFF

  • Const: INT_MIN..INT_MAX

Description:
Signed integer.
Examples:

int c;

int c=-200000;

int c=200000;

enum

Syntax:

enum name {

name1 [= number1],

name2 [=number2],

name3 [= number3],

:

};

Size:
  • 32 bits
  • 4 bytes
Range:
  • Dec: -2147483648..2147483647

  • Hex: 80000000..0x7FFFFFFF

  • Const: INT_MIN..INT_MAX

Description:
Enumeration constant list of signed integers.
Examples:

enum subject {

MATH= 1,

PHYSICS= 2,

CHEMISTRY = 3,

PROGRAMMING = 4,

ELECTRONICS = 9

};

unsigned int

Syntax:

unsigned int variable name;

Size:
  • 32 bits (machine dependent)
  • 4 bytes
Range:
  • Dec: 0..4294967295

  • Hex: 0..0xFFFFFFFF

  • Const: 0..UINT_MAX

Description:
Unsigned integer.
Examples:

unsigned int c;

unsigned int c=200000U;

long

Syntax:

long variable name;

Size:
  • 32 bits
  • 4 bytes
Range:
  • Dec: -2147483648..2147483647

  • Hex: 80000000..0x7FFFFFFF

  • Const: LONG_MIN..LONG_MAX

Description:
Signed long integer.
Examples:

long c;

long c=-200000L;

long c=200000L;

unsigned long

Syntax:

unsigned long variable name;

Size:
  • 32 bits
  • 4 bytes
Range:
  • Dec: 0..4294967295

  • Hex: 0..0xFFFFFFFF

  • Const: 0..ULONG_MAX

Description:
Unsigned long integer.
Examples:

unsigned long c;

unsigned long c=200000UL;

long long

Syntax:

long long variable name;

Size:
  • 64 bits
  • 8 bytes
Range:
  • Dec: 9223372036854775808..

    9223372036854775807

  • Hex: 0x8000000000000000..

    0x7FFFFFFFFFFFFFFF

  • Const: LLONG_MIN..LLONG_MAX

Description:
long long integer.
Examples:

long long c;

long long c=200000LL;

unsigned long long

Syntax:

unsigned long long variable name;

Size:
  • 64 bits
  • 8 bytes
Range:
  • Dec: 0..18446744073709551615

  • Hex: 0..0xFFFFFFFFFFFFFFFF

  • Const: 0..ULLONG_MAX

Description:
Unsigned long long integer.
Examples:

unsigned long long c;

unsigned long long c=200000ULL;

float

Syntax:

float variable name;

Size:
  • 32 bits
  • 4 bytes
Range:
  • Dec:1.17549e-38..

    3.40282e+38

  • Const: FLT_MIN..FLT_MAX

Description:
Single precision floating point.
Examples:

float c;

float c=200.8F;

float c=-2e-8F;

double

Syntax:

double variable name;

Size:
  • 64 bits
  • 8 bytes
Range:
  • Dec: 2.2250e-308..

    1.7976e+308

  • Const: DBL_MIN..DBL_MAX

Description:
Double precision floating point.
Examples:

unsigned char c;

unsigned char c=200.8;

unsigned char c=-2e-8;

long double

Syntax:

long double variable name;

Size:
  • 64 bits
  • 8 bytes
Range:
  • Dec: 2.2250e-308..

    1.7976e+308

  • Const: LDBL_MIN..LDBL_MAX

Description:
Long double precision floating point.
Examples:

unsigned char c;

unsigned char c=200.8L;

unsigned char c=-2e-8L;

Pointer

Syntax:

type *name;

type (*func_ptr)(type1, type2,..);

Description:
Contains the memory address of another variable
Example1:

int x=10;

int *px;

/* px points to x -

px contains the address ofx */

px = (int *)(&x);

/* x now equal to 20 */

*px = 20;

Example2:

int square(int x)

{

return x*x;

}

int main(void)

{

/* function pointer declaration*/

int (*pfunc)(int) = square;

/* call function -> y=100 */

int y = (*pfunc)(10);

}

Array

Syntax:

type name[size];

type name[size1][size2];

Description:
Contains a set of variables of the same type
Example1:

int x[10];

int y[10][20];

int z[5]={10,20,30,40,50};

char s[]="Hello";

int i,j;

/* reset x array */

for(i=0; i<10; i++) {

x[i] = 0;

for(j=0; j<10;j++) {

y[i][j] = 0;

}

}

/**/

*px = 20;

Example2:

int square(int x)

{

return x*x;

}

int main(void)

{

/* function pointer declaration*/

int (*pfunc)(int) = square;

/* call function -> y=100 */

int y = (*pfunc)(10);

}

Structure

Syntax:

struct tag {

type1 member1;

type2 member2;

...

} name;

Description:
Contains a set of variables of different types
Example1:

struct time {

int h;

int m;

};

struct time *pt;

struct time dt;

struct time t1 = {8,0};

struct time t2 = {16,0};

dt.h = t2.h - t1.h;

dt.m = t2.m - t1.m;

if(dt.m < 0) {

dt.m += 60;

dt.h -= 1;

}

Example2 - bit fields:

struct reg {

unsigned int in: 3;

unsigned int out: 4;

unsigned int reserved : 9;

};

int input;

int output;

struct reg r=0; // sets all bits to 0

input = r.in;

output = 3;

r.out = output;

Union

Syntax:

union tag {

type1 member1;

type2 member2;

...

} name;

Description:
Contains a set of variables, that are allocatedon the same memory
Example:

union data {

unsigned long num;

char name[4];

}

union data d1.name="abc";

union data d2.num =0x00434241;

/* now d1 is equal to d2 */

typedef

Syntax:

typedef old_type new_type;

Description:
New type definition
Example:

typedef unsigned char Byte

typedef unsigned int Word

typedef struct time {

char h;

char m;

} Time;

Byte c=255;

Word x;

Time t;

extern

Syntax:

extern type name;

Description:
External variable declaration, used in other filesto broaden the variable scope to other files. The externaldeclaration is usually written in header file.
Example:
See Program Structure

static

Syntax:

static type name;

static type function(type arg1,...)

Description:
Static variables are freed only whenprogram ends, even when defined inside a function.

Static variableshave local file scope for global variables, and localfunction scope for local variables.

Static functions have local file scope and are notvisible outside the file.

Example:

// external static variable

static int a;

void myfunc()

{

// local static variable

static int x=10;

x = x+1; /* =10,11,12,...*/

/* for each function call*/

}

const

Syntax:

const type name=value;

Description:
Constant variable - can't be changed
Example:

const int linesnum=100;

linenum = 0; /* error! */

auto

Syntax:

auto type name;

Description:
Declare local variable inside function. Can be omittedfrom the code.
Example:

void myfunc()

{

auto int x;

...

}

register

Syntax:

register type name;

Description:
Declare local variable inside function. Suggests compiler to keep variable in CPUregister. Used for optimization.
Example:

void myfunc()

{

register int x;

...

}

volatile

Syntax:

volatile type name;

Description:
Avoid compiler optimization, used when can be changedexternally (i.e: I/Oaddressed variable).
Example:

volatile unsigned short data;

C Data Types - C Programming (2024)
Top Articles
Latest Posts
Article information

Author: Lidia Grady

Last Updated:

Views: 6235

Rating: 4.4 / 5 (65 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Lidia Grady

Birthday: 1992-01-22

Address: Suite 493 356 Dale Fall, New Wanda, RI 52485

Phone: +29914464387516

Job: Customer Engineer

Hobby: Cryptography, Writing, Dowsing, Stand-up comedy, Calligraphy, Web surfing, Ghost hunting

Introduction: My name is Lidia Grady, I am a thankful, fine, glamorous, lucky, lively, pleasant, shiny person who loves writing and wants to share my knowledge and understanding with you.