Saturday, June 30, 2018

C programming Constants and literals



Constants and literals in C programming language is topic we discussed in detail. Here, you will know difference in constants and literals in C. You can also check literals and constants in C with examples.  But you should also practice examples for C constants. Moreover, there is a link to Variables in C as well.
Therefore, it is better if you have idea about Data types in C. As this will help you to understand constants in C language.

What are constants and literals in C programming language?

In C programs, constants mean fixed values. In addition, programs will not change during execution. Moreover, these values are literals only. These constants may be of basic types. Moreover, constants are similar to variables. But the only difference is that you cannot modify these values. This rule applies after defining them.

Integer literals in C programming language

Integer literal may be octal, decimal or even hexadecimal constant. Moreover, prefix will indicate base or even radix. 0X or 0x is for hexadecimal. 0 is for octal and there is nothing for decimal. There is also suffix for integer literal. And it is combination of L and U. Here, L stands for long and U is for unsigned. Moreover, suffix can either be in lower or uppercase.
Program example

Character constants in C programs

Single quotes enclose character literals. You can store them in simple variable of char type. In addition, these can be plain characters or escape sequence. Even universal characters are of this type. However, some characters in C have special meaning. This is especially when there is backslash in front of them.
Example program

Floating point literals in C programs

Even floating point literal will have integer part. In fact there is decimal point, exponent and fractional part as well. You may represent floating point literals in C in exponential or decimal form. Finally, e or E introduces signed exponent.
Program example

What are string literals in C?

Double quotes enclose string literals or constants. In addition, string has characters. And these characters are similar to the character literals. These similarities are escape sequences, plain characters and the universal characters. You may even break long lines in to many lines. You can do this by using string literals.  However, separating them with White spaces in C is also possible.
Program example

How to define constants

You can use #define pre-processor or const keyword to define constants. You can check below examples to know how to do this.
Example programs.
Thus, you now know constants in C language with examples. Even literals in C with examples are clear. However, do not forget to practice constant string literals in C programming language examples.
Constants and literals concept will help you in further chapters. Therefore, start practicing exercises here. Because it will be helpful in loops, functions in C programs.




No comments:

Post a Comment