You are on page 1of 12

C++ Programming Language

Basics
Very brief history of C++

C++
“If you’re not at all interested in performance,
When to choose C++ shouldn’t you be in the Python room down the hall?”
― Scott Meyers (author of Effective Modern C++)

• Despite its many competitors C++ has • Choose C++ when:


remained popular for ~30 years and will • Program performance matters
continue to be so in the foreseeable future. • Dealing with large amounts of data, multiple
CPUs, complex algorithms, etc.
• Why?
• Programmer productivity is less important
• Complex problems and programs can be
• It is faster to produce working code in Python,
effectively implemented
R, Matlab or other scripting languages!
• OOP works in the real world!
• The programming language itself can help
• No other language quite matches C++’s organize your code
combination of performance, expressiveness, • Ex. In C++ your objects can closely model
and ability to handle complex programs. elements of your problem
• Access to libraries
• Ex. Nvidia’s CUDA Thrust library for GPUs
• Your group uses it already!
Basics of a Typical C++ Environment
Program is created in
Editor Disk the editor and stored

• Phases of C++ Programs: on disk.

Preprocessor Preprocessor program


Disk
1. Edit
processes the code.

Compiler creates
Compiler object code and stores

2. Preprocess
Disk
it on disk.

Linker links the object


code with the libraries,

3. Compile
Linker Disk creates a.out and
stores it on disk

Primary

4. Link Loader
Memory

Loader puts program

5. Load Disk ..
in memory.

..

6. Execute
..

Primary
Memory
CPU CPU takes each
instruction and
executes it, possibly
storing new data
values as the program
.. executes.
..
..
1 // Fig. 1.2: fig01_02.cpp

2 // A first program in C++ Comments


3 #include <iostream> Written between /* and */ or following a //
Improve program readability and do not cause the
4 computer to perform any action

First 5

6
int main()

C++ 7 std::cout << "Welcome to C++!\n";


preprocessor directive
Message to the C++ preprocessor
Lines beginning with # are preprocessor directives
Progr
8
#include <iostream> tells the preprocessor to
9 return 0; include
// indicate that program ended the contents of the file <iostream>, which
successfully

am 10 }
includes
C++ input/output
programs
the
one ofscreen).
operations
contain one
which must be main
(such as printing
or more functions, exactly to

Parenthesis used to indicate a function


int means that main "returns" an integer value.
More
Prints the string of characters contained in Chapter
between the 3.
quotation marks.
return is one a way to exit a
function. Left brace { begins the body of every function
The entire line, including std::cout, andthe << brace } ends it
a right
return 0, in this case, means that
operator, the string "Welcome to C++!\n" and
the program terminated normally.
the semicolon (;), is called a statement.

All statements must end with a semicolon.


• std::cout
• standard output stream object
• “connected” to the screen
• we need the std:: to specify what
"namespace" cout belongs to Escape Sequence Description
• we shall remove this prefix with using
Newline. Position the screen cursor to the
statements \n
beginning of the next line.

• << \t
Horizontal tab. Move the screen cursor to the next
tab stop.
• stream insertion operator Carriage return. Position the screen cursor to the
\r beginning of the current line; do not advance to
• value to the right of the operator (right the next line.
operand) inserted into output stream (which \a Alert. Sound the system bell.
is connected to the screen) \\ Backslash. Used to print a backslash character.
std::cout << " Welcome to C++!\n" \"
Double quote. Used to print a double quote
character.
•\
• escape character
• indicates that a “special” character is to be
output
Keywords
• Keywords also known as reserved words
• It has special meaning to compiler and used for special purpose
• All keywords are written in lower case
• Below list of keywords are additional in C++
Identifiers
• Identifiers are used to refer to name of variable, functions, arrays and
other user-defined data types
• Rules for Identifiers
• Only alphabetic characters, digits and underscore is allowed
• First character can’t be digit of identifier
• A keyword can’t be used as identifier
• Identifiers are case sensitive

Valid Identifiers Invalid Identifiers


Result Sum-1
Test2 2data
_sum break
C++ Data Types
• Primitive data types can be directly used to declare the variable
• Integer – Keyword int used for integer data types
• Character – Keyword char used for character data types
• Boolean – Keyword bool used for Boolean data types
• Floating Point – Keyword float used for single precision floating point
numbers
• Double Floating Point – Keyword double used for double precision floating
point numbers
• Valueless or Void – Keyword void means without any value used for functions
which don’t return any value
• Wide Character – Keyword wchar_t used for characters with greater size than
char data type
Data Type Size (in bytes) Range Name Expresses
short int 2 -32,768 to 32,767
CHAR_MIN The minimum value for an object of type char

unsigned short int 2 0 to 65,535


CHAR_MAX Maximum value for an object of type char

unsigned int 4 0 to 4,294,967,295


SCHAR_MIN The minimum value for an object of type Signed char

int 4 -2,147,483,648 to 2,147,483,647


SCHAR_MAX Maximum value for an object of type Signed char

long int 4 -2,147,483,648 to 2,147,483,647


UCHAR_MAX Maximum value for an object of type Unsigned char

unsigned long int 4 0 to 4,294,967,295


CHAR_BIT Number of bits in a char object

long long int 8 -(2^63) to (2^63)-1


MB_LEN_MAX Maximum number of bytes in a multi-byte character

unsigned long long int 8 0 to 18,446,744,073,709,551,615


SHRT_MIN The minimum value for an object of type short int
signed char 1 -128 to 127
SHRT_MAX Maximum value for an object of type short int
unsigned char 1 0 to 255
USHRT_MAX Maximum value for an object of type Unsigned short int
float 4 -3.4×10^38 to 3.4×10^38
INT_MIN The minimum value for an object of type int
double 8 -1.7×10^308 to1.7×10^308
INT_MAX Maximum value for an object of type int
long double 12 -1.1×10^4932 to1.1×10^4932
UINT_MAX Maximum value for an object of type Unsigned int
wchar_t 2 or 4 1 wide character
LONG_MIN The minimum value for an object of type long int

LONG_MAX Maximum value for an object of type long int

ULONG_MAX
sizeof() operator: sizeof() operator is used to find the number of Maximum value for an object of type Unsigned long int

bytes occupied by a variable/data type in computer memory. LLONG_MIN The minimum value for an object of type long long int

LLONG_MAX Maximum value for an object of type long long int

Maximum value for an object of type Unsigned long long


ULLONG_MAX int
Variables in C++
• Name of memory location used to store data its value can be changed
during execution
• Variable declaration:
• Single variable Declaration
Data_type Variable_name;
• Multiple Variable Declaration
Data_type variable_name1,variable_name2,…;
• Variable declaration and initialization
Data_type Variable_name = value;
C++ Basic Input / Output
• C++ I/O is using stream(sequence of bytes) concept.
• If bytes flow from main memory to device then it is called output
operation
• std::cout<<“Welcome to Program”;

• If bytes flow from a device to main memory then it is called input


operation
• std::cin>>variable_to_store;

You might also like