0% found this document useful (0 votes)
15 views

Chapter 3 Getting Started With C++ Programming

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Chapter 3 Getting Started With C++ Programming

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

CC02 MODULE FUNDAMENTALS OF PROGRAMMING

CHAPTER 3: GETTING STARTED WITH C++ PROGRAMMING

Objectives:

a.) Define identifiers know its importance


b.) Distinguish what the reserve words are and how to use data
types.
c.) Recognize how to define and declare variables and constants in
a program the basic commands used.

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.

The standard reserved keywords are:

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

➢ composed of a sequence of letters, digits, and the special character _(underscore)


➢ these are defined by the programmer, thus they should be descriptive.
➢ It must consist only of letters, digits and underscores
➢ It cannot begin with a digit or underscore.
➢ An identifier defined in the C++ standard library should not be redefined.

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.

DATA TYPE (Most Commonly Used)


Defines as the set of values that a variable can store along with a set of operations that
can be performed on that variable.

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.

Type Typical Bit Width Typical Range


char 1byte -127 to 127 or 0 to 255
int 2bytes -32,768 to 32,767
long int 4bytes -2,147,483,647 to 2,147,483,647
float 4bytes +/- 3.4e +/- 38 (~7 digits)
double 8bytes +/- 1.7e +/- 308 (~15 digits)
Table 2. Data types typical range

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.

C++ BACKSLASH CODE Sample code

#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.

Format: <data type><variable_name> ;


These are two valid declarations of variables. The first
For example: 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.

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;

This declares three variables (a, b and c), all 1 int a;


of them of type int, and has exactly the same 2 int b;
3 int c;
meaning as:

C++ assignment statement

➢ may be used to assign values to variables of any types.

Format: <variable> = Constant


Variable
Ex.: num1 = num; Arithmetic expression
num2 = 23;
num3 = tot1 + tot2;

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.

A variable can be either of global or local scope.


A. A global variable is a variable declared in the main body of the source code, outside all
functions.

Page 4
CC02 MODULE FUNDAMENTALS OF PROGRAMMING

B. A local variable is one declared within the body of a function or a block.

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.

Ex. char LETTER = ‘I’;

Page 5
CC02 MODULE FUNDAMENTALS OF PROGRAMMING

int MILES = 23;

There are two simple ways in C++ to define constants:

1. Using const keyword.


2. Using #define preprocessor.

1. Declared Constant (const)


With the const prefix you can declare constants with a specific type in the same way as you
would do with a variable:

Here, pathwidth and tabulator are two typed


1 const int pathwidth = 100;
constants. They are treated just like
2 const char tabulator = '\t';
regular variables except that their values
cannot be modified after their definition.
2. #define DIRECTIVE

• 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.

Format: #define constant_identifier literal

Ex.: #define SCHOOL “ICCT Colleges”


#define INITIAL ‘I’

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

The Sum is: 3 HELLO WORLD

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:

a = 5; The 5 in this piece of code was a literal constant.

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.

1.) Whole Numbers or Integer Numerals

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.

There is no doubt that it is a constant: whenever we write 1776 in a program, we will be


referring to the value 1776.
In addition to decimal numbers (those that all of us are used to using every day), C++ allows the
use of octal numbers (base 8) and hexadecimal numbers (base 16) as literal constants. If we want
to express an octal number we have to precede it with a 0 (a 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:

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.

75 // int In both cases, the suffix can be specified using


75u // unsigned int either upper or lowercase letters.
75l // long

2.) Real Numbers or Floating Point Literals

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:

These are four valid numbers with decimals


expressed in C++. The first number is PI,Page
the 7
second one is the number of Avogadro, the third
is the electric charge of an electron (an
extremely small number) -all of them
CC02 MODULE FUNDAMENTALS OF PROGRAMMING

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:

Any of the letters that can be part of a floating


1 3.14159L // long double
2 6.02e23f // float point numerical constant (e, f, l) can be written
using either lower or uppercase letters without
any difference in their meanings.
Note:

In defining numeric literals, the following rules should be followed:


1] No comma.
2] No space between the unary sign (+ or -) and the digits.
3] Must begin and end with a digit.

B. Non-Numeric Literals
May be in the form of a character or a series of characters(Strings).

The first two expressions represent single character constants,


'z' and the following two represent string literals composed of
'p' several characters. Notice that to represent a single character
"Hello world" we enclose it between single quotes (') and to express a string
"How do you do?"
(which generally consists of more than one character) we
enclose it between double quotes (").

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

For more knowledge Fundamentals of Programming, please check the


link provided;

➢ 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

You might also like