c语言变量_C语言变量

c语言变量

When we want to store any information(data) on our computer/laptop, we store it in the computer's memory space. Instead of remembering the complex address of that memory space where we have stored our data, our operating system provides us with an option to create folders, name them, so that it becomes easier for us to find it and access it.

当我们要将任何信息(数据)存储在计算机/笔记本电脑上时,我们会将其存储在计算机的内存空间中。 我们的操作系统为我们提供了创建文件夹,命名文件夹的选项,而不是记住我们存储数据的那个内存空间的复杂地址,这样我们就可以更轻松地查找和访问它。

Similarly, in C language, when we want to use some data value in our program, we can store it in a memory space and name the memory space so that it becomes easier to access it.

同样,在C语言中,当我们想在程序中使用某些数据值时,可以将其存储在内存空间中并命名该内存空间,以便于访问它。

The naming of an address is known as variable. Variable is the name of memory location. Unlike constant, variables are changeable, we can change value of a variable during execution of a program. A programmer can choose a meaningful variable name. Example : average, height, age, total etc.

地址的命名称为变量 。 变量是内存位置的名称。 与常量不同,变量是可变的,我们可以在程序执行期间更改变量的值。 程序员可以选择一个有意义的变量名。 例如:平均数,身高,年龄,总数等

变量的数据类型 (Datatype of Variable)

A variable in C language must be given a type, which defines what type of data the variable will hold.

必须给C语言中的变量指定类型,该类型定义该变量将保存的数据类型。

It can be:

有可能:

  • char: Can hold/store a character in it.

    char :可以在其中保存/存储字符。

  • int: Used to hold an integer.

    int :用于保存整数。

  • float: Used to hold a float value.

    float :用于保存float值。

  • double: Used to hold a double value.

    double :用于保留double值。

  • void

    void

命名变量的规则 (Rules to name a Variable)

  1. Variable name must not start with a digit.

    变量名称不能以数字开头。

  2. Variable name can consist of alphabets, digits and special symbols like underscore _.

    变量名称可以包含字母,数字和特殊符号,例如下划线_

  3. Blank or spaces are not allowed in variable name.

    变量名称中不能有空格或空格。

  4. Keywords are not allowed as variable name.

    关键字不允许用作变量名。

  5. Upper and lower case names are treated as different, as C is case-sensitive, so it is suggested to keep the variable names in lower case.

    由于C区分大小写,因此大写字母和小写字母名称被视为不同,因此建议将变量名称保持为小写字母。

声明,定义和初始化变量 (Declaring, Defining and Initializing a variable)

Declaration of variables must be done before they are used in the program. Declaration does the following things.

必须先声明变量,然后才能在程序中使用它们。 声明执行以下操作。

  1. It tells the compiler what the variable name is.

    它告诉编译器变量名是什么。

  2. It specifies what type of data the variable will hold.

    它指定变量将保存的数据类型。

  3. Until the variable is defined the compiler doesn't have to worry about allocating memory space to the variable.

    在定义变量之前,编译器不必担心为变量分配内存空间。

  4. Declaration is more like informing the compiler that there exist a variable with following datatype which is used in the program.

    声明更像是通知编译器存在一个变量,该变量具有在程序中使用的以下数据类型。

  5. A variable is declared using the extern keyword, outside the main() function.

    main()函数外部使用extern关键字声明变量。

extern int a;
extern float b;
extern double c, d;

Defining a variable means the compiler has to now assign a storage to the variable because it will be used in the program. It is not necessary to declare a variable using extern keyword, if you want to use it in your program. You can directly define a variable inside the main() function and use it.

定义变量意味着编译器现在必须为该变量分配存储,因为它将在程序中使用。 如果要在程序中使用变量,则不必使用extern关键字声明变量。 您可以直接在main()函数中定义一个变量并使用它。

To define a function we must provide the datatype and the variable name. We can even define multiple variables of same datatype in a single line by using comma to separate them.

要定义一个函数,我们必须提供数据类型和变量名。 我们甚至可以在一行中使用逗号分隔来定义同一数据类型的多个变量。

int a;
float b, c;

Initializing a variable means to provide it with a value. A variable can be initialized and defined in a single statement, like:

初始化变量意味着为其提供值。 可以在单个语句中初始化和定义变量,例如:

int a = 10;

Let's write a program in which we will use some variables.

让我们编写一个程序,在其中使用一些变量。

#include <stdio.h>

// Variable declaration(optional)
extern int a, b;
extern int c;

int main () {

    /* variable definition: */
    int a, b;
 
    /* actual initialization */
    a = 7;
    b = 14;
    /* using addition operator */
    c = a + b;
    /* display the result */
    printf("Sum is : %d \n", c);
    
    return 0;
}

Sum is : 21

总和是:21

You must be thinking how does this printf() works, right? Do not worry, we will learn about it along with other ways to input and output datain C language in the next tutorial.

您必须考虑一下printf()工作原理,对吗? 不用担心,我们将在下一教程中学习有关C语言输入和输出数据的其他方式。

变量和标识符之间的区别? (Difference between Variable and Identifier?)

An Identifier is a name given to any variable, function, structure, pointer or any other entity in a programming language. While a variable, as we have just learned in this tutorial is a named memory location to store data which is used in the program.

标识符是给编程语言中的任何变量,函数,结构,指针或任何其他实体的名称。 正如我们在本教程中刚刚学到的那样,变量是一个命名的存储位置,用于存储程序中使用的数据。

IdentifierVariable
Identifier is the name given to a variable, function etc.While, variable is used to name a memory location which stores data.
An identifier can be a variable, but not all indentifiers are variables.All variable names are identifiers.
Example:
// a variable
int studytonight;
// or, a function
int studytonight() { 
    .. 
}
Example:
识别码 变量
标识符是给变量,函数等的名称。 While变量用于命名存储数据的存储位置。
标识符可以是变量,但并非所有标识符都是变量。 所有变量名称都是标识符。
例:
// a variable
int studytonight;
// or, a function
int studytonight() { 
    .. 
}
例:

Another great analogy to understand the difference between Identifier and Variable is:

了解标识符和变量之间区别的另一个很好的类比是:

You can think of an Identifier int x to be a variable's name, but it can also be a function's name int x() { } and still be an identifier.

您可以将标识符int x视为变量的名称,但也可以将其作为函数的名称int x() { }仍然是标识符。

Just like Obama is a name of a person, but also the name of a foundation.

就像奥巴马是一个人的名字,也是基金会的名字一样。

翻译自: https://www.studytonight.com/c/variables-in-c.php

c语言变量

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值