static 关键字的作用

c语言中,static既可以修饰变量(全局变量和局部变量),又可以修饰函数。static类型的变量称为静态变量,如果不初始化则编译器自动初始化为0
static的用法有两种:
1.修饰全局变量 或 函数,用于限定该变量或函数作用域为本文件。(这种限定是针对编译器而言,运行时没有区别)
2.修饰局部变量,用于修改变量的生存周期,维持持久性。

1. 局部静态变量

static修饰局部变量的时候,该变量的存储空间由栈变为静态区。
变量的作用域仍然为局部的,但是离开作用域后,静态变量不会被销毁,而是仍然驻留在内存当中,直到程序结束。
如果将静态变量的指针传出后,在作用域之外仍然可以通过指针访问这个变量。
再次调用函数的时候,这个静态变量仍然保持着上次时候的值。

2. 全局静态变量

全局静态变量也是储存在静态区,但是它的作用域仅为本文件。
对于全局变量使用static,可以多个文件中使用相同名字的全局变量而不会发生名字冲突。

3. 静态函数

在函数定义前面加上static,限定该函数只在当前文件中可见。
这样不同的文件可以使用相同名字的函数而不会发生混淆。

strcmp 函数

语法:

#include int strcmp( const char str1, const char str2 );

功能:比较字符串str1 and str2, 返回值如下:

返回值 解释
less than 0 str1 is less than str2
equal to 0 str1 is equal to str2
greater than 0 str1 is greater than str2

例如:
printf( “Enter your name: “ );
scanf( “%s”, name );
if( strcmp( name, “Mary” ) == 0 )
printf( “Hello, Dr. Mary!\n” );

编程题:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. //第五题
  5. typedef struct stu
  6. {
  7. char name[50];
  8. int chinese;
  9. int math;
  10. struct stu *next;
  11. }info;
  12. info* INIT_info()
  13. {
  14. info* head=(info *)malloc(sizeof(info));
  15. memset(head->name,0,50);
  16. head->chinese=0;
  17. head->math=0;
  18. head->next=NULL;
  19. return head;
  20. }
  21. void head_inster(info *head)
  22. {
  23. info* new=(info*)malloc(sizeof(info));
  24. new->next=head->next;
  25. printf("请输入学生的信息");
  26. scanf("%s",new->name);
  27. scanf("%d",&new->chinese);
  28. scanf("%d",&new->math);
  29. head->next=new;
  30. }
  31. void print(info* head)
  32. {
  33. while(head->next!=NULL)
  34. {
  35. head=head->next;
  36. printf("%s %d %d\n",head->name,head->chinese,head->math);
  37. }
  38. }
  39. int main()
  40. {
  41. info *head=INIT_info();
  42. int i=0;
  43. for(i=0;i<5;i++)
  44. {
  45. head_inster(head);
  46. }
  47. // head_inster(head);
  48. // head_inster(head);
  49. // head_inster(head);
  50. // head_inster(head);
  51. print(head);
  52. return 0;
  53. }
  54. #include <stdio.h>
  55. #include <string.h>
  56. //第四题
  57. #define NUM 20
  58. struct Stu
  59. {
  60. char name[50];
  61. unsigned char age;
  62. float height;
  63. };
  64. int get_stu_info( struct Stu *arr)
  65. {
  66. int i=0;
  67. for(i=0;i<NUM;i++)
  68. {
  69. printf("please input student name:");
  70. scanf("%s",arr[i].name);
  71. if(strcmp(arr[i].name,"exit")==0)
  72. {
  73. printf("输入完成\n");
  74. return i;
  75. }
  76. scanf("%d",&arr[i].age);
  77. scanf("%f",&arr[i].height);
  78. }
  79. printf("输入完成\n");
  80. return NUM;
  81. }
  82. int main()
  83. {
  84. struct Stu stu_arr[NUM];
  85. int size=get_stu_info(stu_arr);
  86. FILE *fp=fopen("d:\\songpeng.txt","w+");
  87. fwrite(stu_arr,sizeof( struct Stu),size,fp);
  88. fflush(fp);
  89. rewind(fp);
  90. memset(stu_arr,0,NUM);
  91. fread(stu_arr,sizeof( struct Stu),size,fp);
  92. int i=0;
  93. for(i=0;i<size;i++)
  94. {
  95. printf("%s--%d--%.2f\n",stu_arr[i].name,stu_arr[i].age,stu_arr[i].height);
  96. }
  97. return 0;
  98. }