Professional Documents
Culture Documents
Characters are the basic building blocks in C program, equivalent to letters in English language Includes every printable character on the standard English language keyboard except `, $ and @
Example of characters:
Numeric digits: 0 - 9 Lowercase/uppercase letters: a - z and A - Z Space (blank) Special characters: , . ; ? / ( ) [ ] { } * & % ^ < > etc
A token is a language element that can be used in forming higher level language constructs Equivalent to a word in English language
Reserved Words
Keywords that identify language entities such as
(etc..)
Displayed in BLUE color in MS Visual C++
Identifiers
Words used to represent certain program entities (program
Constants
Entities that appear in the program code as fixed values. 4 types of constants: Integer constants Floating-point constants Character constants String Literal
Integer Constant
Positive or negative whole numbers with no fractional part Optional + or sign before the digit. It can be decimal (base 10), octal (base 8) or hexadecimal (base 16) Hexadecimal is very useful when dealing with binary numbers Example: const int MAX_NUM = 10;
Integer Constant
Rules for Decimal Integer Constants
1. Decimal integer constants must begin with a nonzero decimal digit, the only exception being 0, and can contain decimal digital values of 0 through 9. An integer that begins with 0 is considered an octal constant 2. If the sign is missing in an integer constant, the computer assumes a positive value. 3. Commas are not allowed in integer constants. Therefore, 1,500 is illegal; it should be 1500. Example of legal integer constants are 15, 0, +250 and 7550 0179 is illegal since the first digit is zero 1F8 is illegal since it contains letter F 1,700 is illegal since it contains comma
Character constants
A character enclosed in a single quotation mark Example:
const char letter = n; const char number = 1; printf(%c, S); Output would be: S
How to write a single quotation mark? is ambiguous, so escape character back slash \ \
String Literals
A sequence of any number of characters surrounded by double quotation marks. Example:
Human Revolution
How to write special double quotation mark? is ambiguous, so use escape character
Example: printf(He shouted, /Run!/); output: He shouted, Run! - The escape character along with any character that follow it is called Escape Sequence
String Literals
Escape Sequence \a \b \f \n \r \t \v \\ \ \ \? Alert Backspace Formfeed New line Carriage return Horizontal tab Vertical tab Back slash Single quotation Double quotation Question mark Name Meaning Sounds a beep Backs up 1 character Starts a new screen of page Moves to beginning of next line Moves to beginning of current line Moves to next tab position Moves down a fixed amount Prints a back slash Prints a single quotation Prints a double quotation Prints a question mark
Punctuators (separators)
Symbols used to separate different parts of the C program. These punctuators include:
[ ] ( ) { } , ; : * #
Usage example:
void main (void) { int num = 10; printf (%i, num); }
Operators
Tokens that result in some kind of computation or action when applied to variables or or other elements in an expression. Example of operators:
*+=-/
Usage example:
result = total1 + total2;
Operators
Bit wise operators are important when dealing with low level processing (image processing etc)
Complement ( ~ ) Reverse every bit in the data. char num = 0xF5; //1111 0101 printf(%X, ~num); //output is 0x0A - left shift ( << ) Shift to left and replace the blank space with ZERO char num = 0xF5; //1111 0101 printf(%X,num<<2); //output is 0xD4 Equivalent to multiply the operand with 2
Operators
- right shift ( >> ) Shift to right and replace the blank space with ZERO char num = 0xF5; //1111 0101 printf(%X,num>>2); //output is 0x3D Equivalent to divide the operand by 2
- Bit wise AND ( & ) AND every bit of first operand with the second one. char num = 0xF5, mask = 0x0F; //1111 0101 0000 1111 printf(%X,num&mask); //output is 0x05 Useful for masking to CLEAR (set to zero) specific bits
Operators
- Bit wise OR ( | ) OR every bit of first operand with the second one. char num = 0xF5, mask = 0x0F; //1111 0101 0000 1111 printf(%X,num|mask); //output is 0xFF Useful for masking to SET (set to 1) specific bits
End...