吾爱程序员:这里有好玩的游戏和软件
当前位置:首页C语言教程 → C中的变量和关键字

C中的变量和关键字

来源:网络 | 更新时间:2022-01-08 07:04:23
变量简单来说是具有分配给它的一些存储器的存储位置。基本上,一个变量用于存储某种形式的数据。不同类型的变量需要不同的内存量,并且有一些可以应用于它们的特定操作集。 变量声明: 典型的变量声明形式如下: 
  type variable_name;
    or for multiple variables:
  type variable1_name, variable2_name, variable3_name;
变量名可以由字母(大写和小写)、数字和下划线“_”字符组成。但是,名称不能以数字开头。 b/w 变量声明和定义的区别 变量声明是指变量在第一次使用之前首先声明或引入的部分。变量定义是为变量分配内存位置和值的部分。大多数时候,变量声明和定义是一起完成的。 请参阅以下 C 程序以获得更好的说明:  
#include <stdio.h>
int main()
{
// declaration and definition of variable 'a123'
char a123 = 'a';

// This is also both declaration and definition as 'b' is allocated
// memory and assigned some garbage value. 
float b; 

// multiple declarations and definitions
int _c, _d45, e;

// Let us print a variable
printf("%c n", a123);

return 0;
}
输出: 
a
是否可以有单独的声明和定义?  外部变量函数的情况下是可能的。 定义变量的规则
  1. 变量可以有字母、数字和下划线。
  2. 变量名可以以字母开头,并且只能下划线。它不能以数字开头。
  3. 变量名中不允许有空格。
  4. 变量名不能是任何保留字或关键字,例如 int、goto 等。
C 中的变量类型 1.局部变量 在函数或块中声明和使用的变量称为局部变量。  它的范围仅限于功能或块。不能在块外 使用。局部变量在使用前需要 初始化。  例子 - 
#include <stdio.h>
void function() {
int x = 10; // local variable
}

int main()
{
function();
}
在上面的代码中 x 只能在 function() 的范围内使用。在 main 函数中使用它会出错。 2.全局变量 在函数或块之外声明的变量称为全局变量。  它在程序开始时声明。它适用于所有功能。 
#include <stdio.h>
int x = 20;//global variable
void function1()
{
printf("%dn" , x);
}
void function2()
{
printf("%dn" , x);
}
int main() {

function1();
function2();
return 0;
}
输出 
20
20
在上面的代码中,两个函数都可以使用全局变量 x,因为我们已经可以通过所有函数访问全局变量。 3.静态变量 在多个函数调用之间保持其值的变量称为静态变量。  它是用 static 关键字声明的。  例子- 
#include <stdio.h>
void function(){ 
int x = 20;//local variable 
static int y = 30;//static variable 
x = x + 10; 
y = y + 10; 
printf("n%d,%d",x,y); 
} 
int main() {

function();
function();
function();
return 0;
}
输出
30,40
30,50
30,60
在上面的示例中,每当调用函数时,局部变量将始终打印相同的值,而静态变量将在每个函数调用中打印递增的值。 4.自动变量 C 语言中所有在块内声明的变量,默认都是自动变量。我们  可以使用 auto 关键字显式声明一个自动变量。自动变量类似于  局部变量。  例子 - 
#include <stdio.h>
void function()
{
int x=10;//local variable (also automatic) 
auto int y=20;//automatic variable
}
int main() {

function();
return 0;
}
在上面的例子中,x 和 y 都是自动变量。唯一的区别是变量 y 是用auto关键字显式声明的。 5.外部变量 外部变量可以在多个C文件之间共享。我们可以使用extern关键字声明外部变量 。 例子:
 myfile.h

  extern int x=10;//external variable (also global)  

   
  program1.c
  #include "myfile.h"  
  #include <stdio.h>  
  void printValue(){  
  printf("Global variable: %d", global_variable);  
  }
