Variables and Types 变量和类型

本文介绍了C语言中的各种数据类型,包括整型、无符号整型、浮点数等,并通过实例演示了如何使用这些类型进行数学运算。此外,还简要提及了结构体的概念,以及C语言中字符串的表示方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 


Data types   数据类型

C has several types of variables, but there are a few(一些) basic types:

  • Integers (整型)- whole numbers which can be either positive(正数) or negative(负数)(要么...要么). Defined using charintshortlong or long long.

  • Unsigned integers(无符号整型) - whole numbers which can only be positive. Defined  using unsigned char,  unsigned int,   unsigned shortunsigned long or unsigned long long.

  • Floating point numbers(浮点数) - real numbers实数 (numbers with fractions). Defined using float and double.

  • Structures(结构体) - will be explained later, in the Structures section.(稍后将在“结构”一节中解释。)
     

The different types of variables define their bounds(界限). A char can range(范围) only from -128 to 127, whereas a long can range from -2,147,483,648 to 2,147,483,647 (long and other numeric data types(数字数据类型) may have another range on different computers, for example - from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 on 64-bit computer).

Note that C does not have a boolean type(布尔类型). Usually, it is defined using the following notation(符号):

#define BOOL char
#define FALSE 0
#define TRUE 1

C uses arrays of characters(字符数组) to define strings(字符串), and will be explained in the Strings section.


Defining variables  定义变量

For numbers(数字), we will usually use the type int, which an integer in the size of a "word"(字)the default number size of the machine which your program is compiled on. On most computers today, it is a 32-bit number, which means the number can range from -2,147,483,648 to 2,147,483,647.

To define the variables foo and bar, we need to use the following syntax(语法):

int foo;
int bar = 1;

The variable foo can be used, but since we did not initialize(初始化) it, we don't know what's in it. The variable bar contains(包含) the number 1.

Now, we can do some math. Assuming(假设) abcd, and e are variables, we can simply use plus, minus and multiplication(乘法) operators in the following notation, and assign(分配,赋值) a new value to a:

int a = 0, b = 1, c = 2, d = 3, e = 4;
a = b - c + d * e;
printf("%d", a); /* will print 1-2+3*4 = 11 */


Exercise

In the next exercise, you will need to create a program which prints out the sum of the numbers ab, and c.

原:

#include <stdio.h>

int main() {
  int a = 3;
  float b = 4.5;
  double c = 5.25;
  float sum;

  /* Your code goes here */

  printf("The sum of a, b, and c is %f.", sum);
  return 0;
}

 

改:

#include <stdio.h>

int main() {
  int a = 3;
  float b = 4.5;
  double c = 5.25;
  float sum;

  /* Your code goes here */
    sum = a + b + c ;

  printf("The sum of a, b, and c is %f.", sum);
  return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yangbocsu

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值