Download - Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)
Lecture 3: Getting Started & Input / Output (I/O)
“TRON” Copyright 1982 (Walt Disney Productions)
Overview
Getting Started Semi-colons, curly-braces, whitespace Comments #include's Main program
Input / Output A little more detail on the compiling process
Nearly every line of C/C++ code ends with a semi-colon Frustrating at first, second-nature later...
White-space is generally ignored “Containing” is indicated by pairs of curly-braces
No requirements they appear on different lines
int i=5;// Note – this line has a blank line
(whitespace)i++;
while (i < 10) // Note: no semi-colon here{
cout << i << endl;}
int i=5;// Note – this line has a blank line
(whitespace)i++;
while (i < 10) // Note: no semi-colon here{
cout << i << endl;}
A way to add descriptive text to a source file More readable
To you To other people that have to look at it
Documentation What a section of code does, and how
Doesn't affect the size of the executable (although the size of the source files is increased)
Comments, cont. Syntax:
Complete single-line End-of-line Multi-line (or in-line)
Bold black text is actual C/C++ code
// This is a single line comment
x = 5; // This is an end-of-line comment
/* This is a multi-lineComment */
// The 10 below is an in-line commentfor (int i=0; i < /*10*/ 5; i++){
}
// This is a single line comment
x = 5; // This is an end-of-line comment
/* This is a multi-lineComment */
// The 10 below is an in-line commentfor (int i=0; i < /*10*/ 5; i++){
}
A technique to include a collection (library) of pre-defined symbols Functions Classes Constants …
Just a (text) file with a collection of C/C++ code [Look in $CodeBlocks\MinGW\lib\gcc\mingw32\4.7.1\
include\c++\iostream] For now, just “built-in” collections
Later: our own collections, downloaded libraries Later: more precise definition of what they really are...
#include, cont.
Syntax:
Put at the top of a .c or .cpp file. #include is part of the Pre-processor sub-language
of C/C++ [Note the # at the beginning]
#include <iostream> // A standard include file#include “my_file.h” // A “local” include file (later)
#include <iostream> // A standard include file#include “my_file.h” // A “local” include file (later)
#include, cont.
How it works: #include textually copy/pastes the content of the
referenced file in-place of the #include directive This happens before the compiler even starts.
Example: [Local include file, with a const variable – show
what compiler sees]
Where the program starts In a .c or .cpp file Can only appear once in a project
For console app's, 4 types: void return type (C++ only) int return type (C/C++) With command-line arguments without command-line arguments
Windows, OSX, etc. app's might have a different form...
Main Program, cont.void main() // C++ only{
/* Program code goes here... */}
void main() // C++ only{
/* Program code goes here... */}
int main(){
/* Program code goes here... */return 0; // Indicates everything ran OK
// Return non-zero to indicate errors.}
int main(){
/* Program code goes here... */return 0; // Indicates everything ran OK
// Return non-zero to indicate errors.}
void main(int argc, char ** argv) // C++ only{
/* Program code goes here... */}
void main(int argc, char ** argv) // C++ only{
/* Program code goes here... */}
int main(int argc, char ** argv){
/* Program code goes here... */return 0;
}
int main(int argc, char ** argv){
/* Program code goes here... */return 0;
}
A way to input or output data from our program Input
Keyboard [Mouse another day, hopefully]
Output Command-line output (screen)
(As will become the norm) we'll look at this in both C and C++ Most of the time they'll be the same...
Output to command-line (C++)
Uses the <iostream> header (collection) Uses namespaces (we'll look at these later...) For now, just put “using namespace std;” below the
#include's Uses the pre-defined cout object
By default, the screen (can be “remapped”) A stream
Write data elements to it using the chevron operator << Conversions are applied as necessary (automatically)
Output to command-line (C++), cont.
#include <iostream>using namespace std;
void main(){
cout << “Hello, World!”;cout << “A (fake) second line.”;cout << endl << “The real second line!” << endl;cout << “An integer: “ << 5 <<
“, A double: “ << 3.14159 << “, A string: “ << “'Hi!'” <<
endl;}
#include <iostream>using namespace std;
void main(){
cout << “Hello, World!”;cout << “A (fake) second line.”;cout << endl << “The real second line!” << endl;cout << “An integer: “ << 5 <<
“, A double: “ << 3.14159 << “, A string: “ << “'Hi!'” <<
endl;}
Escape sequences
A way to put a “hard-to-type” character in a string Start with a backslash and then a 1-character code
indicating which escape sequence to use.
\n // New-line
\t // Tab
\\ // An actual back-slash
\” // A quote
cout << “****\n@\t@\n****” << endl;cout << “****\n@\t@\n****” << endl;
Output:****@ @****
Output to command-line (C) Also defaults to the screen. You need to include...
<cstdio> if compiling with a C++ compiler <stdio.h> if compiling with either a C or a C++
compiler Done with the printf function. Uses a “format string” followed by 0 or more values
%d // An integer
%f // A float or double
%s // A string
// There are more – these are just the basics
Output to command line (C), cont.
#include <stdio.h>
int main(){
printf(“Hello, World!”);printf(“A (fake) second line.”);printf(“\nThe real second line!\n”);printf(“An integer: %d, A double: %f, A string: %s\n”,
5, 3.14159, “'Hi!'”);
return 0;}
#include <stdio.h>
int main(){
printf(“Hello, World!”);printf(“A (fake) second line.”);printf(“\nThe real second line!\n”);printf(“An integer: %d, A double: %f, A string: %s\n”,
5, 3.14159, “'Hi!'”);
return 0;}
Input from the command line (C++)
By default, reads keyboard input Also included by <iostream>
Don't forget the “using namespace std;” line Uses the pre-made cin object In the example, I'll use variables
We'll discuss these in detail in the next section Also uses the chevron operator, but this time they
go the other way (>>) I think of this as pointing from cin to the variables
Input from the command-line (C++), cont.#include <iostream>#include <string>using namespace std;
int main(){int myInt;float myFloat;string myString;
cout << "Enter an integer, then press enter: "; cin >> myInt; cout << "Enter a float and a string separated by a "; cout << "space, then press enter: "; cin >> myFloat >> myString;
cout << "Your int was: " << myInt << endl; cout << "Your float was: " << myFloat << endl; cout << "Your string was: '" << myString << "'" << endl;
return 0;}
#include <iostream>#include <string>using namespace std;
int main(){int myInt;float myFloat;string myString;
cout << "Enter an integer, then press enter: "; cin >> myInt; cout << "Enter a float and a string separated by a "; cout << "space, then press enter: "; cin >> myFloat >> myString;
cout << "Your int was: " << myInt << endl; cout << "Your float was: " << myFloat << endl; cout << "Your string was: '" << myString << "'" << endl;
return 0;}
Input from the command-line (C) Also defaults to the keyboard. You need to include...(just like printf)
<cstdio> if compiling with a C++ compiler <stdio.h> if compiling with either a C or a C++
compiler Done with the scanf function. Uses a “format string” like printf.
However, you pass the address of variables in which you wish to store the typed value.
Input from command-line (C), cont.#include <cstdio>
int main(){int myInt;float myFloat;char myString[256];
printf("Enter an integer, then press enter: "); scanf("%d", &myInt); printf("Enter a float and a string separated by a "); printf("space, then press enter: "); scanf("%f %s", &myFloat, myString);
printf("Your int was: %d\n", myInt); printf("Your float was: %f\n", myFloat); printf("Your string was: '%s'\n", myString);
return 0;}
#include <cstdio>
int main(){int myInt;float myFloat;char myString[256];
printf("Enter an integer, then press enter: "); scanf("%d", &myInt); printf("Enter a float and a string separated by a "); printf("space, then press enter: "); scanf("%f %s", &myFloat, myString);
printf("Your int was: %d\n", myInt); printf("Your float was: %f\n", myFloat); printf("Your string was: '%s'\n", myString);
return 0;}
Input / Output to (Text and Binary) Files
Very similar syntax to command-line input/output We'll look at this shortly
After we've looked at variables