在上面的示例中,x 是一个用于多个文件的外部变量。 关键字是 C 中的特定保留字,每个保留字都有与之相关的特定特征。几乎所有帮助我们使用 C 语言功能的词都包含在关键字列表中。因此,您可以想象关键字列表不会很小! C中共有44个关键字(C89-32、C99-5、C11-7): 
auto        extern        short        while
break        float        signed        _Alignas
case        for        sizeof        _Alignof
char        goto        static        _Atomic
const        if        struct        _Bool
continue    inline        switch        _Complex
default     int        typedef        _Generic
do         long        union        _Imaginary
double         register    unsigned    _Noreturn
else         restrict    void        _Static_assert
enum         return        volatile    _Thread_local
这些关键字中的大多数已经在C 语言的各个小节中讨论过,例如数据类型、存储类、控制语句、函数等。 让我们讨论一些允许我们使用 C 的基本功能的其他关键字: const : const 可用于声明常量变量。常量变量是初始化时不能改变其值的变量。或者换句话说,分配给它们的值不能在程序中进一步修改。  语法: 
const data_type var_name = var_value;
注意:常量变量必须在声明期间进行初始化。const 关键字也与指针一起使用。 外部的: extern 只是告诉我们该变量是在别处定义的,而不是在使用它的同一块内。基本上,该值在不同的块中分配给它,并且也可以在不同的块中覆盖/更改。因此,外部变量只不过是一个用合法值初始化的全局变量,在该变量中声明它以便在其他地方使用。它可以在任何功能/块中访问。此外,通过在任何函数/块中的声明/定义之前放置“extern”关键字,也可以将普通全局变量设为外部变量。这基本上意味着我们没有初始化一个新变量,而是我们只使用/访问全局变量。使用外部变量的主要目的是可以在作为大型程序一部分的两个不同文件之间访问它们。  语法: 
extern data_type var_name = var_value;
static:static 关键字用于声明静态变量,在 C 语言编写程序时常用。静态变量具有保留其值的属性,即使它们超出了它们的范围!因此,静态变量在其作用域内保留了它们最后一次使用的值。所以我们可以说它们只被初始化一次并且一直存在到程序终止。因此,没有新的内存被分配,因为它们没有被重新声明。它们的作用域是定义它们的函数的本地范围。全局静态变量可以在该文件中的任何位置访问,因为它们的范围是文件的本地变量。默认情况下,编译器为它们分配值 0。  语法: 
static data_type var_name = var_value;
void : void 是一种特殊的数据类型。但是是什么让它如此特别?void,顾名思义,是一个空数据类型。这意味着它一无所有或没有价值。例如,当它用作函数的返回数据类型时,它只是表示该函数没有返回值。同样,当它添加到函数标题时,它表示该函数不接受任何参数。  注意: void 对指针也有重要用途。请参考C 中的void 指针来理解相同的内容。 typedef: typedef 用于为已经存在的甚至是自定义数据类型(如结构)赋予新名称。它有时会派上用场,例如,当您定义的结构的名称很长,或者您只需要一个现有数据类型的简写符号时。 让我们实现我们上面讨论过的关键字。看看下面的代码,它是一个演示这些关键字的工作示例:  

#include <stdio.h>

// declaring and initializing an extern variable
extern int x = 9;

// declaring and initializing a global variable
// simply int z; would have initialized z with
// the default value of a global variable which is 0
int z=10;

// using typedef to give a short name to long long int
// very convenient to use now due to the short name
typedef long long int LL;

// function which prints square of a no. and which has void as its
// return data type
void calSquare(int arg)
{
printf("The square of %d is %dn",arg,arg*arg);
}

// Here void means function main takes no parameters
int main(void)
{
// declaring a constant variable, its value cannot be modified
const int a = 32;

// declaring a char variable
char b = 'G';

// telling the compiler that the variable z is an extern variable
// and has been defined elsewhere (above the main function)
extern int z;

LL c = 1000000;

printf("Hello World!n");

// printing the above variables
printf("This is the value of the constant variable 'a': %dn",a);
printf("'b' is a char variable. Its value is %cn",b);
printf("'c' is a long long int variable. Its value is %lldn",c);
printf("These are the values of the extern variables 'x' and 'z'"
" respectively: %d and %dn",x,z);

// value of extern variable x modified
x=2;

// value of extern variable z modified
z=5;

// printing the modified values of extern variables 'x' and 'z'
printf("These are the modified values of the extern variables"
" 'x' and 'z' respectively: %d and %dn",x,z);

// using a static variable
printf("The value of static variable 'y' is NOT initialized to 5 after the "
"first iteration! See for yourself :)n");

while (x > 0)
{
static int y = 5;
y++;
// printing value at each iteration
printf("The value of y is %dn",y);
x--;
}

// print square of 5
calSquare(5);

printf("Bye! See you soon. :)n");

return 0;
}
输出: 
Hello World
This is the value of the constant variable 'a': 32
'b' is a char variable. Its value is G
'c' is a long long int variable. Its value is 1000000
These are the values of the extern variables 'x' and 'z' respectively: 9 and 10
These are the modified values of the extern variables 'x' and 'z' respectively: 2 and 5
The value of static variable 'y' is NOT initialized to 5 after the first iteration! See for yourself :)
The value of y is 6
The value of y is 7
The square of 5 is 25
Bye! See you soon. :)
     

最新文章

热点资讯

手游排行榜

CopyRight 2020-2030吾爱程序员

鄂ICP备2021004581号-8

本站资源收集于网络,如有侵权请联系我们:35492删除0109@qq.com