Title: Standard InputOutput, Variables and Storage
1Standard Input/Output, Variables and Storage
2Agenda
- Basic Program Structure
- Variable, Data Types and Identifiers
- Fundamental Data Types
- Strings
- Variable Scope and Initialization
- Standard Input/Output
- Constants
3 Basic Program Structure
4Basic Program Structure
- The basic elements of a program are
- Header
- Variable declaration
- Function
- Comment
5Basic Program Structure
- /
- Heading comments
- /
- data declarations
- int main()
-
- executable statements
- return 0
6Basic Program Structure
- include ltiostreamgt
- using namespace std
- int main()
- int feet, inches
- feet 6
- inches feet 12
- cout ltlt "Height is " ltlt inches ltlt " in."
- return 0
7Basic Program Structure
- include ltiostreamgt
- int main()
- int feet, inches
- feet 6
- inches feet 12
- stdcout ltlt "Height is " ltlt inches ltlt " in."
- return 0
8Basic Program Structure
- include statement is a compiler directive that
tells the compiler to make a specified file
available to a program. - using namespace std informs the compiler that
the files to be used are in a special region
known as namespace std. - int main() is a main control center for program
execution. - int feet, inches is a variable declaration.
- return 0 statement returns control of the
program back to the computer's operating system.
9Variable, Data Types and Identifiers
10Variables Data Types
- Variable is a portion of memory used to store a
determined value. - C allows you to store values in variables.
- Each variable is identified by a variable name.
- Each variable has a data type.
- A data type defines a set of values and the
allowable operations on those values. - The semicolon () is used to indicate the end of
statement.
11Variables
- Each variable needs an identifier that
distinguishes it from the others, in the below
example, the variable identifiers were a, b and
result. - a 5
- b 2
- a a 1
- result a - b
12Identifiers
- A valid identifier is a sequence of one or more
letters, digits or underscore characters (_). - begin with a letter.
- begin with an underline character (_ ), but
should be avoided. - cannot contain spaces, punctuation marks,
symbols. - cannot begin with a digit.
- cannot match any keyword of the C language nor
your compiler's specific ones, which are reserved
keywords like for, while, cin, cout
13Identifiers
- The C language is a "case sensitive" language.
- This means that an identifier written in capital
letters is not equivalent to another one with the
same name but written in small letters. - Thus, for example, the RESULT variable is not the
same as the result variable or the Result
variable. These are three different variable
identifiers.
14 Fundamental Data Types
15Fundamental Data Types
- Type Bits Range
- char 8 signed -128 to 127
- unsigned 0 to 255
- short int (short) 16 signed -32768 to 32767
- unsigned 0 to 65535
- int 32 signed -2147483648 to 2147483647
- unsigned 0 to 4294967295
- long int (long) 32 signed -2147483648 to
2147483647 - unsigned 0 to 4294967295
- bool 8 true or false
- float 32 exponent -32 to 38,
- precision 7 digits
- double 64 exponent -308 to 308,
- precision 15 digits
- long double 80 exponent -4932 to 4932,
- precision 15 digits
- wchar_t 1632 1 wide character
16Variable Declarations
- Before you can use a variable in C, it must be
defined in a declaration statement. - A variable declaration serves three purposes
- It defines the name of the variable.
- It defines the type of the variable (integer,
real number, character, etc.). - It gives the programmer a description of the
variable.
17Declaration of Variables
- int a
- float mynumber
- These are two valid declarations of variables.
- The first one declares a variable of type int
with the identifier a. - The second one declares a variable of type float
with the identifier mynumber. - Once declared, the variables a and mynumber can
be used within the rest of their scope in the
program.
18Declaration of Variables
- You can declare many variables in a single
statement by separating their identifiers with
commas. - int a, b, c
- This declares three variables (a, b and c), all
of them of type int, and has exactly the same
meaning as - int a
- int b
- int c
19Declaration of Variables
- The integer data types char, short, long and int
can be either signed or unsigned depending on the
range of numbers needed to be represented. - Signed types can represent both positive and
negative values. - Unsigned types can only represent positive values
(and zero). - unsigned short int NumberOfSisters
- signed int MyAccountBalance
20Declaration of Variables
- By default, if we do not specify either signed or
unsigned most compiler settings will assume the
type to be signed. - signed int MyAccountBalance
- int MyAccountBalance
- have exactly the same meaning.
21Declaration of Variables
- include ltiostreamgt
- using namespace std
- int main ()
- // declaring variables
- int a, b
- int result
- // process
- a 5
- b 2
- a a 1
- result a - b
- // print out the result
- cout ltlt result
- // terminate the program
- return 0
22Integer Declarations
- Integer number (also known as whole number) has
no fractional part or decimal point. - Numbers such as 1, 87, and -222 are integers.
- The number 8.3 is not an integer because it
contains a decimal point.
23Assignment Statements
- Variables are given a value through the use of
assignment statements. int answer answer (1
2) 4
24Floating-Point Declarations
- Floating point number (also called real numbers)
contain a decimal point - For example, 5.5, 8.3, -12.6.
- It is possible to omit the leading or trailing
zero in floating point numbers. - For example, 5. is the same as 5.0 and .2 is the
same as 0.2. However adding the extra zero makes
obvious that you are using a floating point
number. - Exponent notation for floating point numbers
e-exp. - For example, 1.2e34, is shorthand for 1.21034.
25Floating-Point Declarations
- include ltiostreamgt
- using namespace std
- float result // store the result
- int main()
- result 1.0 / 3.0
- cout ltlt "One-third is " ltlt result ltlt '\n'
- return 0
26Floating-Point Declarations
- The float type comes in various flavors.
- float denotes normal precision (usually 4 bytes)
- double indicates double precision (usually 8
bytes) - On most machines, single-precision floating-point
instructions execute faster (but less accurate)
than double precision. - Double precision gains more accuracy at the
expense of time and storage. - In most cases float is adequate however, if
accuracy is a problem, switch to double.
27Floating-Point Declarations
- Why is this program result "0" ? What must be
done to fix it? - include ltiostreamgt
- using namespace std
- float answer
- int main()
- answer 1/3
- cout ltlt "1/3 is " ltlt answer ltlt "\n"
- return 0
28Floating-Point Declarations
- What are the results of each expression?
- include ltiostreamgt
- int integer
- float floating
- int main()
- floating 1.0/2.0
- integer 1/3
- floating (1/2)(1/2)
- floating 3.0/2.0
- integer floating
- return 0
29Floating-Point versus Integer
- Integer division truncates, 19/10 is 1
- Floating point division does not, 19.0/10.0 is
1.9 - A division is floating point if either operand is
a floating point number.
30Character Declarations
- Character variable is enclosed in single quotes
('). Examples 'A', 'a', '!', and '1'. - The backslash (\) is used to indicate special
characters. - '\n' is the newline character.(Advances the
output to the next line.)
31Character Declarations
include ltiostreamgt char char1,char2,char3 int
main() char1 'A' char2 'B' char3
'C' cout ltlt char1 ltlt char2 ltlt char3 ltlt
endl cout ltlt char3 ltlt char2 ltlt char1 ltlt
endl return 0
32Boolean Declarations
- Boolean variable has one of two values true,
false - bool flag
- flag true
- Note The bool type is relatively new to C and
some legacy macros exist to implement a bool
type. - These macros use BOOL or Bool as a data type and
TRUE and FALSE as the values. (These legacy types
should be avoided.)
33 Strings
34String Declarations
- Variables that can store non-numerical values
that are longer than one single character are
known as strings. - The C language library provides support for
strings through the standard string class. - This is not a fundamental type, but it behaves in
a similar way as fundamental types do in its most
basic usage. - A first difference with fundamental data types is
that in order to declare and use objects
(variables) of this type we need to include an
additional header file in our source code - include ltstringgt
- and have access to the std namespace.
35String Declarations
- include ltiostreamgt
- include ltstringgt
- using namespace std
- int main ()
- string mystring "This is a string"
- cout ltlt mystring
- return 0
36String Declarations
- Strings can be initialized with any valid string
literal just like numerical type variables can be
initialized to any valid numerical literal. - string mystring "This is a string"
- string mystring ("This is a string")
37 Variable Scope Initialization
38Scope of Variables
- A variable can be either of global or local
scope. - A global variable is a variable declared in the
main body of the source code, outside all
functions. - A local variable is one declared within the body
of a function or a block. - Global variables can bereferred from anywhere in
the code, even inside functions, whenever it is
after its declaration.
39Initialization of Variables
- There are two ways to initialize the variable at
the same moment that it is declared. - The first one, known as c-like, is done by
appending an equal sign followed by the value to
which the variable will be initialized - type identifier initial_value
- int a 0
40Initialization of Variables
- The other way to initialize variables, known as
constructor initialization, is done by enclosing
the initial value between parentheses (()) - type identifier (initial_value)
- int a (0)
41Initialization of Variables
- include ltiostreamgt
- using namespace std
- int main ()
- int a5 // initial value 5
- int b(2) // initial value 2
- int result // initial value
undetermined - a a 3
- result a - b
- cout ltlt result
- return 0
-
42 Standard Input/Output
43Standard Output (cout)
- By default, the standard output of a program is
the screen, and the C stream object defined to
access it is cout. - cout is used in conjunction with the insertion
operator, which is written as ltlt (two "less than"
signs). - cout ltlt "Output" // prints Output on screen
- cout ltlt 120 // prints number 120 on screen
- cout ltlt x // prints the content of x on
screen
44Standard Output (cout)
- More uses of cout
- cout ltlt "Hello" // prints Hello
- cout ltlt Hello // prints the content of Hello
variable - cout ltlt "Hello, " ltlt "I am " ltlt "a C
statement" - cout ltlt "Hello, I am " ltlt age ltlt "."
- cout ltlt "First sentence.\n"
- cout ltlt "Second sentence.\nThird sentence."
- cout ltlt "First sentence." ltlt endl
- cout ltlt "Second sentence." ltlt endl
45Output in Table Form
- The setw manipulator is used to control
horizontal alignment of output (found in the
library iomanip) - cout ltlt "1234567890" ltlt endl
- cout ltlt setw(7) ltlt "46" ltlt endl
- cout ltlt setw(7) ltlt "Smith" ltlt endl
- cout ltlt "Jones" ltlt endl
- cout ltlt "AB" ltlt setw(7) ltlt "CDEF" ltlt endl
46Standard Input (cin)
- The standard input device is usually the
keyboard. - Handling the standard input in C is done by
applying the overloaded operator of extraction
(gtgt) on the cin stream. - int age, zipcode
- cin gtgt age
- cin gtgt age gtgt zipcode
47Standard Input (cin)
- include ltiostreamgt
- using namespace std
- int main ()
- int i
- cout ltlt "Please enter an integer value "
- cin gtgt i
- cout ltlt "The value you entered is " ltlt i
- cout ltlt " and its double is " ltlt i2 ltlt ".\n"
- return 0
48cin and strings
- We can use cin to get strings with the extraction
operator (gtgt) as we do with fundamental data type
variables - cin gtgt mystring
- However, as it has been said, cin extraction
stops reading as soon as if finds any blank space
character, so in this case we will be able to get
just one word for each extraction. - A method to store the whole sentence into a
string will be mentioned later.
49 Constants
50Constants
- A constant is a datum whose value cannot be
changed once it is initially bound to a value. - Literals are used to express particular values
within the source code of a program. - a 5
- The 5 in this piece of code was a literal
constant. - Literal constants can be divided in Integer
Numerals, Floating-Point Numerals, Characters,
Strings and Boolean Values.
51Constants
- Integer Numerals
- a 1776a 707a -273
- a 75 // decimal
- a 0113 // octal
- a 0x4b // hexadecimal
- a 75 // int
- a 75u // unsigned int
- a 75l // long
- a 75ul // unsigned long
52Constants
- Floating Point Numbers
- a 3.14159 // 3.14159
- a 6.02e23 // 6.02 x 1023
- a 1.6e-19 // 1.6 x 10-19
- a 3.0 // 3.0
- a 3.14159L // long double
- a 6.02e23f // float
53Constants
- Characters and Strings
- a 'z'
- a 'p'
- a "Hello world"
- a "How do you do?"
54Constants
- Special Characters
- \n newline
- \r carriage return
- \t tab
- \v vertical tab
- \b backspace
- \f form feed (page feed)
- \a alert (beep)
- \' single quote (')
- \" double quote (")
- \? question mark (?)
- \\ backslash (\)
55Constants
- Special Characters
- a '\n'
- a '\t'
- a "Left \t Right"
- a "one\ntwo\nthree"
- a "string expressed in \
- two lines"
- a "this forms" "a single" "string" "of
characters" - a L"This is a wide character string"
56Defined Constants
- You can define your own names for constants that
you use very often without having to resort to
memory-consuming variables, simply by using the
define preprocessor directive. - define identifier value
- define PI 3.14159265
- define NEWLINE '\n'
57Defined Constants
- include ltiostreamgt
- using namespace std
- define PI 3.14159
- define NEWLINE '\n'
- int main ()
- double r5.0 // radius
- double circle
- circle 2 PI r
- cout ltlt circle
- cout ltlt NEWLINE
- return 0
58Defined Constants
- define X 11
- int myvariable X 2
- // myvariable 3 because
- // int myvariable X 2
- // int myvariable 11 2
- define X (11)
- int myvariable X 2
- // myvariable 4 because
- // int myvariable X 2
- // int myvariable (11) 2
59Declared Constants
- With the const prefix you can declare constants
with a specific type in the same way as you would
do with a variable - const int pathwidth 100
- const char tabulator '\t'
- pathwidth and tabulator are two typed constants.
- They are treated just like regular variables
except that their values cannot be modified after
their definition.