The Five Basic Data Types
There are five atomic data types in C: character, integer, floating-point, double floating-point, and valueless (char, int, float, double, and void, respectively). As you will see, all other data types in C are based upon one of these types. The size and range of these data types may vary between processor types and compilers. However, in all cases a character is 1 byte. The size of an integer is usually the same as the word length of the execution environment of the program. For most 16-bit environments, such as DOS or Windows 3.1, an integer is 16 bits. For most 32-bit environments, such as Windows NT, an integer is 32 bits.Standard C++ does not specify a minimum size orrange for the basictypes. Instead, it simply states that they must meet certain requirements. For example, Standard C++ states that an int will “have the natural size suggested by the architecture of the execution environment." In all cases, this will meet or exceed the minimum ranges specified by Standard C. Each C++ compiler specifies the size and range of the basic types.
Identifier Names
In C/C++, the names of variables, functions, labels, and various other user-defined objects are called identifiers. These identifiers can vary from one to several characters. The first character must be a letter or an underscore, and subsequent characters must be either letters, digits, or underscores. Here are some correct and incorrect identifier names:
Correct Count test23 high_balance
Incorrect 1count hi!there high...balanc
Variables
A variable is a named location in memory that is used to hold a value that may be modified by the program. All variables must be declared before they can be used. The general form of a declaration is
type variable_list;
Here are some declarations:
int i,j,l;
short int si;
Remember, in C/C++ the name of a variable has nothing to do with its type.
Variables that are declared inside a function are called local variables.
For Ex
void func1(void)
{
int x;
x = 10;
}
void func2(void)
{
int x;
x = -199;
}
For Ex
void func1(void)
{
int x;
x = 10;
}
void func2(void)
{
int x;
x = -199;
}
Unlike local variables, global variables are known throughout the program and may be used by any piece of code. Also, they will hold their value throughout the program's execution. You create global variables by declaring them outside of any function. Any expression may access them, regardless of what block of code that expression is in.
#include <stdio.h>
int count; /* count is global *
Access Modifiers
There are two modifiers that control how variables may be accessed or modified. These qualifiers are const and volatile. They must precede the type modifiers and the type names that they qualify. These modifiers are also referred to as cv-qualifiers.
const Variables
Variables of type const may not be changed by your program. (A const variable can be given an initial value, however.) The compiler is free to place variables of this type into read-only memory (ROM). For example,const int a=10;
creates an integer variable called a with an initial value of 10 that your program may not modify. However, you can use the variable a in other types of expressions. A const variable will receive its value either from an explicit initialization or by some hardware-dependent means.
Constants
Constants refer to fixed values that the program may not alter. Constants can be of any of the basic data types. The way each constant is represented depends upon its type. Constants are also called literals.
Character constants are enclosed between single quotes. For example 'a' and '%' are both character constants. Both C and C++ define wide characters
String Constants
C/C++ supports one other type of constant: the string. A string is a set of characters enclosed in double quotes. For example, "this is a test" is a string. You have seen examples of strings in some of the printf() statements in the sample programs. Although C allows you to define string constants, it does not formally have a string data type. (C++ does define a string class, however.)
You must not confuse strings with characters. A single character constant is enclosed in single quotes, as in 'a'. However, "a" is a string containing only one letter.
Backslash Character Constants
Enclosing character constants in single quotes works for most printing characters. A few, however, such as the carriage return, are impossible to enter into a string from the keyboard. For this reason, C/C++ include the special backslash character constants shown in Table 2-2 so that you may easily enter these special characters as constants. These are also referred to as escape sequences. You should use the backslash codes instead of their ASCII equivalents to help ensure portability.
For example, the following program outputs a new line and a tab and then prints the string This is a test.
#include <stdio.h>
int main(void) {
printf("\n\tThis is a test.");
rreturn 0;
Operators
C/C++ is very rich in built-in operators. In fact, it places more significance on operators than do most other computer languages. There are four main classes of operators: arithmetic, relational, logical, and bitwise. In addition, there are some special operators for particular tasks.The Assignment Operator
variable_name = expression;
Multiple Assignments x = y = z = 0;
Increment and Decrement C/C++ includes two useful operators not generally found in other computer languages. These are the increment and decrement operators, ++ and −−. The operator ++ adds 1 to its operand, and −−subtracts one. In other words:
x = x+1;
is the same as
++x;
and
37
x = x-1;
is the same as
x--;
Both the increment and decrement operators may either precede (prefix) or follow (postfix) the operand. For example,
x = x+1;
can be written
++x;
or
x++;
Operators
The & and * Pointer
Operators A pointer is the memory address of some object. A pointer variable is a variable that is specifically declared to hold a pointer to an object of its specified type. Knowing a variable's address can be of great help in certain types of routines. However, pointers have three main functions in C/C++. They can provide a fast means of referencing array elements. They allow functions to modify their calling parameters. Lastly, they support linked lists and other dynamic data structures. Chapter 5 is devoted exclusively to pointers. However, this chapter briefly covers the two operators that are used to manipulate pointers.
The first pointer operator is &, a unary operator that returns the memory address of its operand. (Remember, a unary operator only requires one operand.) For example,
m = &count;
places into m the memory address of the variable count. This address is the computer's internal location of the variable. It has nothing to do with the value of count. You can think of & as meaning "the address of." Therefore, the preceding assignment statement means "m receives the address of count." To better understand this assignment, assume that the variable count is at memory location 2000. Also assume that count has a value of 100. Then, after the previous assignment, m will have the value 2000. The second pointer operator is *, which is the complement of &. The * is a unary operator that returns the value of the variable located at the address that follows it. For example, if m contains the memory address of the variable count,
q = *m;
places the value of count into q. Now q has the value 100 because 100 is stored at location 2000, the memory address that was stored in m. Think of * as meaning "at address." In this case, you could read the statement as "q receives the value at address m." Unfortunately, the multiplication symbol and the "at address" symbol are the same, and the symbol for the bitwise AND and the "address of" symbol are the same. These operators have no relationship to each other. Both & and * have a higher precedence than all other arithmetic operators except the unary minus, with which they share equal precedence. Variables that will hold memory addresses (i.e., pointers), must be declared by putting * in front of the variable name. This indicates to the compiler that it will hold a pointer. For example, to declare ch as a pointer to a character, write
char *ch;
Here, ch is not a character but a pointer to a character—there is a big difference. The type of data that a pointer points to, in this case char, is called the base type of the pointer. However, the pointer variable itself is a variable that holds the address to an object of the base type. Thus, a character pointer (or any pointer) is of sufficient size to hold an address as defined by the architecture of the computer that it is running on. However, remember that a pointer should only point to data that is of that pointer's base type.You can mix both pointer and nonpointer variables in the same declaration statement. For example,
int x, *y, count;
declares x and count as integer types and y as a pointer to an integer type. The following program uses * and & operators to put the value 10 into a variable called target. As expected, this program displays the value 10 on the screen.
#include <stdio.h>
int main(void) {
int target, source; int *m;
source = 10;
m = &source;
target = *m;
printf("%d", target);
return 0;
}
Type Conversion in Expressions
When constants and variables of different types are mixed in an expression, they are all converted to the same type. The compiler converts all operands up to the type of the largest operand, which is called type promotion. First, all char and short int values are automatically elevated to int. (This process is called integral promotion.) Once this step has been completed, all other conversions are done operation by operation, as described in the following type conversion algorithm:
Comments
Post a Comment