Introduction to Programming C/Lectures/Variables

From Wikiversity
Jump to navigation Jump to search

Defining a variable, step by step[edit | edit source]

So, lets go step by step through defining a variable.

  1. Determine the real type of the variable. Remember to be detailed enough that what is being stored is obvious.
  2. Create a good, descriptive name for that type.
  3. Map the real type to a storage type via typedef or enum
  4. Create a good, descriptive name for the variable
  5. Determine what scope you want for the variable. This is usually local. If a value is used in multiple functions and persists between those functions, use file. If it persists and is accessed by multiple files, make it global.
  6. Declare it in the right place for the scope.

What comes below the basic data types ?[edit | edit source]

Each of the primitive data types learned here are one of two types: integral or floating point. Integral types store a literal integer in RAM - the sequence 1001 0011 1101 0111 in RAM represents exactly that number, in base-2, for the purpose of math performed upon it. Any int type (eg. int, long int, uint, int8_t, uint8_t etc.) or char type (eg. char, signed char, unsigned char) is an integral type. The reason a char works this way is because, when printed, the machine writes the values to memory containing the file (if you use a unix-like OS. The file is stored in RAM). Then when displaying this, the GPU displays in the correct position the pixels for each character in memory that is visible through the window. Note that a character can be by default either signed or unsigned - it doesn't technically matter, since it should only be used for characters, unless you want to use the int8_t/uint8_t,which are defined as signed char/unsigned char (if you use an 8-bit-char CPU). Note that these types can only store whole numbers - by which it is meant their memory, when used in any sort of math, as treated as representing a whole number, by which in turn it is meant that math the CPU does using only these types produces an output as if they were treated as being done with the numbers an base-2 integers. If you use an integral and a floating-point type, then the integral type is first converted into its representation as a floating-point number. The floating-point type in C is represented by all float and double types. The same keywords are available for integral and floating-point types, excluding the signedness factor, due to the way the number is stored in memory. Generally, a float is stored, for an n-bit number, as follows: 1st bit: represents the signedness, can be viewed as a plus-or-minus. This method of representation is why all floating-point types MUST be signed. 2nd to xth bits: represents the exponent. The radix to which the exponent is taken is system-defined. xth to nth bits: represents the significand. All floating-point math (should) take in only floating-point types, and output a floating point type. All floating-point math is done in a special unit of the CPU, called the FPU.

Naming again[edit | edit source]

Since naming is by far the most important thing to take away from this lesson, I'm going to hit it again. Choosing good names is one of the most important thing to do when writing code. It will do more for making your code easy to read and understand than almost anything else. Its important to get in good habits now, too many programmers learn how to code and use x and y for all their variables. Lets use an example. Which of these give you the most information:

  1. int x;
  2. int myHeight;
  3. inches x;
  4. inches myHeight;

The first tells you nothing. The second tells you whats being stored, but not how its represented (inches? centimeters? microns?). The third tells you how something is being stored, but not what it is. The final one tells us not only what's being stored, but how its represented. Now imagine a large program with 10,000 variables. Which do you think will take you less time to understand? So be kind to your fellow programmers, and use descriptive names.