If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In C, a literal is a value that is written directly in the source code of a program. Literals are used to initialize variables and constants, and to specify values in expressions. C supports several types of literals, including:
Integer literals: These are whole numbers, such as 42 or -5. They can be written in decimal, octal (by prefixing with 0), or hexadecimal (by prefixing with 0x) notation.
Floating-point literals: These are numbers with decimal places, such as 3.14 or -2.718. They can be written in decimal notation or in scientific notation using the letter 'e' or 'E' to represent the exponent.
Character literals: These are single characters, such as 'A' or '$'. They are written in single quotes.
String literals: These are sequences of characters, such as "Hello" or "Goodbye". They are written in double quotes.
Boolean literals: These are the values true and false.
Enumeration literals: These are used to assign names to integer values in an enumeration.
It's important to note that literals are constants and their value cannot be changed once it's assigned. Also, when a string literal is assigned to a character array, it's necessary to add a null character at the end of the string to mark the end of the string.
Copy code
char str[6] = "Hello"; // It requires an extra element to store the null character
Literals provide a convenient way to specify values in C programs, and their values are known at compile time. This makes them useful for initializing variables, constants, and arrays, and for specifying values in expressions and control structures.
Comments: 0