C Tutorial -- Constant

Constant? Easy! Let’s talk about it!

C


Constants are fixed values that do not change during program execution.

​ Constants can be any basic data type, such as integer constants, floating-point constants, character constants, or string literal values, as well as enumeration constants.

Constants are like regular variables, except that the values of constants cannot be modified after they are defined.


Integer Constant

​ An integer constant can be a decimal, octal, or hexadecimal constant. The prefix specifies a base: 0x or 0X for hex, 0 for octal, and a decimal by default without prefix.

FOR INSTANCE:

85
0213   
0x4b
30
30u
30l
30ul

An integer constant can also have a suffix, which is a combination of U and L, which represents an unsigned integer (unsigned), and L, which represents a long integer. The suffix can be capital, or it can be small, and the order of U and L is arbitrary.

Floating-point Constant

​ When expressed in the form of a number of integers, you must include an integer part, a small part, or both. When expressed in exponential form, must contain a number of points, indexes, or both. The signed exponent is introduced with e or E.

Here are a few examples of floating-point constants:

3.14159
314159E-5L

The character Constant

​ Character constants are enclosed in single quotes, for example, ‘Hello World’ can be stored in simple variables of the char type.

​ The character constant can be a normal character (such as ‘Hello World’), an escape sequence (such as ‘\t’), or a generic character (such as ‘u02C0’).

​ In C, there are specific characters that have a special meaning when they have backslashes in front of them, and are used to represent things like line breaks (\n) or tabs (\t).

There’s many escape sequence like…

\n line breaks

\t tabs

\b backspace

\a alarm (eh…)

\ \ \

\? ?

\ ‘ ‘

\ “ “

Example:


#include <stdio.h>
int main() 
{ 
printf("Hello\tWorld\n\n"); 
return 0; 
}

result:

Hello	World			/*a TAB between "Hello" and "World"*/
    

String Constant

​ String literal values or constants are enclosed in double quotes (“…”). A string contains characters that resemble character constants: ordinary characters, escape sequences, and generic characters.

You can use spaces as separators to branch a long string constant.

The following example shows some string constants. The strings shown in the following three forms are the same.

"hello, world"

"hello, \
world"

"hello, " "w" "orld"

Define the constant

So, how to define a constant?

​ In C, there are two simple ways to define a constant:

  • Use #define preprocessor.

    #define identifier value
    

    Example:

    #include <stdio.h>
      
    #define LENGTH 10 
    #define WIDTH 5 
    #define NEWLINE '\n' 
      
    int main() 
    { 
    	int area; 
    	area = LENGTH * WIDTH;
        printf("value of area : %d", area);
        printf("%c", NEWLINE);
        return 0;
    }
    

    result:

    value of area : 50
      
    
  • Use the const keyword.

    const type variable = value;
    

    Example:

    #include <stdio.h>
      
    int main()
    { 
    	const int LENGTH = 10;
    	const int WIDTH = 5;
    	const char NEWLINE = '\n';
    	int area;
      area = LENGTH * WIDTH;
    	printf("value of area : %d", area);
    	printf("%c", NEWLINE);
    	return 0;
      }
    

    result:

          value of area : 50
    	
    

    It’s a good habit to define a constant all in capital letters.


© 2020-2021. All rights reserved.