Q&A
-
long int variables hold bigger numbers, so why not always use them instead of int variables?
-
A long int variable takes up more RAM than the smaller int. In smaller programs, this doesn't pose a problem. As programs get bigger, however, you should try to be efficient with the memory you use.
-
What happens if I assign a number with a decimal to an integer?
-
You can assign a number with a decimal to an int variable. If you're using a constant variable, your compiler probably will give you a warning. The value assigned will have the decimal portion truncated. For example, if you assign 3.14 to an integer variable called pi, pi will only contain 3. The .14 will be chopped off and thrown away.
-
What happens if I put a number into a type that isn't big enough to hold it?
-
Many compilers will allow this without signaling any errors. The number is wrapped to fit and therefore won't be correct. For example, if you assign 32768 to a two-byte signed variable of type short, the variable would really contain the value -32768. If you assign the value 65535 to this variable, it really contains the value -1. Subtracting the maximum value that the field will hold generally gives you the value that will be stored.
-
What happens if I put a negative number into an unsigned variable?
-
As the preceding answer indicated, your compiler might not signal any errors if you do this. The compiler does the same wrapping as if you assigned a number that was too big. For instance, if you assign -1 to an unsigned int variable that is two bytes long, the compiler will put the highest number possible in the variable (65535).
-
What are the practical differences between symbolic constants created with the #define directive and those created with the const keyword?
-
The differences have to do with pointers and variable scope. Pointers and variable scope are two very important aspects of C programming and are covered on Days 9 and 12.