C++ Variables and Data Types Review - Prof. L. G. Volkert, Study notes of Computer Science

A comprehensive review of basic notions in c++ programming, focusing on variables, data types, and related concepts. Topics covered include what a variable is, variable types (int, double, char, bool), variable declaration, assignment, streams, and input/output operators. The document also discusses type compatibility, expressions, and unary operations.

Typology: Study notes

Pre 2010

Uploaded on 02/25/2010

koofers-user-y72-1
koofers-user-y72-1 🇺🇸

9 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Basic notions review
what is a variable? va lue? address? memory location?
what is an identifier? variable name? keyword?
what is legal identifier ? what identifiers are legal?
what are C-style? Pas cal-style identifiers?
what is variable type? what types have we studied?
what is variable decla ration? where (in the program) a v ariable
declaration placed?
what is an assignmen t?
what is a stream? inp ut stream? output stream? cout? cin?
what is extraction/inse rtion operator?
what is escape seque nce?
what is an input token ?
Types, Expressions, More on
Assignment
Int, Double and others
type is the kind o f data that is stored in variables
int - whole num bers
double - numbers w ith fractions
since the storage is lim ited fraction can contain only a li mited
number of digits (usua lly up to 14)
two ways to write a do uble number in C++
regular:
2.0 -3.23 +0.0456 .45 dot needs to be there
scientific or floating po int:
5.89e5 .045E-4 5e+5 dot does not n eed to be there
mantissa is limited in size.
The largest allowable number differs for every architect ure. Usually:
int - up to 3276 7
double - up to 10308
int or double (or any other type in C++) cannot c ontain a comma
other possible types a re short, float and l ong double
Type Char
char holds any single character
declaration examples :
char symbol, letter;
character constants a re put in single quotes: ‘A’ ‘+’ ‘a’
the uppercase and lo wer case of the same letter are dif ferent
characters
note the difference:
strings placed in doub le quotes and can contain multipl e
characters: “thi s is a string” “A”
single characters are stored in single quotes and can be
stored in variables of type char: ‘A’
‘Ab’ - wro ng!
Use of Char
// asks for initials and outputs greeting
#include <io stream>
main (){
char first, second;
cout << “Ent er your initials: “ ;
cin >> first >> second;
cout << “Hel lo “ << first << second << endl;
cout << “ple ased to meet you\n” ;
}
Type bool
bool (short for bo olean) is used for branching and looping
statements
a boolean variable ca n have only two values true o r false
bool result;
result = true;
true and false are b oolean constants. They are keywords
and their use is reserv ed
this type is a fairly rec ent addition to C++ and old compi lers may
not support it
pf3
pf4

Partial preview of the text

Download C++ Variables and Data Types Review - Prof. L. G. Volkert and more Study notes Computer Science in PDF only on Docsity!

Basic notions review

 what is a variable? value? address? memory location?  what is an identifier? variable name? keyword?  what is legal identifier? what identifiers are legal?  what are C-style? Pascal-style identifiers?  what is variable type? what types have we studied?  what is variable declaration? where (in the program) a variable declaration placed?  what is an assignment?  what is a stream? input stream? output stream? cout? cin?  what is extraction/insertion operator?  what is escape sequence?  what is an input token?

Types, Expressions, More on

Assignment

Int, Double and others

type is the kind of data that is stored in variables  int - whole numbers  double - numbers with fractions  since the storage is limited fraction can contain only a limited number of digits (usually up to 14)  two ways to write a double number in C++  regular: 2.0 -3.23 +0.0456 .45 dot needs to be there  scientific or floating point: 5.89e5 .045E-4 5e+5 dot does not need to be there  mantissa is limited in size.  The largest allowable number differs for every architecture. Usually:  int - up to 32767  double - up to 10^308  int or double (or any other type in C++) cannot contain a comma  other possible types are short, float and long double

