Chapter 3 Getting Started With C++ Programming
Chapter 3 Getting Started With C++ Programming
Objectives:
KEYWORDS
Keywords are predefined reserved identifiers that have special meanings. They
cannot be used as identifiers in your program.
Reserved words
Words “reserved” by the programming language for expressing various statements
and constructs, thus they may not be redefined by the programmer.
asm, auto, bool, break, case, catch, char, class, const, const_cast, continue, default, delete, do,
double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline,
int, long, mutable, namespace, new, operator, private, protected, public, register, reinterpret_cast,
return, short, signed, sizeof, static, static_cast, struct, switch, template, this, throw, true, try,
typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t, while
Additionally, alternative representations for some operators cannot be used as identifiers since
they are reserved words under some circumstances:
and, and_eq, bitand, bitor, compl, not, not_eq, or, or_eq, xor, xor_eq
Your compiler may also include some additional specific reserved keywords.
IDENTIFIERS
Page 1
CC02 MODULE FUNDAMENTALS OF PROGRAMMING
Very important: The C++ language is a "case sensitive" language. That 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 isnot the same as
the result variable or the Result variable. These are three different variable identifiers.
Type Description
bool Stores either value true or false.
char Typically a single octet(one byte). This is an integer type.It represents
the character on your keyboard – letters of the alphabet, numbers or
special character
int The most natural size of integer for the machine. It is a whole number
or a series of decimal digits which can be preceded by a positive or
negative sign.
float A single-precision floating point value. A number with decimal part
double A double-precision floating point value. a special float which can store
more significant digits and take up more memory space.
long a large whole number
Table 1. Commonly used data types
Typical range
The following table shows the variable type, how much memory it takes to store the value
memory and what is maximum and minimum vaue which can be stored in such type of variables.
Page 2
CC02 MODULE FUNDAMENTALS OF PROGRAMMING
NoteThe values of the columns Size and Range depend on the system the program is
compiled for. The values shown above are those found on most 32-bit systems. But for
other systems, the general specification is that int has the natural size suggested by the
systemarchitecture (one "word") and the four integer types char, short, int and long must
each one be at least as large as the one preceding it, with char being always one byte in size.
The same applies to the floating point types float, double and long double, where each one must
provide at least as much precision as the preceding one.
#include<iostream.h>
CODE MEANING #include<conio.h>
main()
\a alert (bell) {
\n newline
cout<<”H\nE\nL\nL\nO”;
\b backspace
\f formfeed cout<<”\t\”WORLD\””;
\r carriage return getch();
\t tab }
Output
\v vertical tab
\\ backslash H
\’ single quote mark E
\” double quote mark L
\0 null L
O
“WORLD”
DATA VALUES
VARIABLES- identifiers that can store value that can be changed and can store literals or some
type of structured data.
Variable declaration
➢ consist of a data type and a variable name.
➢ The effect is that it reserves memory space for each of the variable it declares.
In order to use a variable in C++, we must first declare it specifying which data type we want
it to be. The syntax to declare a new variable is to write the specifier of the desired data type (like
int, bool, float...) followed by a valid variable identifier.
Page 3
CC02 MODULE FUNDAMENTALS OF PROGRAMMING
1 int a;
2 float mynumber;
If you are going to declare more than one variable of the same type, you can declare all of them
in a single statement by separating their identifiers with commas. For example:
int a, b, c;
SCOPE OF VARIABLES
All the variables that we intend to use in a program must have been declared with its type specifier
in an earlier point in the code, like we did in the previous code at the beginning of the body of the
function main when we declared that a, b, and result were of type int.
Page 4
CC02 MODULE FUNDAMENTALS OF PROGRAMMING
Diagram 2.
Scope of variables
Global variables can be referred from anywhere in the code, even inside functions, whenever it
is after its declaration.
The scope of local variables is limited to the block enclosed in braces ({}) where they are
declared. For example, if they are declared at the beginning of the body of a function (like in
function main) their scope is between its declaration point and the end of that function. In the
example above, this means that if another functionexisted in addition to main, the local variables
declared in main could not be accessed from the other function and vice versa.
CONSTANTS
Constants are expressions with a fixed value. It is identifiers that can store values that
cannot be changed.Usually are all capital letters with underscores between each word to
distinguished from variable.
Page 5
CC02 MODULE FUNDAMENTALS OF PROGRAMMING
• Directs the compiler to replaces all the instances of constant_identifier with literal value
whenever it appears in the program.
• The main use to a #define directive is to make it easy for the programmer to change all
occurrences of a certain value throughout a program.
• A #define line can occur anywhere in a program, but are normally placed at column 1. it
affects only those lines that come after it.
Sample code
char a;
const int a=1; #define a ”HELLO WORLD”;
const int b=2; cout<<a;
cout<<”The Sum is : “<<a+b;
Output
TYPES OF DATA
LITERALSare the most obvious kind of constants. They are used to express particular
values within the source code of a program. We have already used these previously to give
Page 6
CC02 MODULE FUNDAMENTALS OF PROGRAMMING
concrete values to variables or to express messages we wanted our programs to print out, for
example, when we wrote:
Literal constants can be divided in Numeric Literals, Non-Numeric Literal and the Boolean
Literals
A. Numeric Literals
Can be further sub divided into Whole Numbers and Real Numbers.
They1 are
1776numerical constants that identify integer decimal
values.707
2 Notice that to express a numerical constant we do not
3 -273
have to write quotes (") nor any special character.
1 75 // decimal
2 0113 // octal
3 0x4b // hexadecimal
All of these represent the same number: 75
(seventy-five)
Literal constants, like variables, are considered expressed
to have a specific data as a base-10
type. numeral,
By default, integer
octal numeral and hexadecimal
literals are of typeint. However, we can force them to either be unsigned by appending numeral,
the u character to it, or long by appending /: respectively.
They express numbers with decimals and/or exponents. They can include either a decimal
point, an e character (that expresses "by ten at the Xth height", where X is an integer value that
follows the e character), or both a decimal point and an e character:
1 3.14159 // 3.14159
2 6.02e23 // 6.02 x 10^23
3 1.6e-19 // 1.6 x 10^-19
4 3.0 // 3.0
The default type for floating point literals is double. If you explicitly want to express a float or
a long doublenumerical literal, you can use the f or l suffixes respectively:
B. Non-Numeric Literals
May be in the form of a character or a series of characters(Strings).
Note:
Character literals are enclosed in single quotes. If the literal begins with L (uppercase
only), it is a wide character literal (e.g., L'x') and should be stored in wchar_t type of
variable . Otherwise, it is a narrow character literal (e.g., 'x') and can be stored in a simple
variable of char type.
String literals are enclosed in double quotes. A string contains characters that are similar to
character literals: plain characters, escape sequences, and universal characters. You can break
a long lines into multiple lines using string literals and separating them using whitespaces.
Page 8
CC02 MODULE FUNDAMENTALS OF PROGRAMMING
➢ https://www.youtube.com/watch?v=LF5nSeNamvg
➢ https://www.youtube.com/watch?v=zB9RI8_wExo
➢ https://www.youtube.com/watch?v=jzV1fPOA1wc
REFERENCES
❖ https://www.w3schools.com/cpp/cpp_variables.asp
Page 9