We cover below parts.
- Variable names
- Data types and sizes
- Constants
- Declarations
- Arithmetic operators
- Relational and logical operators
- Type conversions
- Increment and decrement operators
- Bitwise operators
- Assignment operators and expressions
- Conditional expressions
1. Variable names
There are some restrictions on the names of variables and symbolic constants. Names are made up of letters and digits; the first character must be a letter. The underscore ``_'' counts as a letter;
Don't begin variable names with underscore, however, since library routines often use such names. Upper and lower case letters are distinct, so x and X are two different names. Traditional C practice is to use lower case for variable names, and all upper case for symbolic constants.
Keywords like if, else, int, float, etc., are reserved: you can't use them as variable names.
2. Data types and sizes
There are only a few basic data types in C:
char - a single byte, capable of holding one character in the local character set
int - an integer, typically reflecting the natural size of integers on the host machine
float - single-precision floating point
double - double-precision floating point
In addition, there are a number of qualifiers that can be applied to these basic types. short and long apply to integers:
short int sh;
long int counter;
The intent is that short and long should provide different lengths of integers where practical; int will normally be the natural size for a particular machine. short is often 16 bits long, and int either 16 or 32 bits. Each compiler is free to choose appropriate sizes for its own hardware.
The qualifier signed or unsigned may be applied to char or any integer. unsigned numbers are always positive or zero, and obey the laws of arithmetic modulo 2n, where n is the number of bits in the type. So, for instance, if chars are 8 bits, unsigned char variables have values between 0 and 255, while signed chars have values between -128 and 127 (in a two's complement machine.) Whether plain chars are signed or unsigned is machine-dependent, but printable characters are always positive.
The standard headers <limits.h> and <float.h> contain symbolic constants for all of these sizes.
3. Constants
An integer constant like 1234 is an int. A long constant is written with a terminal l (ell) or L, as in 123456789L; Unsigned constants are written with a terminal u or U, and the suffix ul or UL indicates unsigned long.
Floating-point constants contain a decimal point (123.4) or an exponent (1e-2) or both; their type is double, unless suffixed. The suffixes f or F indicate a float constant;
The value of an integer can be specified in octal or hexadecimal instead of decimal. A leading 0 (zero) on an integer constant means octal; a leading 0x or 0X means hexadecimal.
A character constant is an integer, written as one character within single quotes, such as 'x'.
The character constant '\0' represents the character with value zero, the null character.
A constant expression is an expression that involves only constants. Such expressions may be evaluated at during compilation rather than run-time
i.e
#define MAXLINE 1000
char line[MAXLINE+1];
A string constant, or string literal, is a sequence of zero or more characters surrounded by double quotes, as in
"I am a string". String constants can be concatenated at compile time:
"hello, " "world"
is equivalent to
"hello, world"
A string constant is an array of characters. The internal representation of a string has a null character '\0' at the end.
The standard library function strlen(s) returns the length of its character string argument s, excluding the terminal '\0'.
Be careful to distinguish between a character constant and a string that contains a single character: 'x' is not the same as "x". The former is an integer, used to produce the numeric value of the letter x in the machine's character set. The latter is an array of characters that contains one character (the letter x) and a '\0'.
There is one other kind of constant, the enumeration constant. An enumeration is a list of constant integer values, as in
enum boolean { NO, YES };
Enumerations provide a convenient way to associate constant values with names, an alternative to #define.
4. Bitwise operators
C provides six operators for bit manipulation; these may only be applied to integral operands, that is, char, short, int, and long, whether signed or unsigned.
& - bitwise AND
| - bitwise inclusive OR
^ - bitwise exclusive OR
<< - left shift
>> - right shift
~ - one's complement (unary)
AND operator & is often used to mask off some set of bits.
OR operator | is used to turn bits on.
exclusive OR operator ^ sets a one in each bit position where its operands have different bits, and zero where they are the same.
We must distinguish the bitwise operators & and | from the logical operators && and ||. For example, if x is 1 and y is 2, then x & y is zero while x && y is one.
The shift operators << and >> perform left and right shifts of their left operand by the number of bit positions given by the right operand, which must be non-negative.
i.e x << 2 (equivalent to x^2).
The unary operator ~ yields the one's complement of an integer; that is, it converts each 1-bit into a 0-bit and vice versa.
5. Assignment Operators and Expressions
An expression such as
i = i + 2
can be compressed as
i += 2
The operator += is called an assignment operator.
Most binary operators have a corresponding assignment operator op=, where op is one of
+ - * / % << >> & ^ |
If expr1 and expr2 are expressions, then
expr1 op= expr2
is equivalent to
expr1 = (expr1) op (expr2)
i.e. x *= y + 1
means
x = x * (y + 1)
rather than
x = x * y + 1
6. Conditional Expressions
The conditional expression
expr1 ? expr2 : expr3
the expression expr1 is evaluated first. If it is non-zero (true), then the expression expr2 is evaluated, and that is the value of the conditional expression. Otherwise expr3 is evaluated, and that is the value.
7. Precedence and Order of Evaluation
Below table summarizes the rules for precedence and associativity of all operators, including those that we have not yet discussed. Operators on the same line have the same precedence; rows are in order of decreasing precedence, so, for example, *, /, and % all have the same precedence, which is higher than that of binary + and -.
Không có nhận xét nào:
Đăng nhận xét