Chapter 3 Getting Started with C LEARN BY

Chapter 3 Getting Started with C++: LEARN BY DOING Todd Breedlove Troy Scevers Randal L. Albert

3. 1 C++ Basics – Reserved Words • C++ is case sensitive • Average is different than average and AVERAGE • Reserved words • Identifiers that have special meaning and are a part of the language • All C++ reserved words are in lowercase

3. 1 C++ Basics – Statement Termination • Most statements terminated by a semicolon except: • Function headers • Preprocessor directives • Control statements • Preprocessor directives • Commands executed by the preprocessor • Preprocessor directives require the first character on the line to be a number sign (#)

3. 2 Whitespace – Definition • Whitespace • Empty or non-visible character (i. e. , space or tab), including blank lines • Aids readability • Readability allows a program to be more easily modified and maintained • Most whitespace is ignored by the compiler • Some whitespace is required by the translation process

3. 2 Whitespace - Example • Hard to read example int main(){return 0; } • Readable example int main ( ) { return 0; }

3. 3 Comments – Definition • Comments • Lines of C++ code that are ignored by the compiler • Used to document the source code for you or for other programmers • Aid in the readability and maintainability of source code

3. 3 Comments – Example • Two forms • // inline comment • An inline comment starts at the // and goes to end of line • /* block comment */ • A block comment can span multiple lines

3. 4 The main Function – Definition • The main function (commonly referred to as “main”) is the starting point of a C/C++ program • Every C/C++ program will have exactly one main • Function • A group of related statements that perform a specific task or job

3. 4 The main Function – Function Terminology • Function header • First line in a function definition • Function definition • Combination of the function header and a function body • Function body • The statements enclosed in curly braces int main() // Function header, no semicolon {// Start of function body return 0; // C++ statement, requires semicolon }// End of function body

3. 4 The main Function – Curly Braces • Curly braces and parentheses always come in pairs • Curly braces: • Group statements together • Define the beginning and ending of the function body (among other things)

3. 5 The #include Preprocessor Directive – Definition • Preprocessor directive #include allows access to predefined routines in external or separate files – called header files • Predefined routine • Not part of the core language but an extension to the language

3. 5 The #include Preprocessor Directive – Forms • Two forms of the #include: • #include <header_file> • Looks in the “include” directory specified by the compiler • Used with predefined header files • #include "header_file" • Looks for the header file in a user specified location • Used with user defined header files • Will be discussed further in Chapter 13

3. 5 The #include Preprocessor Directive – Example • The <iostream> header file contains predefined input and output routines #include <iostream> // Allows access to I/O routines int main() { // Outputs "Hello World!" to the screen std: : cout << "Hello World!"; } return 0;

3. 5. 1 Namespaces – Definition • namespace • Allows grouping or structuring related entities inside one category • cout is located in “std” namespace

3. 5. 1 Namespaces – Forms • Ways to access routines in namespaces: • Explicitly – requires use of the namespace prefix every time a routine is used std: : cout << "Hello World!"; • Using directive – allows access to all routines within a namespace using namespace std; • Using declaration – allows access to only those routines specified (preferred method) using std: : cout;

3. 7 C The Differences – Comments • C programmers are limited to the block style comment (/* */) with older compilers • Many new C compilers allow the use of the inline comment (//) which has become part of the C standard as of C 99

3. 7 C The Differences – Header Files • C uses different header files and routines for I/O • Namespaces are not required to access predefined functions #include <stdio. h> // Allows access to I/O routines int main( void ) { // Outputs "Hello World!" to the screen printf( "Hello World!" ); } return 0;
- Slides: 17