Everything2
Near Matches
Ignore Exact
Full Text
Everything2

C Data Types

created by flamingweasel

(idea) by flamingweasel (2.1 y) (print)   ?   (I like it!) Fri Dec 08 2000 at 11:04:03

Back to A Brief Guide to C

note: These are formatted as follows:
  • Datatype: Explanation

    Issues with use

Basic Datatypes

A note of explanation: These are almost certainly not all the datatypes available with your compiler. You probably have special ones that allow you to use very long integers, for instance. I will just be describing the ones that are completely standard, and they'll be what you use 99% of the time.
  • int: The integer.

    A whole number, whose size depends on the processor you are using. For instance my Pentium 2, a 32-bit processor, would hold 4,294,967,296 total numbers, half being negative, one being 0, and the rest being positive (in my case, this is a range between -2,147,483,648 and 2,147,4836,47). The number is held in 2's Complement format.

  • float: Floating-point number.

    A decimal number, 4 bytes long in memory, consisting of the following parts:

    • Sign Bit: One bit, denoting if the value is negative or not. If this has 1 it is negative.
    • Mantissa: 23 bits, holding a number which is >1 and <2. If the exponent is at the very bottom of its range, the most significant digit of the mantissa may become 0, enabling the float hold smaller values at the expense of accuracy.
    • Exponent: 8 bits, holding the exponent + 127 (the number is stored unsigned, so this "bias" is needed to extend the range below zero).

    Remember, kids, there's a reason why this type is nicknamed "liar." Never, ever use this type if you are concerned at all about accuracy. The only reason to use this over a double is if you don't care about getting the value (almost definitely) wrong and need the 4 byte difference between the types. This type has 6 or 7 significant digits; stores numbers between 1.175494351 E -38 and 3.402823466 E 38

  • double: Double-accuracy Floating Point.

    Another decimal storage type, except this one is useful when you actually care about getting the answer (almost definitely) correct. This type holds numbers in a range between 2.2250738585072014 E -308 1.7976931348623158 E 308. Has components like the float, with some modifications:

    • Sign Bit: Same as before
    • Mantissa: 52 bits, holding a number >1 and <2
    • Exponent: 11 bits, holding the exponent + 1023

  • char: Character

    One byte, holding a number which corresponds to a character. Check the Table of ASCII Characters for the conversion from number to character. Remember that this type can be very useful for small values, flags, etc.

Modifiers

  • unsigned: Force the type to only store numbers greater than 0.

    This is very good to use if you only need to store positive numbers, and would like to double your range (instead of a char storing -128 to 127, and unsigned char would hold 255 to 0)

  • long: Enlarge the type to hold more values at the expense of memory space

    This has non-standard effects, depending on your processor and compiler. Run the chunk of code at the end of this chapter to test the size of the resulting type.

  • short: Shrink the type's size to conserve memory space at the expense of the type's range of values

    Again, this has non-standard effects depending on your compiler and processor. Check the resulting size with the code at the end of the chapter.

Testing the Size of Types


/*test the size of your favorite types on the system of
  your choice. replace foo's type with what you want to 
  see the size of.*/

#include <stdio.h>

int main() {
    long int foo;
    unsigned char size;
    
    size = sizeof(foo);
    printf ("%d\n", (int)size);
}

printable version
chaos

A Brief Guide To C Learn to Program: Types and Objects data type enum
relational operator cross-linked list Alignment void
vprintf Table of ASCII Characters libc dictionary
unsigned
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.
  Epicenter
Login
Password

password reminder
register

Everything2 Help

Cool Staff Picks
Just another sprinkling of indeterminacy
Hillsborough Disaster
noble gas
Gospel of Thomas
facial expression
Editor Log: December 2003
Pickett's Charge
reincarnation
perspective in small children's artwork
Prisoner's Dilemma
You are precious to me. Did you know that?
Questions for those who don't like capitalism
Ceremony of Carols
Battle of Stamford Bridge
New Writeups
Madara
There’s nothing like a trail of blood to find your way back home(fiction)
Heitah
After sneeze(idea)
froggy7384
Why we smoke(personal)
doctor wilson
treewrite(thing)
kanoodle
Tiglath-pileser III(person)
raincomplex
Adaptive(place)
The Custodian
Forgotten things in space(place)
DreamingOfGaia
boredom(event)
tentative
July 4, 2008(personal)
Whiskeydaemon
sewing machine(log)
waverider37
Forgotten things in space(poetry)
jessicaj
Days when art is too much to bear(idea)
You've Got Gold
I aborted my peace and tranquility(event)
Simulacron3
Sensing and Perceiving(essay)
StrawberryFrog
Bore(person)
This page courtesy of The Everything Development Company