top of page

Data Types in C

Introduction


When working with higher-level languages such as Python, you may be used to simply initializing a variable as a number or string and letting the compiler do the heavy-lifting behind the scenes.


If you've read my blog on CS50's Week 1, you'll know that in C this is simply not the case.


While at first it may seem like a hassle to manually specify datatypes for each and every variable or function (and encounter a few compile errors along the way), you might just find that the ability to control code on a lower-level can add up to increased program speed and optimized system resource usage.


Let's take a closer look at some such datatypes (and types in general).


Sidenote: There are 8 bits in a byte.


int


  • used for variables that store integers

    • int integer = 12;

  • takes up 4 bytes (32 bits of memory)

  • range:

    • [-2³¹, 2³¹ -1]

    • stores 2³² values in total because 2ⁿ where n is the number of binary bits available to represent a number

    • half are negative, one is zero, half - 1 are positive


unsigned int


  • used for variables that store only positive integers with double the positive range

    • unsigned int integer = 12;

  • takes up 4 bytes (32 bits of memory)

  • range:

    • [0, 2³² -1]

    • stores 2³² values in total because 2ⁿ where n is the number of binary bits available to represent a number

    • one is zero, total - 1 are positive


char


  • used for variables that store single characters

    • char character = 'A';

  • takes up 1 byte (8 bits of memory)

  • range:

    • [-2⁷, 2⁷ -1]

    • stores 2⁸ values in total because 2ⁿ where n is the number of binary bits available to represent a character

    • half are negative, one is zero, half - 1 are positive

    • ASCII is a standard used to map numbers to characters from 0 to 127.

      • for example, 'A' maps to 65, while 'a' maps to 97


float


  • used for variables that store real numbers or decimals

    • float decimal = 12.23;

  • takes up 4 bytes (32 bits of memory)

  • range:

    • depends on your number

    • we have 32 bits to work with (32 "slots" so to speak)

    • floating point imprecision

      • for example, we can't represent pi or a decimal that may take up more than 32 bits of space precisely using a float


double


  • used for variables that store real numbers or decimals + can store more trailing decimal values

    • double decimal = 12.23;

  • takes up 8 bytes (64 bits of memory)

  • range:

    • same as float, however we have double the number of bits to work with, making our numbers much more precise

    • kind of deals with the imprecision


void


  • not a datatype, but rather a type

  • cannot be assigned to variables, but can be assigned to a function's parameter list or return type

    • int main (void) { ... }

    • void updateScores() { ... }

  • Means that a function does not take any parameters or does not return any values


bool


  • used for variables that store a Boolean value: true and false.

    • bool isTrue = true;

  • requires additional libraries to be used, as C doesn't offer bools by default. Including one of the two will suffice!

  • range:

    • 1 bit: 0 (false) or 1 (true)


string (char *)


  • used for variables that store a series of characters (consecutively stored in memory with \0 or null signalling the end of the string)

    • string name = "Bob";

    • OR

    • char *name = "Bob"

  • using the type string requires importing the cs50 library

  • char * denotes a pointer to the memory location of a variable, on which you can find more about in my Week 4 blog!


structs and typedefs


  • data structure that allows us to create custom data types or structures by fusing existing ones

  • for instance, I could create a type term that contains the word itself, its part of speech, and its dictionary definition:

    • typedef struct term {

char *word;

char *speechPart;

char *definition;

}

term;


Final Thoughts


That's all for data types in C, although, as you can imagine, there are many, many more I haven't covered this time around.



Thanks for reading! Meanwhile, stay tuned for updates by following my blog and LinkedIn page.

Commentaires


bottom of page