Home > Backend Development > C#.Net Tutorial > How to express numerical constants in C language

How to express numerical constants in C language

下次还敢
Release: 2024-04-29 17:36:15
Original
1209 people have browsed it

In C language, numeric constants can be represented as integers, floating point and character constants. Integer constants can be represented in decimal, octal, or hexadecimal, and can be signed or unsigned; floating-point constants can be represented in decimal or scientific notation; character constants are represented by a single character enclosed in single quotes.

How to express numerical constants in C language

Representation of numerical constants in C language

In C language, numerical constants can be represented as integers Constants, floating point constants and character constants.

Integer constants

Integer constants can be represented as signed or unsigned integers, and can be represented using decimal, octal, or hexadecimal notation. Signed integers use a minus sign (-) to represent negative values, while unsigned integers have no sign.

<code class="c">// 无符号十进制整数
unsigned int num1 = 100;

// 有符号十进制整数
int num2 = -50;

// 八进制整数
int num3 = 0777;

// 十六进制整数
int num4 = 0xABCD;</code>
Copy after login

Floating point constants

Floating point constants are expressed as decimals or scientific notation. Decimal format consists of an integer part and a decimal part, separated by a decimal point (.). Scientific notation consists of a number multiplied by a power of 10.

<code class="c">// 小数浮点常量
float num1 = 3.14;

// 科学记数法浮点常量
double num2 = 6.022e23;</code>
Copy after login

Character constants

Character constants are represented as a single character, enclosed in single quotes (').

<code class="c">// 字符常量
char ch = 'a';</code>
Copy after login

Note:

  • The default type of integer constant is int, but you can use suffix modifiers to specify other types, such as long, unsigned, short.
  • The default type of floating-point constants is double, but float can be specified using suffix modifiers.
  • Character constants are actually integers, representing the ASCII code value of the character.

The above is the detailed content of How to express numerical constants in C language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template