Fundamentals
Fundamentals
• These are the directives, which give instructions to the compiler to preprocess the information before
actual compilation starts.
• Lines included in the code of programs preceded by a hash sign (#). They are not program statements but
directives for the preprocessor.
Directive Purpose
#include Links a header file in the source code
#define Creates a symbolic or macro constant Check the content
#undef Deletes an existing macro of *.i file
Comments
• Used to improve readablity of the program
• // (double slash) single line comments
• /*, */ - Multiline comments
I/P operator
• The statement ”cin >> number;” is an input statement. The operator << is known as the get-from operator. It
extracts value from the keyboard and assigns it to the variable on its right.
• The program waits for the user to type in a number. The number keyed in is placed in the variable number.
• The identifier cin is a predefined object in C++ that corresponds to the standard input stream. Here this stream
represents the keyboard.
cin >> number1 >> number2;
O/P operator
• The statement ”cout << number;” is an input statement. The operator << is known as the insertion operator. It
prints the output on its right on the screen.
• The identifier cout is a predefined object in C++ that corresponds to the standard output stream. Here this stream
represents the screen.
cout << number1 << number2;
Namespace
• Namespace is a new concept introduced by the ANSI C++ standards committee.
• The C++ Standard Library is vast, encompassing a wide range of classes, functions, and objects.
• These components are encapsulated within the std namespace to avoid naming conflicts with user-
defined code and other libraries.
• When you write using namespace std;, you are essentially bringing all these names into the global
scope, which can lead to several issues.
• scope resolution operator operator gives you more freedom in naming your variables by letting you
distinguish between variables with the same name.
Scope resolution operator
C++ resolves this problem by introducing a new operator (::) called the
scope resolution operator.
C++ resolves this problem by introducing a new operator (::) called the
scope resolution operator.
• They are explicitly reserved and can’t be used as names for user-defined program
elements (such as variables and function names).
• Only lowercase and alphabetical characters (int, void, public, private, namespace)
Identifiers
• Identifiers are names given to variables, functions, arrays, and other user-defined items.
• An identifier can consist of alphabetical characters, digits, and underscores. It can be both
uppercase and lowercase.
• No special symbols or punctuations in keywords and identifiers. The special character ‘_’ can be
used in identifiers.
Constants
• Constants in C++ refer to variables with fixed values that cannot be changed. They remain
constant throughout the execution of the program.
• Constants can be integers, floating-point numbers, characters, or strings.
Strings
• Strings are nothing but an array of characters ended with a null character (‘\0’).
• Strings are always enclosed in double quotes. Whereas a character is enclosed in single quotes in
C and C++.
Operators
• These are symbols that trigger an action when applied to variables and other objects.
• The data on which they operate are called operands.
• C++ has a rich set of operators. All C operators are valid in C++ also. Ex:- Arithmetic operators,
relation operators, logical operators, bitwise operators, Assignmet operators.
• In addition, C++ introduces some new operators.
|| Logical OR Left-to-right
?: Ternary conditional Right-to-left
= += -= *= /= %= &= ^= |= <<= >>= Assignment and Right-to-left
compound assignment
comma Left-to-right
Memory Management operator
• C uses malloc and calloc functions to allocate memory dynamically at run time.
• Similarly, it uses the function Free( ) to free dynamically allocated memory. We use dynamic
allocation techniques when it is not known in advance how much memory space is needed.
• C++ also supports those functions it also defines two unary operators new and delete that
perform the task of allocating and freeing the memory in a better and easier way.
• new can be used to create a memory space for any data type including user-defined such as
arrays, structures, and classes.
To free a dynamically allocated array, we must use the following form of delete.
delete[size] pointerVariable;
delete[] pointerVariable;
Data types in C++
• Primitive - These are the built in data types to be used directly by user
Integer, Character, Boolean, Float, Double, Void, wide character
• User-defined or Abstract
Class, Structure, Union, Enumeration, Typedef
Primitive data types
Both C and C++ compilers support all the built-in types.
int Integer 2 or 4
float Floating point 4 Except void, all the basic datatypes may
have several modifiers preceding them
double Double floating point 8
The modifiers are signed, unsigned,
char Character 1 long, and short and may apply to
character and integer basic data types.
wchar_t Wide Character 2
• Arrays
• Reference
• Functions
• Pointers
Arrays
• In C/C++, an array is a data structure that is used to store multiple values of similar data types in a
contiguous memory location.
DataType ArrayName[Size];
• For example, if we make the variable sum a reference to the variable total, then sum and total
can be used interchangeably to represent the variable.
• Reference variables points to the same memory location as the original variable. Thus, changing
one variable results in the value of the other variable also getting changed.