C Contants

Updated:2019-09-15
Share:

In programming Constants means values that never changes. Constants generally of the simple data types like an integers, floats, chars, strings, or enums. Constants act like any variables except trying to change Constant valuei is a compile-time error, in other words your program cannot be compiled.

Integer Constants

An integer literal can be a decimal, octal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal. An integer literal can also have a suffix that is a combination of U and L, for unsigned and long, respectively. The suffix can be uppercase or lowercase and can be in any order.

integer examples

101
102u /* unsigned integer */
1l /* long */
107ul /* unsigned long */
0xCode /* hexadecimal */
0755 /* octal */

Float Constants

float literal has an integer part, a decimal point, a fractional part, and an exponent part. You can represent floating point literals either in decimal form or exponential form. While representing decimal form, you must include the decimal point, the exponent, or both; and while representing exponential form, you must include the integer part, the fractional part, or both.
The signed exponent is introduced by e or E. Here are some examples of float

 
1.191 /* float without E */ 
1.9E-7L /* float with E */ 

Character Constants

Character literals are enclosed in single quotes,, char can contain only one character, be it alphanumeric, whitespace letters ( e.g '\t', '\n'), or a unicode character.

#include <stdio.h> 

int main() { 
              printf("CoderMe.com\n"); 
              return 0; 
}

When you compile the code and execute it, it prints the following:

CoderMe.com

String constants are enclosed in double quotes "" NOT single quotes. A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters.
You better break a long line into multiple lines as this improve readability, you can do that by separating them using any white space characters.
Look at these examples of string. All of them are equivalent.

"coderme.com" 
"coderme"     ".com" 

Defining Constants

You can define Constants using any of the two simple ways in C. In either way the convention is to make all constants names UPPERCASE.

  • Using #define preprocessor, for example
    #define HOST = "CODERME.COM"
    #define VISITS = 1000
    
  • Using const keyword, for example:
    const int VISITS = 1000;
    

Read also

Python-Loops

Python interpreter typically runs code sequentially first statement then the second .. from top to bottom, one alternative way of running code, is executing the same statements repeatedly, this term is known in programming as a loop not Lupe :) Python loop types Python supports two type of loop for and while loops while loop which is a simple loop used mainly for infinite loops aka loops that doesn't need to stop, like listening for incoming connection on a server socket w...

Debian Router - Setup

In this articles series you will learn how to setup Linux (Debian) as a Router for home/business local networks, to spice things I'll show how to add dns server, proxy/web caching server, and basic web traffic filter to our cute debian box.   In this series I'll go over: Introduction . Hardware Requirements . Software installation . Basic Setup . /etc/network/Interfaces Configuration . Unbound dns server setup . DHCP server setup . Squid setup. Final step i...