ANSI C refers to ISO/IEC C. The source code for this Module is: C & C++ source codes and the practice worksheets: C & C++ basic data types and the C & C++ standard input, scanf()/scanf_s().
C / C++ programs consist of functions (routines/procedures/methods), one of which must be main() for execution point to start. Every C / C++ program begins execution at the main() function.
The keywords used in C/C++ have special meaning to the compiler. The programmer can’t use these words for identifiers such as variable names. The following table is a list of keywords used in ANSI C.
Keyword | Description |
auto | An automatic storage class for automatic variable. Normally not explicitly used. |
break | Used to force an immediate exit from while, for, do loops and switch-case statement. |
case | A label used together with switch statement for selection. |
char | A single byte data type, capable holding one character in the character set. |
const | A qualifier used to declare variable to specify that its value will not be changed. |
continue | Related to break statement, causes the next iteration of the enclosing for, while or do loop to begin. Applies only to loops, not to switch statement. |
default | An optional label used together with case label. When there is no case expression matched, default label expression will be executed. |
do | Used in do-while loop, repetition where the test condition is at the end of the loop body. |
double | A double-precision floating point. |
elif | #elif. Preprocessor statement for else-if. |
else | Used together with if (if-else) for conditional execution. |
endif | #endif. Preprocessor statement for end-if. |
enum | Used in declaring enumeration constant. Enumeration is a list of constant integer values. |
extern | External storage class. External to all function or globally accessible variable. Variable declared with extern can be accessed by name by any function. |
float | Used when declaring floating-point data type. |
for | Used in the repetition loop. |
goto | A program control statement for branching/jumping to. |
if | Used for conditional execution, standalone or with else. #if used for conditional inclusion of the preprocessor directive. |
ifdef | #ifdef, if defined; test whether a name is defined. |
ifndef | #ifndef, if not defined; test whether a name is not defined. |
int | An integer data type, the size of normal integers. |
long | A qualifier (long and short) applied to basic data types. short – 16 bits, long-32 bits, int either 16 or 32 bits. |
register | Another storage class specifier. Used to advise the compiler to place the variables in machine’s processor register instead of machine’s memory but it is not a mandatory for the compiler. |
return | Used to return a value from the called function to its caller. Any expression can follow return. The calling function is free to ignore the returned value and can be no expression after return (no value is returned). For main(), return will pass to system environment, operating system if there is no error. |
short | A qualifier (long and short) applied to basic data types. short – 16 bits, long-32 bits, int either 16 or 32 bits. |
signed | A qualifier may be applied to char or any integer. For example, signed int. Including the positive and negative integers. For example, integer equivalent range for signed char is -128 and 127 (2’s complement machine). |
sizeof | An operator. Shows the number of bytes (occupied or) required to store an object of the type of its operand. The operand is either an expression or a parenthesized type name. |
static | A storage class specifier. Local variables (internal variables) that retain their values throughout the lifetime of the program. Also can be applied to external variables as well as functions. Functions declared as static, its name is invisible outside of the file in which it is declared. For an external variables or functions, static will limit the scope of that objects to the rest of the source file being compiled. |
struct | A structure specifier for an object that consist a sequence of named members of various types. |
switch | Used in a selection program control. Used together with case label to test whether an expression matches one of a member of case’s constant integer and branches accordingly. |
typedef | Used to create new data type name. |
union | A variable that may hold (at different time) objects of different types and sizes. If at the same time, use struct. |
unsigned | A qualifier may be applied to char or any integer. For example, unsigned int. Including the positive integers or zero. For example, integer equivalent range for unsigned char is 0 and 255. |
void | Data type that specifies an empty set of values or nonexistence value but pointers (pointers to void) may be assigned to and from pointers of type void *. |
volatile | A qualifier used to force an implementation to suppress optimization that could otherwise occur. |
while | Used for conditional loop execution. Normally together with the do. |
Table 2.1: ANSI C Keywords |
The following table is a list of C++ keywords; most of the keywords will be used in C++ Tutorial.
Keywords | Brief descriptions |
asm | Using or inserting assembly language in C++, refer to your compiler documentation support. |
catch | Exception handling generated by a throw keyword. |
bool | To declare Boolean logic variables; that is, variables which can be either true or false. |
class | Define a new class then objects of this class can be instantiated. |
const_cast | To add or remove the const or volatile modifier from a type. |
delete | Destroy an object in memory dynamically, created by using keyword new. |
dynamic_cast | Convert a pointer or reference to one class into a pointer or reference to another class using run time type information (rtti). (Converts a pointer to a desired type. |
explicit | Used to avoid a single argument constructor from defining an automatic type conversion in class declaration. |
false | The Boolean value of "false". |
friend | Declare a function or class to be a friend of another class providing the access of all the data members and member function of a class. |
inline | Asking the compiler that certain function should be generated or executed inline instead of function call. |
mutable | The mutable keyword overrides any enclosing const statement. A mutable member of a const object can be modified. |
namespace | Keyword used to create a new scope. |
new | Dynamically allocate a memory object on a free store, that is an extra memory that available to the program at execution time and automatically determine the object's size in term of byte. |
operator | Declare an overloaded operator. |
private | A class member accessible to member functions and friend functions of the private member's class. |
protected | protected members may be accessed by member functions of derived classes and friends of derived classes. |
public | A class member accessible to any function. |
reinterpret_cast | Replaces casts for conversions that are unsafe or implementation dependent. |
static_cast | Converts types between related types. |
template | Declare how to construct class or function using variety of types. |
this | A pointer implicitly declared in every non-static member function of a class. It points to the object for which this member function has been invoked. |
throw | Transfer control to an exception handler or terminate program execution if appropriate handler cannot be located. |
true | The Boolean value of "true". |
try | Creates a block that containing a set of statements that may generate exceptions, and enables exception handling for any exceptions generated (normally used together with throw and catch). |
typeid | Gets run-time identification of types and expressions. |
typename | Used to qualify an identifier of a template as being a type instead of a value. |
using | Used to import a namespace into the current scope. |
virtual | Declare a virtual function. |
wchar_t | Used to declare wide character variables. |
Table 2.2: C++ Keywords |
The following are C keywords defined by ISO/IEC 9899:1999 (C99).
auto | break | case | char | const | continue | default | do | double | else | enum |
extern | float | for | goto | if | inline | int | long | register | restrict | return |
short | signed | sizeof | static | struct | switch | typedef | union | unsigned | void | volatile |
while | _Bool | _Complex | _Complex | _Imaginary | ... |
|
|
|
|
|
One way to master C/C++ programming is to master the keywords and their usages :o). From time to time when there are new C/C++ standard, new keywords should be introduced and some will be removed.
| 2.3 C/C++ Identifiers
2.4 C/C++ Variables
x = 20; x is lvalue, 20 is rvalue.
int x, y, z; short number_one; long Type0fCar; unsigned int positive_number; char Title; float commission, yield;
|
The following shows declaring and initializing variables examples:
int m, n = 10;
char * ptr = "TESTING";
float total, rate = 0.5;
char user_response = ‘n’;
char color[7] = "green";
Or declare and initialize:
int m, n;
float total, rate;
char user_response;
char color[7];
n = 20;
rate = 4.5;
user_response = ‘n’;
color = "green";
2.5 Basic Data types
|
Data type | Keyword | Bits | Range |
integer | int | 16 | -32768 to 32767 |
long integer | long | 32 | -4294967296 to 4294967295 |
short integer | short | 8 | -128 to 127 |
unsigned integer | unsigned | 16 | 0 to 65535 |
character | char | 8 | 0 to 255 |
floating point | float | 32 | approximately 6 digits of precision |
double floating point | double | 64 | approximately 12 digits of precision |
Table 2.3: Basic data type |
The following tables list the sizes and resulting ranges of the data types based on IBM PC compatible system. For 64 bits, the size and range may not valid anymore☺.
Type | Size (bits) | Range | Sample applications |
unsigned char | 8 | 0 to 255 | Small numbers and full PC character set |
char | 8 | -128 to 127 | Very small numbers and ASCII characters |
enum | 16 | -32,768 to 32,767 | Ordered sets of values |
unsigned int | 16 | 0 to 65,535 | Larger numbers and loops |
short int | 16 | -32,768 to 32,767 | Counting, small numbers, loop control |
int | 16 | -32,768 to 32,767 | Counting, small numbers, loop control |
unsigned long | 32 | 0 to 4,294,967,295 | Astronomical distances |
long | 32 | -2,147,483,648 to 2,147,483,647 | Large numbers, populations |
float | 32 | 3.4-1038 to 3.41038 | Scientific (7-digit precision) |
double | 64 | 1.7-10308 to 1.710308 | Scientific (15-digit precision) |
long double | 80 | 3.4-104932 to 1.1104932 | Financial (18-digit precision) |
near pointer | 16 | Not applicable | Manipulating memory addresses |
far pointer | 32 | Not applicable | Manipulating addresses outside current segment |
Table 2.4: C++ 16-bit data types, sizes, and ranges |
Type | Size (bits) | Range | Sample applications |
unsigned char | 8 | 0 to 255 | Small numbers and full PC character set |
char | 8 | -128 to 127 | Very small numbers and ASCII characters |
short int | 16 | -32,768 to 32,767 | Counting, small numbers, loop control |
unsigned int | 32 | 0 to 4,294,967,295 | Large numbers and loops |
int | 32 | -2,147,483,648 to 2,147,483,647 | Counting, small numbers, loop control |
unsigned long | 32 | 0 to 4,294,967,295 | Astronomical distances |
enum | 32 | -2,147,483,648 to 2,147,483,647 | Ordered sets of values |
long | 32 | -2,147,483,648 to 2,147,483,647 | Large numbers, populations |
float | 32 | 3.4 -1038 to 1.71038 | Scientific (7-digit) precision) |
double | 64 | 1.7 -10308 to 3.410308 | Scientific (15-digit precision) |
long double | 80 | 3.4 -104932 to 1.1104932 | Financial (18-digit precision) |
Table 2.5: C++ 32-bit data types, sizes, and ranges |
We are very familiar with integer constants that are the base 10 numbers, 0 – 9. There are other bases such as 16, 8 and 2 numbers that we will encounter when learning programming.
Octal integer constants must start with 0 followed by any combination of digits taken from 0 through 7. For examples:
0 07 0713 // represent octal numbers
Hexadecimal integer constants must start with 0x or 0X (capital hexadecimal) followed by any combination of digits taken from 0 through 9 and uppercase letters A through F. For examples:
0x 0x8 0XADC 0X2FD // represent hexadecimal numbers
The literal data-type qualifiers bring different means for same constant data. For example:
- 75 mean the integer 75, but 75L represents the long integer 75.
- 75U means the unsigned integer 75.
- 75UL means the unsigned long integer 75.
- 4.12345 mean the double value 4.12345, but 4.12345F represents the float value 4.12345.
The The following table lists the suffix that defined by ISO, also implemented by Microsoft. You can find their usages in variable declaration, the standard input function (scanf()/scanf_s()) and standard output function, printf()/printf_s()). You will find the examples in the C lab worksheet practice.
Suffix | Decimal Constant | Octal or Hexadecimal Constant |
none | int long int long long int | int unsigned int long int unsigned long int long long int unsigned long long int |
u or U | unsigned int unsigned long int unsigned long long int | unsigned int unsigned long int unsigned long long int |
l or L | long int long long int | long int unsigned long int long long int unsigned long long int |
Both u or U and l or L | unsigned long int unsigned long long int | unsigned long int unsigned long long int |
ll or LL | long long int | long long int unsigned long long int |
Both u or U and ll or LL | unsigned long long int | unsigned long long int |
Table 2.X: suffix that defined by ISO |