C++ Menu9>
- Introduction
- Basics
- Loop Types
- Intermediate
- Arrays in C++
- Strings in C++
- Pointers in C++
- References in C++
- Functions in C++
- Call by Value Vs Call by Reference
- Member functions in C++
- Static Members in C++
- const in C++
- Inline function in C++
- Friend Function and Friend Class
- virtual
- Function Overriding in C++
- Function Overloading in C++
- Difference between Function Overriding and Function Overloading in C++
- Dynamic Memory Allocation
- New
- Delete
- This Pointer
- Introduction to Classes and OOPs Concept
- OOPS Concept in C++
- Classes and Objects in C++
- Class v/s Object
- Encapsulation in C++
- Data Abstraction in C++
- Access Modifiers
- Implicit Conversion in C++
- Constructors and Destructors
- Constructors
- Types of Constructors
- Parameterized Constructor in C++
- Default Constructor in C++
- Destructors
- Copy Constructors
- Conversion Constructors
- OOPs Advanced - 1
- Inheritance
- Types of Inheritance in c++
- Single Inheritance
- Multilevel Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Hybrid or Multipath Inheritance
- Constructors and Destructors with Inheritance
- Polymorphism
- Function Overriding in C++
- Function Overloading in C++
- Compile Time Polymorphism in C++
- RunTime Polymorphism in C++
- Difference between Compile-time and Runtime Polymorphism
- Dynamic Binding (Late Binding) in C++
- Static Binding (Early Binding) in C++
- Early binding and late binding in C++
- Upcasting and Downcasting in C++
- Upcasting in C++
- Downcasting in C++
- Operator Overloading (detailed)
- Input/Output Operators Overloading in C++
- Assignment Operators Overloading in C++
- Function Call Operator () Overloading in C++
- Class Member Access Operator (->) Overloading in C++
- Unary Operator Overloading in C++
- Binary Operator Overloading in C++
- Relational operator overloading in C++
- Overloading ++ and — increment and Decrement Operators in C++
- Constructor Overloading in C++
- OOPs Advanced - 2
- Basic programs Set 1
- A character is a vowel or consonant
- A character is an alphabet or not
- Ascii values of a character
- Uppercase, Lowercase or special character
- A number is positive or negative
- A number is even or odd
- Area of a circle
- LCM of two numbers
- GCD of two numbers
- HCF of two numbers
- Greatest of two numbers
- Greatest of three numbers
- Number of digits in an integer
- Sum of digits of a number
- Sum of N natural numbers
- Sum of numbers in a given range
- Reverse of a given number
- Factorial of a number
- Fibonacci series up to n
- Leap year or not
- Prime number or not
- Palindrome or not
- Armstrong number or not
- Strong number or not
- Perfect number or not
- Automorphic number or not
- Harshad number or not
- Abundant number or not
- Power of a number
- Factors of a number
- Add two fractions
- Basic programs Set 2
- Prime numbers in a given range
- Armstrong numbers between two intervals
- Can a number be expressed as a sum of two prime numbers?
- Replace all 0’s with 1 in a given integer
- Binary to decimal conversion
- Decimal to binary conversion
- Decimal to octal conversion
- Octal to decimal conversion
- Binary to octal conversion
- Octal to binary conversion
- Maximum number of handshakes
- Quadrants in which coordinates lie
- Convert digit/number to words
- Number of days in a given month of a given year
- Permutations in which n people can occupy r seats in a classroom
- Number of times x digit occurs in each and every number from 0 to n
- Number of integers which has exactly x divisors
- Roots of a quadratic equation
- Count possible decoding of a given digit sequence
- Miscellaneous Coding Questions
Literals in C++
What are Literals ?
At its core, a literal is a fixed value that appears directly in the source code of a program. When the program is compiled, these values remain constant and cannot be changed during the execution of the program. C++ supports various types of literals, and each type serves a specific purpose.
Introduction
Consider the following examples:
Literals | Example |
Integer Literal | int myInteger = 42; |
Floating-Point Literal | float myFloat = 3.14; |
Character Literal | char myChar = ‘A’; |
String Literal | std::string myString = “Hello, world!”; |
Boolean Literal | stbool isTrue = true; |
Null Pointer Literal | int* myPointer = nullptr; |
Integer Literals
Integer literals represent whole numbers without a fractional or decimal component. They can be specified in different formats, such as decimal, octal, or hexadecimal. C++ also allows the use of binary literals for better readability in representing binary numbers.
Literals | Example |
Decimal | int myDecimal = 42 |
Octal | int myoctal = 052 |
Hexadecimal | int myHex = ox2A |
Binary | int myBinary = 0b101010 |
Floating-Point Literals
Floating-point literals are used to represent real numbers with fractional parts. They consist of a whole number part, a decimal point, a fractional part, and an optional exponent. Programmers can use either the traditional notation or scientific notation to represent floating-point literals.
Literals | Example |
Traditional Notation | double myDouble = 3.14; |
Scientific Notation: | double mySciNotation = 3.0e8; |
Character Literals
Character literals represent single characters and are enclosed within single quotes (”). They allow programmers to work with individual characters in C++.
Literals | Example |
Character Literal | char myChar = ‘A’; |
String Literals
String literals, on the other hand, represent sequences of characters and are enclosed within double quotes (“”). They allow programmers to handle textual data, making string manipulation much more manageable.
Literals | Example |
String Literal | std::string myString = “Hello, world!”; |
Boolean Literals
Boolean literals represent truth values and can take one of two values: true or false. They are essential in conditional statements and logical operations.
Literals | Example |
Boolean Literal | bool isTrue = true; |
Null Pointer Literal
C++ introduces the concept of the null pointer literal, represented by the keyword “nullptr.” It is used to indicate that a pointer is not pointing to any memory location.
Literals | Example |
Null Pointer Literal | int* myPointer = nullptr; |
User-Defined Literals
One of the exciting features of C++ is user-defined literals. Programmers can define their own custom literals to work with specific data types and provide a more expressive syntax for their applications.
constexpr long double operator"" _inch(long double x) { return x * 2.54; } // Usage: long double lengthInCm = 10.0_inch; // Converts 10 inches to centimeters
Literal Suffixes
C++ allows literal suffixes to be appended to numeric literals. These suffixes indicate the data type of the literal, providing control over the type and size of the literal.
long long myLong = 42LL; float myFloat = 3.14f; unsigned int myUnsigned = 42u;
Combining Literals with Variables and Constants
Literals are often used in conjunction with variables and constants in C++. Understanding how to combine literals with other elements of the language is essential for writing powerful and flexible programs.int radius = 5; double area = 3.14 * radius * radius;
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others