Literals in C (Part 1)

Literals in C, their types and hexadecimal, octal bases

Last Reviewed and Updated on February 7, 2020
Posted by Parveen(Hoven),
Aptitude Trainer and Software Developer

My C/C++ Videos on Youtube

Here is the complete playlist for video lectures and tutorials for the absolute beginners. The language has been kept simple so that anybody can easily understand them. I have avoided complex jargon in these videos.

What is a literal?

A constant value is a literal. Examples are -

  1. 82 is numeric literal.
  2. 'A' is a character literal.
  3. "Hoven" is a string literal.

Integer literals

An integer numeric literal is a number. It can have a base, like we have base 2, base 10, base 8, base 16 numbers. If it starts with zero, then it is a base 8 number. If it starts with 0x then it is a base 16 number. Otherwise it is a base 10 number. Some examples are -

  1. 056 is a base 8 number.
  2. 56 is a base 10 number.
  3. 0x56 is a base 16 number.

You can use type specifiers on integral literals to tell the compiler to treat them as unsigned/signed int/long, etc., For example 45u or 45U is an unsigned int. 45L or 45l will be of the type long. 67UL will be of the type unsigned long.

Floating Point Literals

A literal that has a decimal point is a floating point literal. Examples are 7.56, 3.18E+7. Please note that 3.18E+7 is same as 3.18 X 107. If you want the compiler to treat your floating point literal as a float type then you can use a type specifier: 4.36F or 4.36f. You just have to suffix a lowercase or uppercase "f". If you donot use a type modifier, then the literal would be treated as of the type double.

Character constants

Character constants are single characters of text. They are stored in single quotes. For example 'A', '$', etc., are all character constants or character literals. You cannot put two of them in a single quotes. This is wrong: 'YT'. But there is one exception to this rule. Escape sequences start with a slash. Escape sequences are special characters. For example, '\n' is an escape sequence. This is typed as two characters, but actually it is a single newline character. Another example is the tab character: '\t'. More examples are available here.


Character Meaning
\a bell
\b backspace
\f formfeed
\n newline
\r Carriage return
\t Tab
\v Vertical tab
\\ Backslash
\' Single quote
\" double quote
\q Question mark

String Literals

String literals are written inside double quotes. Like "Hoven". A string literal is a series of single characters where the last character is the hidden NULL character of ASCII value 0. So the string "Hoven" is a series of 6 characters, the last is the hidden NULL, which is never printed.

Automatic or Implicit Conversions

A compiler will convert one type to another automatically, if the conversion would not lead to a loss of accuracy. In the code below, the literal 8 is a base 10 literal integer number. When it is stored into a float, the compiler will automatically change it's type to a float. This conversion involves no loss of accuracy because 8 is same as 8.0.


float rx = 8;

Explicit Conversions

An explicit conversion is done by the software developer, not the compiler. Such types of conversions are required when there could be a loss of accuracy.


int x = 8.67F;

In the above code we are storing a floating point type of literal into an int type of variable called x. The value that ultimately gets stored in x is 8, and not 8.67. This means a loss of accuracy is going to occur. 8.67 will be truncated to 8. In such cases an explicit cast must be specified as a good programming practice.

Classroom Training

Classroom training in C/C++ is available at our training institute. Click here for details. You can also register yourself here.

Video Tutorial

We have created a video tutorial that can be downloaded and watched offline on your windows based computer. It is in the form of an exe file. No installation is required, it runs by double clicking the file. Since the exe file is hosted on the Google Drive, it should be very safe because Google itself checks for any virus or malware. The video can be watched offline, no internet is required.

Pricing

This single video will cost you USD $5. When you download the video, you will be able to watch the first few minutes for free, as a sample. The video app will prompt you for payment through PayPal and other payment options. If you want ALL VIDEOS, NOT JUST THIS ONE then go here: Complete Set of C/C++ Video Tutorials The entire set is priced at USD $50.

Screenshots

This is a screenshot of the software that should give you a good idea of the available functionality. Click on the image to see a larger view.screenshot of the video being played in the software

Installation

This software doesn't require any installation. Just download and double-click to run it. It doesn't require administrative priviliges to run. It runs in limited access mode, so should be very safe.

supports windows xp and later Supported Operating Systems

These videos can be run on 32-bit as well as 64-bit versions of the Microsoft Windows Operating Systems. Windows XP and all later OS are supported. If you want to run it on Windows XP and Windows Vista, then you must also have the .NET Framework Version 3.5 installed on your machines. Those users who have Windows 7 and later donot have to worry because these operating systems already have the .NET Framework installed on them.

Download Links

IMPORTANT NOTE: This download contains ONLY ONE video. The video is about the topic of this tutorial.

Want ALL Videos? If you want ALL VIDEOS, NOT JUST THIS ONE then go here: Complete Set of C/C++ Video Tutorials. TIP: If you plan to buy these videos, then it would be more economical to buy the entire set.

DISCLAIMER: Even though every care has been taken in making this software bug-free, but still please take a proper backup of your PC data before you use it. We shall not be responsible for any damages or consequential damages arising out of the use of this software. Please use it solely at your own risk.

Please download the software here.
download the video Literals in C(Part 1)



Creative Commons License
This Blog Post/Article "Literals in C (Part 1)" by Parveen (Hoven) is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Updated on 2020-02-07. Published on: 2015-10-05