Type Char

 char holds any single character  declaration examples: char symbol, letter;  character constants are put in single quotes: ‘A’ ‘+’ ‘a’  the uppercase and lower case of the same letter are different characters  note the difference:  strings placed in double quotes and can contain multiple characters: “this is a string” “A”  single characters are stored in single quotes and can be stored in variables of type char: ‘A’  ‘Ab’ - wrong!

Use of Char

// asks for initials and outputs greeting #include main (){ char first, second; cout << “Enter your initials: “; cin >> first >> second; cout << “Hello “ << first << second << endl; cout << “pleased to meet you\n”; }

Type bool

 bool (short for boolean) is used for branching and looping statements  a boolean variable can have only two values true or false bool result; result = true;  true and false are boolean constants. They are keywords and their use is reserved  this type is a fairly recent addition to C++ and old compilers may not support it

Type compatibility

 As a rule you cannot store a value of one type in a variable of another type  trying to do it leads to type mismatch int intvar; intvar = 2.99;  g++ prints this: warning: assignment to int' fromdouble'  but still compiles the program discarding the fractional part  it is usually a bad idea (but some programmers do it) to store char values in variables of type int, bools can also be in int. Even though you compiler allows it, it obscures the meaning of the variables and should be avoided

Expressions

 Expression is a mechanism of calculating new values of objects from old ones;  expression is composed of operands and operations  each expression has type as well as value  simplest expression - constant with no operation applied; examples: 23 18.53 ‘a’

Binary Integer Operations

Operation Example  addition + 2+3 a+4 ‘b’+  subtraction - count-2 4-  multiplication * 56 widthheight  division / 12/3 4/  remainder % 10%3 23%  for positive integers: if the integer division is not even, then the fractional part of the result is discarded: 12/5 produces 2  note that the fractional part is discarded and the result is never rounded up: 11/3 which should be (3.6666…) produces 3 not 4  the remainder can be used to “catch” the “missing” fraction 12%5 produces 2

Binary Double/Mixed Operations

Operation Example  addition + 2.3 + 3.  subtraction - 2.45 - 1.  multiplication * 5.4*2.  division / 12.4 / 5.  there is no remainder operations with floating point  if there are integer and floating-point operands then the integers are first converted (by compiler) to floating-point operands and then the expression is evaluated: 45.34 * 2 is converted to 45.34 * 2.

Unary Operations, Associativity

 Unary operations are allowed: +23 -2.  precedence follows mathematical conventions:

  1. Unary +, -
  2. Binary *, /, %
  3. Binary - and +  you can use () to change precedence:  (2+3)*2 changes default precedence 2 / 3 + 5 is equivalent to (2 / 3) + 5 -8 * 4 (-8) * 4 8 + 7 % 4 8 + (7 % 4)

Whole numbers in division

 When you divide int by int the result is int. It may be problematic in expression and the problem is hard to spot  g++ would not complain  this program converts feet into miles. Is there anything wrong with it? double total_price; int feet; cin >> feet; total_price = 5000 * (feet/5280);

// Compute Molecules from Mass #include #include using namespace std; int main() { cout << "Enter mass of hydrocarbon (in grams)\n" "followed by the number of carbon atoms\n" "followed by the number of hydrogen atoms\n" "(e.g. 10.5 2 6): " ; double Mass; int CarbonAtoms; int HydrogenAtoms; cin >> Mass >> CarbonAtoms >> HydrogenAtoms; // Compute Molecules from Mass(continued) const int CarbonAMU = 12; const int HydrogenAMU = 1; long int FormulaWght = (CarbonAtoms * CarbonAMU)

  • (HydrogenAtoms * HydrogenAMU); const double AvogadroNbr = 6.02e23; double Molecules = (Mass / FormulaWght) * AvogadroNbr; cout << Mass << " grams of a hydrocarbon\nwith " << CarbonAtoms << " carbon atom(s) and " << HydrogenAtoms << " hydrogen atom(s)\ncontains " << Molecules << " molecules" << endl; return 0; }