Lecture 6 - Literals in C(Part 2)| C/C++ Video Tutorials and Lectures for Beginners
Channel: Hoven
Date: 2016-06-02

Complete list: http://hoven.in/Home/Videos/CPP-Video-Tutorials/

Transcription
=============

A suffix is attached at the end of a literal number.
This value has no suffix, so this is by default, a signed value.
If u is suffixed to a literal, then it becomes an unsigned value.
When an L is suffixed, the value becomes a long type.
The suffix is acceptable in both lowercase and uppercase.
But we recommend using it with uppercase L.
The reason is that it is possible to confuse the lowercase l with the digit 1.
It is easy to see that this is an unsigned long literal constant.
Suffixes can be applied to floating point values also.
This value is without any suffix.
Compiler treats it as a double value.
When an F is suffixed, the value becomes a float type of value.
A character literal is a character of text.
Character literals are written inside single quotes.
A literal is internally stored as a number.
This number is called it's ASCII or UNICODE value.
This is wrong. It's a syntax error.
Only one character can be stored in a pair of single quotes.
This expression stores the character literal A in a variable called c.
But since it is basically a char number, A is stored internally stored as 65.
The ASCII value of A is 65.
Here I would like to mention about this data type.
This type is a type of char data type, but it is used for storing UNICODE characters.
UNICODE is the next step from the ASCII set which can support only 255 characters.
The UNICODE is an extended character set.
It contains characters of all languages of the world, like Arabic.
It includes characters of all scripts ever used by human beings.
When an L is prefixed to a character literal, it becomes a wide character.
A string is a sequence or series of characters.
This is a string of characters.
A string is written inside double quotes.
This is a string of one character.
When a character is written inside single quotes, it is a UNICODE or ASCII character.
When the same char is written inside a double quotes, it becomes a string.
A string is known from the double quotes.
Some characters have special meanings.
One such character is the n character.
An n is also used to indicate that a new line of text is being created.
So an n is used as a new line character.
When an n is used as a newline character, it is preceded by a slash.
This slash tells the compiler that n is being used for a special purpose.
This is called "Escaping".
Both the slash and n are together called a new line character.
Even though they are two characters, but when written together, they become one.
They are called the new line character.
A new line character can be written in single quotes because it is a character.
This is a tab character.
This is the null character having the ASCII value of zero.
This is a very important character in C as we shall see later.
Sometimes Integral literals get converted from one type to another.
These conversions can be automatic.
These conversions are done by the compiler.
Consider this code.
In this statement, a signed integer is being stored in a float.
Obviously, the integer will be automatically promoted to a float type.
This is an automatic conversion. But it is a fairly harmless one.
It's safe because 4 is converted to 4.0 without any loss of accuracy.
Sometimes a literal can lose accuracy during conversion.
Conversions involving a loss are not automatically done.
Some compilers give a warning during such conversions.
In this code a float is being stored in an integer type.
This conversion will cause the decimal point to be dropped.
x will get 4.
This code will no doubt run but at the cost of a truncation or loss.
If the developer is doing it knowingly, then he must tell it to the compiler.
C++ has a concept of an explicit cast for doing this..
An explicit cast is the opposite of an automatic conversion.
One of the ways of explicit casting is to use the round brackets as shown.
The compiler will remove the warning, if it was showing it earlier.
You might be wondering what practical difference was made by casting?
The answer is: no practical difference at all.
In both the cases the variable x gets 4.
It's about the developer's workmanship.
A good developer expresses his intent by using the casting feature of C++.
We learnt about the various suffixes on integer and floating point literals.
We also learnt about char and wide char literals.
Finally, we learnt about automatic and explicit casting in C.