Saturday, March 1, 2008

C Programming Part 2: Data Types, Variables, and Constants

How are data stored in C?

  • Data are stored as variables or constants.
  • Variables are just memory locations that hold specified values/data.
  • Variables and constants are assigned a data type during creation.
  • Data types limit the possible values the variable/constant can have.
  • There are data type modifiers which will further limit or increase the range of values.
  • The modifiers are short, long, signed, and unsigned. They are placed before the data type.
  • Examples: short int, unsigned char, long float.
  • Some data type modifiers can also be used as data types. They just act like int.

C Data Types
TypeDescriptionBitsRange
charcharacters8-128 to 127
unsigned charsmall unsigned int80 to 255
intintegers32-2,147,483,648 to +2,147,483,647
unsigned intunsigned integers320 to 4,294,467,295
shortsmall integers16-32,768 to 32,767
unsigned shortunsigned small integers160 to 65,535
longintegers32-2,147,483,648 to +2,147,483,647
unsigned longunsigned integers320 to 4,294,467,295
floatfloating point #s32
doublefloating point #s (greater precision)64
long doublefloating point #s (even greater precision)96


Variables

  • The format of declaring a variable is: data_type variable_name; (i.e. int one;)
  • The first character of a variable name MUST be a letter but the rest can be a letters, numbers or underscores(_) .
  • Variables are case sensitive.
  • The maximum length of the name will depend on the compiler. Although, there is a C standard that states that the initial 63 characters are significant for an internal identifier (name used for variables, types, or labels). The standard for an external identifier is that the first 31 characters are significant.
  • It is a good practice to initialize (assign it a base value) a variable when you first declare it. It is known as defining a variable when you do both the initialization and declaration. The main small difference between the two is that defining it means that memory space is taken up. In a declaration, no memory is used and only the data type is mentioned.
  • Ex. Declaration: int one; | Definition: int one = 1;
  • Variables are considered local, global, or external depending on where they are defined and used. This will be discussed more in a later section.
  • It is possible to declare all variables on one line (i.e. int one, two, three;). Generally, it is a better practice to declare or define them on separate lines.
Constants
  • Constants are data that will not be modified.
  • Constants are generally defined near the very top of the code and its name is all capitalized.
  • They do not take up any variable storage space(memory).
  • The format for defining a constant is: #define CONSTANT_NAME constant_value
  • Constants can be assigned decimal, octal(put a 0 before the value; i.e. 0742), or hexadecimal(put a 0x before the value; i.e. 0xA2) values.
  • To assign a data type(long or float) to a constant, put a L or F after the constant value.
  • Ex. #define PI 3.14F
Special Constants
  • Another type of constant is an enumeration constant. It is created in this format: enum enumeration_type {constant_name1, constant_name2,...}
  • The first constant in a enumeration will be given the value 0 unless otherwise specified. The second constant will have the value of the previous constant plus one. Thus the second constant will be given the value 1 unless it or the constant before was assigned a specific value. This procedure continues on for the rest of the constants in the enumeration.
  • Ex. enum boolean {FALSE, TRUE, MAYBE=5, NO}; FALSE will have a value of 0 and TRUE will have a value of 1. MAYBE will have a value of 5 and NO will have a value of 6.
  • Constants can also be a character(using ''), or a string("").
  • Some character constants can act as escape sequences(used to represent action or other characters that can't be represented normally).

Escape Sequences
SequenceDescription
\aAlert
\bBackspace
\fFormfeed
\nNewline
\rCarriage return
\tHorizontal tab
\vVertical tab
\\Backslash
\?Question mark
\'Single quote
\"Double quote
\0Null (automatically at end of string)
\oooASCII char. in octal
\xhhASCII char. in hex

No comments: