CS 161 Introduction to Programming and Problem Solving Chapter Hello World Program in C++ And Lexical Rules Herbert G. Mayer, PSU Status 10/18/2014 Syllabus Lexical Rules Identifiers Keywords Literals Scopes Numeric Limits, from <limits.h> Hello World in C and C++ Some Data For Loops in C++ Lexical Rules A C program is an ASCII text files ASCII: American Standard Code for Information Interchange This includes white space, such as blanks ‘ ‘, tabs, carriage return, line-feed, etc. Meaningful parts consist of letters, digits, and special symbols, such as ‘+’ or ‘/’ This sequence of (usually) visible characters. Named the source program, is logically partitioned into tokens A token is a complete lexeme in the C language, i.e. one single part of a C program Lexical Rules Sometimes the C rules for composing lexical elements, are sufficient to separate one from another token, e.g. min = 100; consists of 4 tokens: identifier min, special symbol =, decimal integer literal 100, and special symbol ; It could also be written correctly as min=100; Other times a deliberate separation of one token from another is needed, done via white space, e.g. a blank character A C token must fit onto a single line, with the exception of string literals, enclosed in a pair of “ Lexical Rules: Identifiers Identifiers, AKA user-defined identifiers in C are composed of letters, digits, and underscore characters ‘_’ Must begin with a letter or underscore Are case sensitive; i.e. the 3 legal C identifiers MaxValue, Maxvalue, maxvalue are all 3 distinct names Reserved keywords are symbols that adhere to the lexical rules of identifiers, but are reserved, with predefined meanings, and cannot be be used as identifiers Lexical Rules: Reserved Keywords All keywords reserved in the C programming language are also carried over into C++. There are 32 such inherited reserved words: auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while Lexical Rules: Reserved Keywords There are 30 additional C++ reserved keywords that are not part of C, hence new in C++. See below: asm bool catch class const_cast delete dynamic_cast explicit false friend inline mutable namespace new operator private public protected reinterpret_cast static_cast template this throw true try typeid typename using virtual wchar_t Lexical Rules: Literals There are octal, decimal and hexadecimal numeric literals, bool, char, and string literals in C++ Integer numeric literals of base 8, octal numbers, are introduced via the leading ‘0’ digit, followed by any number of octal digits ‘0’ .. ‘7’. Example: 0177, same as 12710; note that overflow can occur, if the resulting number is greater than the maximum integral number the machine can handle Integer numbers may have an optional negative sign, e.g. -077 Integer decimal literals consist of any number of decimal digits ‘0’ .. ‘0’, but may not have a leading ‘0’. Examples: 128, or -255 Lexical Rules: Literals Integer numeric literals of base 16, hexadecimal numbers, are introduced via the leading ‘0x’, followed by any number of hex digits ‘0’ .. ‘9’, and ‘a’..’f’, or ‘A’..’F’. Here ‘a’ stands for the digit of value 1010 , ‘and ‘f’ stands for the digit of value 1510. Upperand lower-case are equivalent. Example: 0xff, same 0xFF, same as 25510 The boolean literals are true and false Literals of type char are embedded in a pair of single quotes, like ‘z’ Some char type literals are not printable, thus they are encoded via an escape mechanism, introduced by the \ followed by the desired symbol. E.g. ‘\n’ stands for carriage return, and ‘\t’ for horizontal tab Lexical Rules: Floating-Point Literals Float (AKA floating-point) literals are numeric literals with a possible fraction. They can have integer and fractional parts, be signed, and be scaled; e.g.: 3.142592 +3.142592 -3.142592 0.3142592E1 31.42592E-1 // // // // // for PI for PI with redundant sign negative PI PI scaled by factor * 10 PI scaled by 10-1 Numeric Limits, from <limits.h> Lexical Rules: Special Symbols as Operators L 1 2 Description Function call ( ) Array subscript [ ] Struct member . Struct dereference expr++ Decrement (post) expr-- Associativity L 5 left-to-right 6 Description Operator Bitwise left shift << Bitwise right shift >> Less than < Greater than > LT or equal to <= GT or equal to >= Equal to == Not equal to != Associativity left-to-right left-to-right Indirection * Reference (addr) & Unary plus + 8 Bitwise AND & left-to-right Unary minus - 9 Bitwise XOR ^ left-to-right Logical negation ! 10 Bitwise OR | left-to-right 11 Logical AND && left-to-right Bitwise NOT ~ 7 right-to-left left-to-right Increment (pre) ++expr 12 Logical OR || left-to-right Decrement (pre) --expr 13 Conditional ? : right-to-left 14 Assignment = += -= *= /= %= >>= <<= &= ^= |= right-to-left 15 Comma , left-to-right Size in bytes 4 -> Increment (post) Cast 3 Operator ( type ) sizeof Multiplication * Division / Modulo % Addition + Subtraction - left-to-right left-to-right Highest precedence is Level 1. Lowest precedence is Level 15. Use parentheses () to alter the order of evaluation. 11 Hello World Program We’ll write and discuss a very simple source programs Tongue-in-cheek known as the hello world program We briefly contrast C and C++, then we cover more advanced C++ for loops This famous hello world program does nothing except to print “Hello” on the standard output device Hello World in C #include <stdio.h> void main( ) { // main printf( “Hello World!\n” ); // no return value given; should be 0 } // end main Hello World in C #include provides to the programmer a so-called library: stdio.h stdio.h makes available standard input and output functions, for example printf(), without programmers having to code them main() identifies a select C function, always to be provided in a complete C source program main() is always the first function to be executed All work to be done by the main() function is enclosed between the { and } braces, as has to be done for every other C function Hello World in C #include directive to include libraries <stdio.h> specifies library to be included void reserved keyword, specifies type of function main reserved keyword, naming a special function ( ) are special symbols, enclosing formal parameters { } special symbols, enclosing body of function // is start of line comment printf user-defined function name from stdio.h “Hello World!\n” string literal to be printed ; special symbol, ending statement Hello World in C++ #include <iostream.h> int main( void ) { // main cout << ”Hello World!” << endl; // endl is \n return 0; } // end main Hello World in C++ C++ also provides the same common input- and output-functions of C, but typical C++ IO uses a new IO format, available in iostream.h main() again identifies the starting point of execution Like in C, all work to be done by the C++ main() function is enclosed between the { and } brackets Instead of a magic symbol ‘\n’ to identify a “carriagereturn and line-feed” to be output, C++ specifies a symbolic, fictitious character, named: endl The << operators define elements to be output If there are no errors, C++ returns error code 0 to say: All is OK! Some Data #include <stdio.h> #include <iostream.h> int main( void ) { // main int i; // we don’t use i!!! int year = 2014; // initialized year float pi = 3.141592; // should be constant printf( ”year = %d\n”, year ); // old C cout << “year = “ << year << endl; // C++ return 0; } // end main Some Data The reserved C keyword int defines a datum, by the name i, and another by the name year The former is left uninitialized, the latter receives an initial value, 2014 Both options are allowed in C Line “float pi” defines a datum of a different type, called a floating-point value. This type permits fractional values, but not all possible values in the numeric range are representable on a computer The printf() argument enclosed in a pair of “ is printed, unless it is a % character, in which data and format interpretation takes place Some Data Interpretation of data to be printed depends on the characters after the % Here the %d means: Find another argument after the “ pair, and print it as an integer value, to base 10; i.e. a decimal integer number The special character sequence \n means: also print a carriage-return line-feed, as we saw with the hello world Quick Excursion to C++ Now you now know something about arrays and while loops Arrays are composite types, AKA aggregate types Loops perform repetitive actions, so are perfectly suited to process arrays We also refer to them as iterations The for loop is the next construct you learn For Loop in C++ Formally, the C++ For Loop defines 3 expressions More practically, the C++ For Loop defines an iteration variable, which progresses over a range of values This is called the iteration space The first expression defines an iteration variable, initialized to a defined start value of scalar type; may be integer or any other scalar type The second expression tests a boolean condition; if true, the progression continues; practically the iteration variable is checked, whether it has reached a final value The third expression defines a change to the iteration variable, executed at the end of the loop body For Loop in C++ The 3 expressions are enclosed in a pair of ( and ) parens, each separated from the next by a ; Then comes a statement, which is executed iteratively, based on the iteration space It is common to refer to the iteration variable inside the body of the for loop Here an example: #define MAX 10 . . . for ( int i = 0; i < MAX; i++ ) { list[ i ] = 0; // best not change i } //end for For Loop in C++ A for loop executes, as long as the boolean expression 2 yields true When the expression is false, the for loop ends, and the operation after the for is executed next Note that the boolean condition could be false at the start, in which case the for loop would be empty, i.e. executed 0 times Simple For Loop in C++ // declaration of some array #define TEN 10 char digits[ TEN ]; // loop to initialize digits[] for( int i = 0; i < TEN; i++ ) {) { digits[ i ] = ‘0’ + i++; } //end for // no compound { and } needed, but is a good habit More Complex For Loop in C++ // declaration of 2-Dim matrix mat[][] #define MAX_ROW 50 #define MAX_COL 50 float mat[ MAX_ROW ][ MAX_COL }; // initialize matrix mat[][] for( int row = 0; row < MAX_ROW; row++ ) { for( int col = 0; col < MAX_COL; col++ ) { mat[ row ][ col ] = 0.0; } //end for } //end for // no compound needed, but is a good habit