C++ Lec 2
C++ Lec 2
Not every statement is an expression. It makes no sense to talk about the value of an #include statement, for instance.
#include<iostream.h>
main(){
int k;
k=4+2;
cout<<“Hello world”;
}
2 Computer fundamentals and programming
❑ Data Types
Every expression has a type – a formal description of what kind of data its value is. For instance,0 is an integer,3.142
is a floating-point (decimal)number, and "Hello, world!\n“ is a string value(a sequence of characters). Data of
different types take a different amounts of memory to store. Here are the built-in datatypes we will use most often:
Double Double precision float point number 8 bytes -1.7e+308 to 2.3e-308 (15 digits)
Sizeof()
In order to determine the size of data types on a particular machine, C++ provides an operator named sizeof. This operator
accepts one parameter, which can be either a type or a variable itself and returns the size in bytes of that type or object:
Format Example:
sizeof(type) int a; cout<<sizeof(char);
sizeof(variable) a = sizeof (char); O/P → 1
cout<<a;
O/P → 1
C++ Program
5 Computer fundamentals and programming
❑ Variables
We might want to give a value a name so we can refer to it later. We do this using variables. A variable is a named
location in memory.
For example, say we wanted to use the value 4+2 multiple times. We might call it x and use it as follows:
(Note how we can print a sequence of values by “chaining” the <<
symbol.)
The name of a variable is an identifier token. Identifiers may contain
numbers, letters, and underscores(_), and may not start with a number.
We could replace lines 5 and 6 with a single statement that does both declaration and initialization:
int x = 4 + 2;
6 Computer fundamentals and programming
❑ Initialization of variables
Tow ways of initializing variables are valid and equivalent in C++
• type identifier = initial_value ; → int a = 0;
• type identifier (initial_value) ;→ int a (0);
#include <iostream.h>
int main () O/P
{ it main
6 ()
int a=5; // initial value = 5
int b(2); // initial value = 2
int result;// initial value undetermined
a = a + 3;
result = a - b;
cout << result;
return 0;
}
7 Computer fundamentals and programming
EX:
▪ All variable names must begin with a letter of the alphabet or an underscore( _ ). For beginning programmers, it may be easier to begin all
variable names with a letter of the alphabet.
▪ After the first initial letter, variable names can also contain letters and numbers. No spaces or special characters, however, are allowed.
▪ Uppercase characters are distinct from lowercase characters. All uppercase letters is used primarily to identify constant variables.
▪ You cannot use a C++ keyword (reserved word) as a variable name.
❑ Constants/Literals.
Constants refer to fixed values that the program may not alter and they are called literals. Constants can be of
any of the basic data types and can be divided into Integer Numerals, Floating-Point Numerals, Characters,
Strings and Boolean Values.
• Integer Numerals
- They are numerical constants that identify integer decimal values. Notice that to express a numerical constant we do
not have to write quotes (") nor any special character → Ex. 1777, 453,..
- In addition to decimal numbers ,C++ allows the use as literal constants of octal numbers (base 8) and hexadecimal
numbers (base 16). If we want to express an octal number we have to precede it with a 0 (zero character). And in
order to express a hexadecimal number we have to precede it with the characters 0x (zero, x). For example, the
following literal constants are all equivalent to each other:
Ex.
75 // decimal
0113 // octal
0x4b // hexadecimal
10 Computer fundamentals and programming
Note
Constants
A constant, like a variable, is a memory location where a value can be stored. Unlike variables, constants
never change in value. You must initialize a constant when it is created.
For example:
#define PI 3.14159265
#define NEWLINE '\n'
C++ Program
11 Computer fundamentals and programming
C++ Program
15 Computer fundamentals and programming
Input
Now that we know how to give names to values, we can have the user of the program input values.
.h>
Just as cout << is the syntax for out putting values, cin >> is the syntax for inputting values.
16 Computer fundamentals and programming
C++ Program
17 Computer fundamentals and programming
Example:
Write a C++ to input two values using cin operator and print sum and average of those values.