W2 - TYPES (AND VARIABLES); A SIMPLE CALCULATION; EXPRESSIONS Flashcards
W2-1 Why C is a “TYPES PROGRAMMING LANGUAGE”
Each type has its own rules:
▪ How values are stored in memory
▪ What operations can be used on that type
W2-2 Two common MAIN groups of data types?
(a) Integral: Whole numbers: 1, 13, 157 …
(b) Floating-Point: Fractional numbers: 1.1,13.304, 157.54648 …
W2-3 The COMPONENTS of the CPU?
(a) ALU (Arithmetic and Logic Unit)
▪ Integral types only (whole numbers): int, char
(b) FPA (Floating Point Accelerator)
▪ Floating-point types only (fractional numbers): float, double
W2-4: Four Common data types
char (1-byte)
int (4-bytes)
=> intergral => ALU
float (4-bytes)
double (8-bytes)
=> floating-point => FPA
*** further fine-tune memory allocation size by using SIZE SPECIFIERS
W2-5: TYPES
_ Integral Size Specifiers (int type only) 1/ short (int): 2 bytes 2/ int: 4 bytes 3/ long (int): 4 bytes 4/ long long (int): 8 bytes => Not applicable to char
_ Floating-Point Size Specifier (double type only)
1/ double: 8 bytes
2/ long double: 8 bytes
=> Not applicable to float
W2-6: SPECIFIERS
SPECIFIERS are an available option to ensure a MINIMUM memory allocation size is reserved when the variable is declared
W2-7: Constant Value (const)
▪ A value which cannot be changed
▪ Reserved keyword/qualifier: const
W2-8: Variable
A VARIABLE is a named placeholder (identifier) for the STORAGE and retrieval of data held in memory
W2-9: Type: char (1 byte or 8-bits)
_ Treated as an INTEGRAL type (integer) using a collating sequence table where each CHARACTER or symbol is given a unique INTEGER value
_ Collating Systems:
1/ ASCII (most popular and aligns with newer Unicode standards)
2/ EBCDIC
W2-10: Variable Naming Restrictions
▪ 1st character must start with:
– UPPER/lower case letter OR
– Underscore (_)
=> Invalid: int 3LegStool = 1;
▪ 2nd character onward can contain: – UPPER/lower case letters – Numbers (0-9) – Underscore (_) => Invalid: int three-Leg-Stool = 1;
▪ Length of name < 32 characters (to ensure portability)
▪ Name cannot be a C reserved word
W2-11: Character Constants
▪ Digit or letter enclosed in SINGLE quotes: ‘A’ (this is the preferred way)
▪ Decimal value (base 10): 65
– ASCII table value for uppercase letter A
▪ Hexidecimal value (base 16): 0x41
– ASCII table value for uppercase letter A
EX:
1/ char exQuote = ‘A’; // system will lookup the collating sequence value
2/ char exDec = 65;
3/ char exHex = 0x41; // “0x” denotes hexadecimal values
W2-12: Escape Sequences
▪ These SPECIAL constants define ACTIONS and symbols
▪ The BALCKLASH () identifies the ESCAPE sequence
W2-13: Constant String Literals
▪ A sequence of characters enclosed in DOUBLE quotes
EX: printf(“Welcome to IPC144\n”);
_ Also includes an ESCAPE sequence for a NEWLINE constant character (\n)
W2-14: Input
scanf( format, address );
▪ Procedure used to capture values from the standard input DEVICE (keyboard)
▪ format argument: The expected INPUT value type
▪ address argument: STORES the input value to the specified variable’s ADDRESS
EX:
int age;
scanf(“%d”, &age);
• Prefix & specifies the memory address of a variable
• Age is the variable to store the value to
W2-15: Output
printf( format, expression );
▪ Procedure used to DISPLAY values to the standard output device (monitor)
▪ format argument: Specifies how to CONVERT data into READABLE text
▪ expression argument: Supplies the data value to be TRANSALATED to the OUTPUT device
EX:
int age = 19;
printf(“Your age is %d”, age);
_ Source variable to be converted to text
NOTE: There is NO (&) used as this procedure requires a value not an ADDRESS