Overview

VARIABLE SCOPE

Auto variables declared within a block are scoped to that block, can be accessed only within that block and go out of existance when that block ends

void main()
{
  int x = 8;	// auto variable
  cout << "\nOutside block local x = " << x;
  {
    cout << "\nIn block x = " << x;
    int x = 9;
    cout << "\nIn block x = " << x;
  }
  cout << "\nOutside block local x = " << x;
}

RESULTS OF EXECUTION

Outside block local x = 8

In block x = 8

In block x = 9

Outside block local x = 8


DATA MODIFIERS (QUALIFIERS)

The basic modifiers are :

  • long

Use as a size qualifier for int and unsigned int variables. Note that long alone means signed long int. A long qualifier indicates that the integer is at least 32 bits, and is often twice as long as an int.

  • short

Use as a size qualifier for int and unsigned int variables. Note that short alone means signed short int. A short qualifier indicates that the integer type is at least two bytes in size; on some systems it may be longer.

  • signed

Use to indicate that data is stored in an integral type (int, char) is signed. For example, a signed char can take values between -127 and +127. Note that the int and char types are signed by default.

  • unsigned

Use with data types char, int, short int and long int to tell the compiler that the variable use to store non-negative numbers only. This effectively doubles the maximum value that can be stored in that variable.

EXERCISE 2

Use the qualifiers long, short, signed and unsigned with the appropriate variable types, initialise them with data of your choice and print the results to the screen as a list.

It's chapter two and incorrect terminology is well into use! None of these are qualifiers - they are types


STRING FUNCTIONS

There are a number of string manipulation functions that come as standard with the compiler:

What on earth? These are C string handing functions - what should be here are the methods held in <string> which are far easier to understand as they are far clearer

strcpy()

Purpose :

Use the strcpy() function to copy the null terminated string string2 to the string string1. The terminating null character (\0) of the second string is also copied so that string1 becomes a copy of string2.

Example use :

#include <string.h>

This should be #include <cstring>

char string1[20];
char string2[20];
strcpy (string1,string2);

strcmp()

Purpose :

Use the strcmp() function to compare the strings string1 and string2. The comparison is performed using the ASCII code. The character 'A' (41 hex) therefore, is less than 'a' (61 hex).

Nope - the comparison is not performed using ASCII comparison

Example use :

false is defined as being 1. true is defined as being !false

#include <string.h>
if (strcmp("DUCK","DECK"));	// will return false or
0
if (strcmp("DUCK","DUCK"));	// will return true or a
positive number

false is defined as being 1. true is defined as being !false

Returns :

An integer greater than, equal to or less than 0 depending on whether string1 is greater than, less than or equal to string2.


strcat()

Purpose:

Use the strcat() function to append a copy of the string source to the of dest.

Example use:

#include <string.h>
char str1[20] = "File";
char str2[20] = " NOT FOUND";	// note the leading space
strcat(str1,str2);
cout << str1;		// outputs File NOT FOUND

WARNING!

The array size is not checked by the function. Exceeding the size of an array produces unpredictable results. The following code section will not work:

#include <string.h>
#include <conio.h>
char str1[] = "FILE";		// creates a char array of 5 characters
                                        // including the NULL character
char str2[] = " NOT FOUND";	// creates a char array of 11		                   			// characters inc. the NULL character.
strcat(str1,str2);		// FAILURE. 15 characters cant be stored
                                // in an array designed to hold 5!
getch();

I can't get it to compile no matter what I do as I don't have the conio.h header on my machines

strlen()

Purpose :

Use the strlen() function to find the length of string in byes, not counting the null character

Example use :

#include <string.h>
char str1[20] = "FILE OPEN";
char str2[20] = " ERROR";
strcat(str1,str2);
cout << strlen(str1);	// will output 15 as the null is not counted.

Returns :

The number of characters in string that preceed the terminating null character.