Introduction to C/C++ Joseph Spring School of Computer Science Operating Systems and Computer Networks Lecture Introduction to C/C++ 1
Areas for Discussioni Introduction Background Books C++ after Java Linux, xterm and emacs Process Lecture 1 Introduction to C/C++ 2
Areas for Discussion Basic Components and Layout Simplest program cout << and cin << Layout Preprocessor Instructions and functions Program and Function Structure Data types Declarations Assignments Statements t t Further Preprocessor Instructions, if... else and count Characters Lecture 1 Introduction to C/C++ 3
Introduction Lecture 1 Introduction to C/C++ 4
Introduction - Background Last week we looked at using Linux and emacs to create C++ programs. We were interested at that t stage to be able to achieve the following: Getting into Linux, opening the xterm window to execute our programs in Opening up the text editor emacs to enter our source code Lecture 1 Introduction to C/C++ 5
Introduction - Background This means we should now be in a position o to: Create directories Move from one directory to another List the contents of a directory Open files for editing Copy and rename files Compile, link and run our programs using statements of the form: g++ datarate1.cpp./a.out Lecture 1 Introduction to C/C++ 6
Introduction - Background From an editing perspective we should now be in a position to: Move left and right by one character Move up and down by one line Move to the beginning or end of a file Move to the beginning gor end of a line Insert or delete lines and or characters Move lines or characters Save files with the same or different names Lecture 1 Introduction to C/C++ 7
Introduction - Process Programs written in a medium level language ge such as C++ have to be transformed into machine language so that the computer can process the instructions. C++ compilers use a two stage process to achieve this: The compiler translates the program into machine language instructions. i These are stored to memory as an object file The second stage, referred to as linking combines the object file with other object files to produce an executable file With g++ both of these stages are achieved as a single step Once the file is in executable form it can be run and checked to see if it performs as intended Lecture 1 Introduction to C/C++ 8
Basic Components and Layout Lecture 1 Introduction to C/C++ 9
Basic Components and Layout Example 1 The Simplest Possible Program #include<iostream> using namespace std; main( ) { 1. The program begin with preprocessor instructons. These take the form #include<iostream> using namespace std; Lecture 1 Introduction to C/C++ 10
Preprocessor Instructions Preprocessor instructions are: Grouped together at beginning of program Begin with a # character Control the use of libraries of predefined functions and include definitions of symbols used within the program Example 1. #include<iostream> iost eam this file econtains declarations about the standard Input and Output library Lecture 1 Introduction to C/C++ 11
Basic Components and Layout #include<iostream> causes the file iostream to be included in the source file before being compiled by the compiler It is used as a convenient way for including lots of standard d declarations and definitions iti iostream contains declarations about the standard input/output library such as: cout, cin,... Lecture 1 Introduction to C/C++ 12
Preprocessor Instructions 2. using namespace std; the elements of the standard C++ library are declared within the namespace with the name std. to access its functionality we declare with this expression that we will be using these entities. The line using namespace std; is frequently employed in C++ programs using the standard library Lecture 1 Introduction to C/C++ 13
Basic Components and Layout main( ) { 1. The main declaration is an unusual function 1. It is called by operating system not from C++ 2. It can return an int value to the operating system such that 1. 0 denotes normal termination 2. Other values denote abnormal termination 2. Arguments may be included or ignored as required Lecture 1 Introduction to C/C++ 14
Basic Components and Layout Example 1 The Simplest Possible Program #include<iostream> using namespace std; main( ) { 1. All C/C++ programs must have reference to the function main 2. When C/C++ programs run they look for and adhere to the definition of main Lecture 1 Introduction to C/C++ 15
Basic Components and Layout Example 2 The Next Simplest Possible Program #include<iostream> using namespace std; int main( ) { cout << Hello World << \n ; Lecture 1 Introduction to C/C++ 16
Basic Components and Layout int main( ) { cout << Hello World << \n ; 1. The cout << call is used to print whatever follows to the terminal screen 2. The above cout << call prints Hello World folllowed by a new line 3. \n newline, \t is a tab, \b backspace, \\ a backslash Lecture 1 Introduction to C/C++ 17
Program and Function Structure Lecture 1 Introduction to C/C++ 18
Program and Function Structure 1 /* comments these can occur throughout */ Preprocessor Instructions /* main program */ main( ) { statement 1; statement 2;... statement n; /* function definitions */ function1( () {... functionm( ) {... Lecture 1 Introduction to C/C++ 19
Program and Function Structure 1 General form: /* comments these can occur throughout */ Preprocessor Instructions /* main program */ main( () { statement 1; statement 2;... statement n; Lecture 1 Introduction to C/C++ 20
Program and Function Structure 1 main() {... /* function definitions */ function1( ) { statement 1;... statement n; functionm( () {... Lecture 1 Introduction to C/C++ 21
Program and Function Structure 1 /* comments these can occur throughout */ Preprocessor Instructions /* main program */ main( ) { statement 1; statement 2;... statement n; /* function definitions */ function1( () {... functionm( ) {... Lecture 1 Introduction to C/C++ 22
Program Structure 1. Comments which can occur throughout 2. Preprocessor instructions 3. The main program 4. Function definitions A C/C++ program must contain: 1. The function main( ) 2. Program statements contained between the curly brackets { and Lecture 1 Introduction to C/C++ 23
Data Types, Declarations, Assignments and Statements Lecture 1 Introduction to C/C++ 24
Programs pot.cpp #include<iostream.h> using namespace std; int main( ) { int pot = 1; while(pot < 10000) { cout << pot << \n ; pot = pot * 2; Lecture 1 Introduction to C/C++ 25
Data Types The integer family int machine dependent, but normally 32 bits long 32 bit short 16 bit char 8 bit (Note, char is a very short int) The unsigned family these are obtained by preceding the int family with unsigned They denote positive values Lecture 1 Introduction to C/C++ 26
Data Types The floating point type double is usually 64 bits float is usually 32 bit The boolean or logical type bool yields true or false Example while(pot < 10000) The expression pot < 10000 is either true or fl false; boolean. Lecture 1 Introduction to C/C++ 27
Declarations Declarations involve a type (such as int) followed by variable(s) Example 1: int pot = 1; Example 2: double rate=0.0, time=0.0, mb=0.0; Note the use of commas in the second example Lecture 1 Introduction to C/C++ 28
Statements The while statement is one that you will have met in the tutorial. It s form is: while (expression) statement Theexpression is evaluated if true then the statement t t that t follows is executed If false then the while statement stops and statements following the while statement are executed In the Powers of Two program pot.cpp the expression is pot < 10000 Lecture 1 Introduction to C/C++ 29
Assignments C++ just uses = for the assignment operator In the pot.cpp program we meet: pot = pot * 2 In this line two times the pot variable is assigned to the pot variable In the datarate1.cpp program the mb variable is assigned the product of the rate and time variables Note the use of the assignment operator in fibon.cpp Lecture 1 Introduction to C/C++ 30
Programs datarate1.cpp #include<iostream.h> using namespace std; int main( ) { double rate =0.0, time=0.0, mb=0.0; while(true) { cout << input rate in Mb/sec: ; cin >> rate; cout << input time in seconds (1 milli=0.001): ; cin >> time; mb = rate * time; cout << data transferred = << mb << Mb\n ; cout << data transferred = << mb/8 << MB\n ; Lecture 1 Introduction to C/C++ 31
Programs datarate2.cpp #include<iostream.h> using namespace std; int main( ) { double rate =0.0, 0 time=0.0, 0 mb=0.0; 0; while(true) { cout << input rate in Mb/sec: ; cin >> rate; cout << input amount of data in MB (megabytes): ; cin >> mb; time = (mb*8) / rate; cout << time for transfer = << time << secs\n ; Lecture 1 Introduction to C/C++ 32
Programs fibon.cpp #include<iostream.h> using namespace std; int main( ) { int last, last_ but_ one, new_ last; last_but_one = 0; last = 1; while(last< 10000) { cout << last << ; new_last =last+last last_but_one; last_but_one = last; last = new_last; cout << \n ; Lecture 1 Introduction to C/C++ 33
Further Preprocessor Instructions, The if... else... Conditional Statement, and count Lecture 1 Introduction to C/C++ 34
Programs largest.cpp #include <iostream> #include <limits.h> using namespace std; int main( (){ int num, count=0, max_so_far=int_min; while ( cin >> num ) { if ( num > max_so_far ) max_so_far = num; count = count + 1; if ( count > 0 ) cout << "largest = " << max_so_far _ << "\n"; else cout << "no numbers input\n"; Lecture 1 Introduction to C/C++ 35
Further Preprocessor Instructions The program largest.cpp introduces a new preprocessor instruction: #include <limits.h> limits.h contains definitions of a range of integer constants, for example, the largest and smallest int, unsigned int, float, double,... The program largest.cpp uses the symbolic constant INT_MIN the smallest int value defined in limits.h The definitions are dependent upon the implementation employed limits.hit is from Chence the use of.h in the name Lecture 1 Introduction to C/C++ 36
The if... else... Conditional Statement The program largest.cpp introduces the following conditional statement: if ( count > 0 ) cout << "largest = " << max_so_far << "\n"; else cout << "no numbers input\n"; The general form for conditional statements is: if (expression) statement 1 else statement 2 Here the expression is evaluated. If it is true the statement 1 is executed. If it is false then statement 2 is executed Lecture 1 Introduction to C/C++ 37
The count variable The program largest.cpp also introduces the count variable count=0;... count = count t+ 1; if ( count > 0 ) cout << "largest = " << max_so_far << "\n"; " else cout << "no numbers input\n ; The count variable is useful in the above context and also when printing the first n values in a sequence For example, the first 18 Fibonacci numbers Lecture 1 Introduction to C/C++ 38
Characters Lecture 1 Introduction to C/C++ 39
Programs copy1.cppcpp #include <iostream> using namespace std; int main() { char c; while ( cin.get(c) ) cout.put(c); Lecture 1 Introduction to C/C++ 40
copy1.cppcpp This program copies the standard input to the standard output The statement cin.get(c) reads in characters from standard input into the variable c The statement t t while ( cin.get(c) ())) cout.put(c); (); returns the value true if a character is stored in the variable c and executes cout.put(c) writing the character to the standard output Lecture 1 Introduction to C/C++ 41
copy2.cppcpp This program also copies the standard input to the standard output, however it omits spaces, newlines and tabs for example Output from the program copy2.cpp is given on the Running copy2.cpp cpp slide Lecture 1 Introduction to C/C++ 42
Programs copy2.cppcpp #include <iostream> using namespace std; int main() { char c; while ( cin >> c ) cout << c; Lecture 1 Introduction to C/C++ 43
Running copy2.cppcpp rabbit(273)$ g++ copy2.cpp pp rabbit(274)$./a.out < copy2.cpp #include<iostream>intmain(){charc;while(cin>>c)cout<<c; Lecture 1 Introduction to C/C++ 44
Programs wrap.cpp #include <iostream> const int LINELENGTH = 30; using namespace std; int main() { int pos = 0; char c; while ( cin.get(c) ) { if (c == \n ) pos = 0; else pos = pos + 1; if (pos > LINELENGTH) { cout << \n ; pos = 1; cout << c; Lecture 1 Introduction to C/C++ 45
wrap.cpp wrapping text and equality The program wrap.cpp ensures reads standard input and prints the text tto the standard doutput t(terminal) ensuring that each line of output is no longer than 30 characters If the linelength is greater than 30 then the newline character \n is inserted and the text continues on the next line Note the test for equality cin == \n uses the double equal sign. Recall a single equals sign is used for assignment or within an equation. The corresponding sign for not equal to would be!= Lecture 1 Introduction to C/C++ 46
wrap.cpp character constants Character constants are given in... Examples \n linefeed \b backspace \o null \t tab \r carriage return Lecture 1 Introduction to C/C++ 47
Summary Introduction Background Books C++ after Java Linux, xterm and emacs Process Basic Components and Layout Simplest program cout << and cin << Layout Preprocessor Instructions and functions Program and Function Structure Data types Declarations Assignments Statements Further Preprocessor Instructions, if... else and count Characters Lecture 1 Introduction to C/C++ 48