C language constants and sample code _c language

Source: Internet
Author: User
Tags constant

C Constants

Constants are fixed values that do not change during program execution. These fixed values, also known as literal quantities.

Constants can be any of the basic data types, such as integer constants, floating-point constants, character constants, or string literals, and enumerated constants.

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

Integer constants

Integer constants can be decimal, octal, or hexadecimal constants. prefix specifies cardinality: 0x or 0X is hexadecimal, 0 is octal, and decimal is by default without a prefix.

Integer constants can also take a suffix, which is a combination of u and L, u represents an unsigned integer (unsigned), and L represents a long integer. Suffixes can be uppercase or lowercase, and the order of U and L is arbitrary.

Examples of several integer constants are listed below:

    1. 212/* Legal * *
    2. 215u/* Legal * *
    3. 0xFeeL/* Legal * *
    4. 078/* Illegal: 8 is not a octal number * *
    5. 032UU/* Illegal: cannot repeat suffix * *

The following are instances of various types of integer constants:

    1. 85/* Decimal * *
    2. 0213/* Octal * *
    3. 0X4B/* Hex *
    4. 30/* Integer * *
    5. 30u/* unsigned integer * *
    6. 30l/* Long integer * *
    7. 30ul/* unsigned long integer * *

Floating-point constants

A floating-point constant consists of an integer part, a decimal point, a fractional part, and an exponent. You can use decimal form or exponential form to represent floating-point constants.
When you use a decimal representation, you must include a decimal point, an exponent, or both. When used in exponential notation, you must include either an integer part, a decimal part, or both. The signed exponent is introduced by E or E.

Several instances of floating-point constants are listed below:

    1. 3.14159/* Legal * *
    2. 314159E-5L/* Legal * *
    3. 510E/* Illegal: Incomplete index * *
    4. 210f/* Illegal: No decimal or Index * *
    5. . E55/* Illegal: missing integer or Score * *

Word Constants Quantity

The amount of constants is enclosed in single quotes, for example, ' x ' can be stored in a simple variable of type char.
The constants amount can be an ordinary character (such as ' X '), an escape sequence (such as ' \ t '), or a generic character (such as ' \u02c0 ').
In C, there are certain characters that, when preceded by a backslash, have special meanings that are used to denote a newline character (\ n) or a tab character (\ t). The following table lists some of these escape sequence codes:

Escape sequence meaning
\\ \ character
\' ' Character
\" "Character
\? ? Character
\a Alarm Ring
\b Backspace key
\f Page breaks
\ n Line feed
\ r Enter
\ t Horizontal tab
\v Vertical tab
\ooo Number of octal in one to three digits
\xhh ... Hexadecimal number of one or more digits

The following example shows some escape sequence characters:

#include <stdio.h>

int main ()
{
  printf ("hello\tworld\n\n");

  return 0;
}

When the above code is compiled and executed, it produces the following results:

Hello World

String constants

String literals or constants are enclosed in double quotes "". A string contains characters similar to the character constants: Ordinary characters, escape sequences, and generic characters.
You can use a space as a separator to branch a long string constant.

The following example shows some string constants. The following three forms show the same string.

"Hello, dear" "Hello,

\

Dear"

"Hello," "D" "ear"

Defining constants

In C, there are two simple ways to define constants:

1. Use the #define preprocessor.

2. Use the const keyword.

#define Preprocessor

The following is the form of defining constants using the #define preprocessor:

#define Identifier Value

Take a look at the following example:

#include <stdio.h>

#define LENGTH  
#define WIDTH 5
#define newline ' \ n '

int main ()
{

  int area; 
 
  Area = LENGTH * WIDTH;
  printf ("Value of area:%d", area);
  printf ("%c", newline);

  return 0;
}

When the above code is compiled and executed, it produces the following results:

Value of AREA:50

const keyword

You can declare constants of the specified type using the const prefix, as follows:

Const type variable = value;

Take a look at the following example:

#include <stdio.h>

int main ()
{
  const int LENGTH = ten;
  const int WIDTH = 5;
  const char newline = ' \ n ';
  int area; 
  
  Area = LENGTH * WIDTH;
  printf ("Value of area:%d", area);
  printf ("%c", newline);

  return 0;
}

When the above code is compiled and executed, it produces the following results:

Value of AREA:50

Note that it is a good programming practice to define constants as uppercase letters.

The above is the C language constant data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.