C++ Basics
C++ Basics
Character Set
• Character set is the combination of English language
(Alphabets and White spaces) and math's symbols (Digits
and Special symbols).
• 1. Alphabets:
• 2. Digits:
• All the keyboard keys except alphabet, digits and white spaces are
the special symbols.
• 4. White Spaces:
• White spaces has blank space, new line return, Horizontal tab
space etc are all used for special purpose.
C++ Tokens
• Tokens are the smallest building block or smallest unit of a C++ program.
• Identifiers
• Keywords
• Constants
• Operators
• Strings
Identifiers
• A C++ identifier is a name used to identify a variable, function, class, module, or
any other user-defined item.
• C++ does not allow punctuation characters such as @, $, and % within identifiers.
• e.g; +, -, *, /
• The double quoting is what makes the difference; when the text is
enclosed between them, the text is printed literally; when they are
not, the text is interpreted as the identifier of a variable, and its
value is printed instead.
Standard Input (cin)
• In most program environments, the standard input by default is the keyboard, and the C++
stream object defined to access it is cin.
• cin is used together with the extraction operator, which is written as >> (i.e., two "greater
than" signs).
• This operator is then followed by the variable where the extracted data is stored. For example:
• int age;
• The first statement declares a variable of type int called age, and the second extracts from
cin a value to be stored in it.
• This operation makes the program wait for input from cin; generally, this means that the
program will wait for the user to enter some sequence with the keyboard. In this case, note that
the characters introduced using the keyboard are only transmitted to the program when the
ENTER (or RETURN) key is pressed.
Extraction operator (>>)
• cin >> X;
• It gets its name from the idea of extracting data from the
input stream.
• cout << "This " << " is a " << "single C++ statement”;
• cout << "I am " << age << " years old and my
zipcode is " << zipcode;
C++ Manipulators
• Manipulators are operators used in C++ for formatting output.
• endl Manipulator:
• This manipulator has the same functionality as the ‘\n’ newline character.
• Its purpose is to point the cursor to the beginning of the next line.
• For example:
• Syntax:
• setw(x)
• puts(“Hello world”);
Concept of Data types
• Data type species the different types of data a program can
work with.
• Data types in any of the language mean that what are the
various type of data the variables can have in that particular
language.
• The term built-in means that they are pre-defined in C++ and can be used directly in a
program.
• Example: int, char , float, bool etc. Primitive data types available in C++ are:
• Integer
• Character
• Boolean
• Floating Point
• Valueless or Void
• Wide Character
• The various built-in data types are:
1. int data type: The int data type is used for integers. Integers are numbers with no
decimal point. For example : 24, -90, 1234, 0 etc.
2. float data type: This data type is used for numbers that have a decimal point which are
commonly called floating-point numbers. For example: 76.45, 12.4, -90.6 etc.
3. double data type: This data type is also used for floating-point numbers. But the
difference is that the range and precision of this data type is greater than that of float. In
other words, it is used for large floating-point numbers.
4. char data type: This data type is used for characters. It can be used for any valid
character in C++ and not just alphabets. Here is a special thing about this data type:
characters are stored in memory using their ASCII codes which are numeric i.e. integers.
Thus, char type is basically the same as int data type.
5. bool data type: bool refers to boolean. This data type has only two values: true and false.
6. void data type: The word void means empty. So it has no value. This is used as return
type for functions that do not return a value.
7. Wide Character: Wide character data type is also a character data type but this data
type has size greater than the normal 8-bit datatype. Represented by wchar_t. It is
generally 2 or 4 bytes long.