From the course: C Programming Basics: Flow Control, Variables, and Pointers

Exploring variables

- [Instructor] A variable is a data container. In the C language, that container holds an integer, floating point, character, or other data type. But technically speaking, what actually is a variable? In the typical C language variable declaration, you learn two tidbits about a variable: its type and its name. Here, you see an integer variable declared. It's an int variable, the type, and its name is a. The variable is uninitialized or never assigned a value. Even so, you see it used in the code as shown in line seven. Build and run the program. On my screen, the value of a is equal to 65. You might even see the value zero, although that's just random. Unlike other programming languages, C doesn't initialize variables as they're declared. Internally, the chunk of memory is assigned or allocated to store the variable's information. That chunk isn't set to zero. It's just a specific location in memory and whatever value is already there is absorbed by your variable as shown in the output. The moral of the story is to always initialize variables before you use them. Two additional tidbits about a variable can be obtained by using special C language operators. The first is the sizeof operator. sizeof is a keyword, but it's considered an operator. What it does is return how many bytes of storage are used by a specific variable. That information may seem trivial, but it comes into play often in the C language. In this code, the variable a isn't initialized, but you can tell that it's an integer variable and its name is a. The sizeof operator is used in the printf statement. Its value is a long unsigned integer, so %lu is used as the placeholder. The data type is enclosed in parentheses. Again, sizeof is an operator, not a function, but that's how it looks. Build and run. On this system, integer variable a occupies four bytes of storage. Actually, all integer variables occupy four bytes, so that information applies to variable a as well. In this code, I've declared a clutch of variable types, char, short, int, long, float, and double. For each type, the sizeof operator returns the number of bytes the variable consumes in memory. Build and run the code. These values are consistent on many modern computers, but you should never assume any of them. That's why the sizeof operator exists. In this code, a structure stuff is declared. It contains three members, an integer, a float, and a character buffer with room enough for 32 characters, technically 31 characters plus the null character. The sizeof operator returns the number of bytes used in the structure. Because a structure variable isn't declared, I use the sizeof operator on the structure definition itself. Build and run, and the structure occupies 40 bytes of storage. In addition to the number of bytes a variable occupies, another tidbit of information you can obtain is the memory location or address where a variable's data is stored. To fetch a variable's memory location, you use the ampersand operator as shown in this code. Three variables are declared, an integer, a character variable, and a float. These variables aren't initialized because they aren't used in the code, but the program does allocate space for them at specific locations in memory. In the printf statements, the ampersand operator returns the variable's locations. The %p placeholder is used to display that value. It appears as a hexadecimal or base 16 value. Build and run the code. The address' output differ from machine to machine and even their format may appear differently depending on the operating system. By the way, the ampersand operator should be familiar to you if you've been coding C. The scanf function and several other C library functions require this operator. The address of operator plays an important role when it comes to perhaps the most frightening concept of the C language, pointers. But for now, just think of it as the friendly address of operator.

Contents