Literally!
In C, literals are ways to express specific values within the source code. Different types of literals include integer, floating-point, character, and string literals.
Here's a sample program with explanations:
#include <stdio.h>
int main() {
// Integer literals
int decimal = 10; // Decimal integer literal
int octal = 012; // Octal integer literal (prefixed with 0)
int hexadecimal = 0xA; // Hexadecimal integer literal (prefixed with 0x)
// Floating-point literals
float floatingPoint = 3.14; // Floating-point literal
double exponential = 1.5e2; // Exponential notation (1.5 * 10^2)
// Character literals
char singleChar = 'A'; // Character literal (enclosed in single quotes)
char escapedChar = '\n'; // Escaped character (newline in this case)
// String literal
char* stringLiteral = "Hello, World!"; // String literal (enclosed in double quotes)
// Printing the values
printf("Integer literals: decimal = %d, octal = %o, hexadecimal = %x\n", decimal, octal, hexadecimal);
printf("Floating-point literals: floatingPoint = %f, exponential = %lf\n", floatingPoint, exponential);
printf("Character literals: singleChar = %c, escapedChar = %c\n", singleChar, escapedChar);
printf("String literal: %s\n", stringLiteral);
return 0;
}
Explanation of each type:
Integer Literals: These are the most basic type of literals and can be expressed in decimal (base 10), octal (base 8, prefixed with
0
), or hexadecimal (base 16, prefixed with0x
).Floating-Point Literals: Represent real numbers and can include a decimal point. They can also be expressed in exponential form (e.g.,
1.5e2
is equivalent to1.5 * 10^2
).Character Literals: Represented by a single character enclosed in single quotes. Special characters (like newline
\n
, tab\t
) can be represented using escape sequences.String Literals: A sequence of characters enclosed in double quotes. They are actually arrays of characters ending with a null character
'\0'
.
Subscribe to my newsletter
Read articles from Jyotiprakash Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Jyotiprakash Mishra
Jyotiprakash Mishra
I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class. At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out. In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.