Guides:C/C Crash Course/Variables and Basic Data Types

From CoderGuide

Jump to: navigation, search

Back to TOC

Variables and Basic Data Types

In algebra, just as in programming languages, we use symbols to represent variables, and constant values. Unlike algebra, variables in C, and other languages, have a limited size, and type of data they can store. These types include floating point numbers, integers, strings (arrays of characters), single characters, and a reference to some place in computer memory (see pointers below).

Here is a table of data types, and what kind of data they store (The minimum range is the minimum level of precision the data type can store as defined by the ANSI C standard-- but your system may be able to support a far greater range for these data types):

C/C++ Data TypeType of data storedMinimum range (can be larger/more accurate on other systems)
intintegers-32,768 to 32,767
shortintegerssame as int (usually smaller than int, if int is larger that 16-bits)
short intintegerssame as short
signedintegerssame as int
signed intintegerssame as int
unsignedpositive integers0 to 65,535
unsigned intpositive integerssame as unsigned
short unsignedpositive integerssame as unsigned (usually smaller than unsigned, if unsigned is larger than 16 bits)
short unsigned intpositive integerssame as unsigned short
longintegers-2,147,483,648 to 2,147,483,647
long intintegerssame as long
signed longintegerssame as long
signed long intintegerssame as long
unsigned longpositive integers0 to 4,294,967,295
unsigned long intpositive integerssame as unsigned long
long longintegersC99 addition, 64-bit integer -9,223,372,036,854,775,807 to 9,223,372,036,854,775,808
unsigned long longpositive integersC99 addition, 64-bit integer 0 to 18,446,744,073,709,551,615
charsingle character, really small signed integerany 8-bit character/1-byte or -128 to 128
unsigned charsingle character, really small unsigned integerany 8-bit character/1-byte, or 0 to 255
floatfloating point numbersix digits of precision, ±1e±37
doublefloating point numbertypically 15 digits of precision, and ±1e±308, and an even mantissa/significant digits
long doublefloating point numbertypically 19 digits of precision, and ±1e±4932, and an even mantissa/significant digits

Your compiler may support more data types, and may have even larger ranges. For instance, GCC, and other C99 (named for the year 1999) compliant C compilers, support the long long type, which is at least 64-bits bits long,

However, the actual size of these data types vary from system to system, and from compiler to compiler. For instance, GCC on 32-bit systems, an int and a long are the same size, 32-bits. GCC on 64-bit systems, int and long are 64-bits by default. This is perfectly fine within the definition of ANSI C, but it is something you should be aware of if you ever want to port your programs to another system (which you will probably want to do at some point).

To define a variable to use in your program, you must declare it. First, you specify the type of variable your declaring, then give it a name (or list of variable names you want to use of that type), end then end your statement with a semicolon, like so:

int a;
float c,d,e;
long x;
long double y;
short z;

C is case sensitive, that means that the label 'a' and 'A' are are treated as two different labels by the C compiler. A label may contain the the a-z, A-Z, 0-9, and _ (underscore). A label name may not begin with number. That means that a3 is fine for a label name, but 3a is not. Also, you should not use labels longer than 32 character. Some C compilers allow labels to be longer that 32 characters, but many don't. If the label is too long, then the extra characters at then end of the name are ignored by the compiler.

To assign values to these variables, we use the equal sign. We can also add an 'L', or an 'l', at the end to let the compiler know that the number that follows is a long or long double (if you don't the compiler will assume that all numbers are ints or either floats or doubles)-- that is, we're adding the 'L' suffix to the number (the upper case 'L' is easer to read, and less likely to be confused with the number '1', so we use the upper case 'L' instead of the lower case 'l'). We can also put a zero in front of the number to tell the compiler that we want to enter in an octal number (base 8), or a 0x to tell the compiler that we want to enter in a hexadecimal number (base 16)-- that is to say, we're adding either the, 0, or 0x, prefix to a number. To specify a exponent on a floating point number, you add a 'e' or an 'E' to the end, followed by the exponent. This 'e' is the same thing at "x10" in scientific notation. Graphing calculators use the same notation for floating point numbers. Here are a few examples:

int x=1234; /*you can assign values to to a variable
              when it is declared*/
 
int1=123456; /*a normal integer*/
longint1=1234567890L; /*a long integer*/
hexint=0xAF03; /*a hexadecimal integer*/
octal=0773; /*an octal number*/
floating=1.2342e-10; /*a floating point number*/
pi=3.1415926535897932384L /*a long double */
 
l_long=1234437439955092953LL /*A long long number*/

Caution: It is very important to remember that you must always initialize (assign a value to) a variable before you use it. This is because that variable may contain any random data that was in memory at the time of it's creation. This becomes even more critical when working with pointers as you can cause a program to crash if you do not initialize them before hand (Microsoft operating systems may issue a "general protection fault" error and can become unstable, Linux will generally kill the program if it accesses memory that doesn't belong to it).

Personal tools