Basics of Variable Storage for C Programming - dummies

Basics of Variable Storage for C Programming

By Dan Gookin

Digital storage is measured in bytes. Though displayed in the C programming language, all the information stored inside memory is simply a mass of data, bits piled upon bits, bytes upon bytes. It’s up to the software to make sense of all that.

Introduction to variable storage

In C programming, data is categorized by storage type (char, int, float, or double) and further classified by keyword (long, short, signed, or unsigned). Despite the chaos inside memory, your program’s storage is organized into these values, ready for use in your code.

Inside a running program, a variable is described these attributes:

  • Name: the name you give the variable. The name is used only in your code, not when the program runs.

  • Type: one of the C language’s variable types: char, int, float, and double.

  • Contents: set in your program when a variable is assigned a value. Though data at the variable’s storage location may exist beforehand, it’s considered garbage, and the variable is considered uninitialized until it’s assigned a value.

  • Location: an address, a spot inside the device’s memory. This aspect of a variable is something you don’t need to dictate; the program and operating system negotiate where information is stored internally. When the program runs, it uses the location to access a variable’s data.

Of these aspects, the variable’s name, type, and contents are already known to you. The variable’s location can also be gathered. Not only that, but the location can be manipulated, which is the inspiration behind pointers.

How to read a variable’s size

How big is a char? How long is a long? Only the device you’re programming knows the exact storage size of C’s standard variables.

How Big Is a Variable? uses the sizeof operator to determine how much storage each C language variable type occupies in memory.

HOW BIG IS A VARIABLE?

#include <stdio.h>
int main()
{
 char c = 'c';
 int i = 123;
 float f = 98.6;
 double d = 6.022E23;
 printf("chart%un",sizeof(c));
 printf("intt%un",sizeof(i));
 printf("floatt%un",sizeof(f));
 printf("doublet%un",sizeof(d));
 return(0);
}

Exercise 1: Type the source code from How Big Is a Variable? into your editor. Build and run to see the size of each variable type.

Here’s the output:

char 1
int 4
float 4
double 8

The sizeof keyword isn’t a function. It’s more of an operator. Its argument is a variable name. The value that’s returned is of the C language variable type known as size_t.

The size_t variable is a typedef of another variable type, such as an unsigned int on a PC or a long unsigned int on other computer systems. The bottom line is that the size indicates the number of bytes used to store that variable.

Arrays are also variables in C, and sizeof works on them.

HOW BIG IS AN ARRAY?

#include <stdio.h>
int main()
{
char string[] = "Does this string make me look fat?";
printf("The string "%s" has a size of %u.n"