|< Introduction | Main | Basic Data Type 2 >| Site Index | Download |


 

 

 

 

MODULE 2

C AND C++ PROGRAM CONSTRUCT

AND BASIC DATA TYPES 1

 

 

 

My Training Period: xx hours

 

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().

 

The C/C++ programming skills that supposed to be acquired:

 

 

2.1    What Is A Program?

2.2    Keywords / Reserved Words

 

 

 

 

 

 

 

 

 

 

 

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

 

 

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

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

 ...

 

 

 

 

 

 

 

 

2.3    C/C++ Identifiers

  • Name that simply references to memory locations, which can hold values (data).

  • Are formed by combining letters (both upper and lowercase), digits (0–9) and underscore ( _ ).

  • Rules for identifier naming are:

  1. The first character of an identifier must be a letter, an underscore ( _ ) also counts as a letter.

  2. The blank or white space character is not permitted in an identifier.

  3. Can be any length.  Internal identifier (do not have the external linkage) such as preprocessor macro names at least the first 31 characters are significant, also implementation dependent.

  4. Reserved words/keywords and characters such as main and # also cannot be used.

2.4      C/C++ Variables

  • Identifier that value may change during the program execution.

  • Every variable stored in the computer’s memory has a name, a value and a type.

  • All variable in a C/C++ program must be declared before they can be used in the program.

  • A variable name in C/C++ is any valid identifier, and must obey the rules mentioned above.

  • Initializing a variable means, give a value to the variable, that is the variable’s initial value and can be changed later on.

  • Variable name are said to be lvalue (left value) because they can be used on the left side of an assignment operator.

  • Constant are said to be rvalue (right value) because they only can be used on the right side of an assignment operator.  For example:

x = 20;

x is lvalue, 20 is rvalue.

  • The evaluation is done from right to left, that is right value to left value.

  • Note that lvalue can also be used as rvalue, but not vice versa.

  • Notation used in C/C++ can be Hungarian Notation or CamelCase Notation.  The information for these notations can be found in C/C++ Notations.

  • The following shows variable declaration examples:

int             x, y, z;   

short        number_one;

long         Type0fCar;

unsigned int     positive_number;

char         Title;

float          commission, yield;

 

The variable declaration general form:

 

data_type    variable_list;

 

Note the blank space.

 

int     m, n = 10;

char * ptr = "TESTING";

float   total, rate = 0.5;

char    user_response = ‘n’;

char    color[7] = "green";

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

  • Why we need to learn data types?  Every variable used in program hold data, and every data must have their own type.   It is the way how we can ‘measure’ the variable’s data value as exist in the real world.  Further more by knowing the data range, we can use data efficiently in our program in term of memory management (storage allocation) aspects.

  • For example, no need for us to reserve a lot of storage space such as a long data type if we just want to store a small amount of data, let say, int data type.

  • Every data in C/C++ has their own type.  There are basic data type and derived data type.  This Module deals with basic data type.

  • There are two kinds of basic data type: integral (integer value) and floating (real number).  char data type classified in integral type.

  • Derived data types will be presented in another Module.  Derived data type including the aggregate data type is constructed from basic data type such as arrays, functions, pointers, structures, unions and other user defined data types.  Basic data type (int, char and float) and their variation are shown in Table 2.3. 2.4 and 2.5.

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

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

0     07   0713     // represent octal numbers

                                      0x   0x 0XADC     0X2FD    // represent hexadecimal numbers

-    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.

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

 

 

 

C & C++ programming tutorials

 

 

 

 

 

 

 

 

 

 

 

 

Further C and C++ reading and digging:

  1. The ASCII, EBCDIC and UNICODE character sets reference Table can be found here: Character sets Table.
  2. Check the best selling C/C++ books at Amazon.com.

 

 

 

 

 

 

 

|< Introduction | Main | Basic Data Type 2 >| Site Index | Download |


Program Structure & Basic Data Type:  Part 1 | Part 2 | Part 3 | Part 4 |