【C语言】关于 static和const的用法

1. 对于const:

难点:const char* 和char* const类型的区别

在C语言中,const char*和char* const都是指向字符型数据的指针类型,但它们的含义不同。
const char*是指向字符常量的指针,也就是说指针所指向的字符数据是不可修改的。例如:
const char* str = "Hello";  str是指向字符常量的指针,指向的字符数据不可修改

在上面的例子中,指针str指向一个字符串常量,而这个字符串常量是不可修改的,因为它被声明为const char* 类型。

char* const是指向字符数组的指针,也就是说指针本身是不可修改的,但指针所指向的字符数据是可修改的。例如:

char array[] = "Hello";
char* const ptr = array;  
ptr是指向字符数组的指针,指针本身不可修改,但指向的字符数据可修改

在上面的例子中,指针ptr指向一个字符数组array,这个指针本身是不可修改的,但是我们可以通过ptr指针来修改array数组中的字符数据。

总结一下,const char* 是指向字符常量的指针,char* const是指向字符数组的指针,它们的区别在于指针本身是否可修改。


2. 对于static

C语言中,static是一个关键字,用于修饰变量、函数和代码块(称为静态变量、静态函数和静态块)。

静态变量:在函数内定义静态变量的关键字是static它的作用是在函数执行完后仍然保留其值,不被释放。在定义时需要加上数据类型和变量名,例如:

void codevoidfunc()
{
    staticint num = 0;
    num++;
    printf("num: %d\n", num);
}

静态函数:在函数定义时加上static关键字,它的作用是使该函数只在当前文件中可见,不能被其他文件调用。在定义时需要加上返回值类型、函数名和参数列表,例如:

stati int add(int a, int b)
{
    return a + b;
}

其主要作用是限制这些元素的作用域,使它们只在定义它们的文件中可见,无法在其他源文件中使用这个静态函数。

具体来说,对于静态变量和静态函数,它们 只能在定义它们的代码文件中使用,而不能被其他代码文件访问。这种限制有助于避免名称冲突和潜在的命名空间问题。对于静态块,它只能在定义它的函数中调用,而不能在其他函数中使用。
在使用static修饰变量时,还可以创建静态局部变量。这些变量与常规局部变量的区别在于它们在函数被调用时不会被重新初始化,而是保留它们的值。这可以用于实现一些有趣的功能,如统计函数被调用的次数等。


3. const和static其实没有直接的区别

const是一个类型限定符,用于指定变量是只读的,即它们的值不能被修改。const变量通常用于表示程序中的常量值,如π的值或常规参数的默认值。const可以应用于任何类型的变量,包括基本数据类型、指针和自定义类型。

尽管static和const都可以用于修饰变量,但它们的目的不同。static的目的是限制变量的作用域,而const的目的是确保变量的值不会被修改。因此,static和const之间没有直接的区别,它们可以用于同一个变量,以实现限制作用域和保护变量值的效果。

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
Static Testing and Dynamic Testing are two methods of software testing. Here is a comparison of the two: 1. Definition: Static Testing is a testing technique to check the code for defects without executing it. Dynamic Testing is a testing technique to check the code for defects by executing it. 2. Purpose: Static Testing is used to find defects in the code early in the development cycle. Dynamic Testing is used to find defects in the code during the execution phase. 3. Types: Static Testing includes techniques like code reviews, walkthroughs, and inspections. Dynamic Testing includes techniques like unit testing, integration testing, system testing, and acceptance testing. 4. Execution: Static Testing is performed manually or with the help of tools. Dynamic Testing is performed using automated testing tools. 5. Time: Static Testing is performed before the code is compiled or executed. Dynamic Testing is performed after the code is compiled or executed. 6. Cost: Static Testing is less expensive than Dynamic Testing as it does not require the execution of code. Dynamic Testing is more expensive than Static Testing as it requires the execution of code. 7. Coverage: Static Testing can cover the entire code base. Dynamic Testing can only cover the code that is executed during testing. In summary, Static Testing is a cost-effective way to find defects early in the development cycle, while Dynamic Testing is used to find defects during the execution phase. Both techniques are important for ensuring high software quality.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

_宁清

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

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

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

打赏作者

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

抵扣说明:

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

余额充值