现在的位置: 首页 > 综合 > 正文

C学习点滴

2013年10月09日 ⁄ 综合 ⁄ 共 2518字 ⁄ 字号 评论关闭

1)

 

 在给数组初始化时这种做法是不正确的:

int a[3];

a[3] = {1, 2, 3};

-----------------------------

也不正确:

struct{

int a[3];

}stu;

 

stu.a[3] = {1, 2, 3};

 

正确的:

int a[3] = {1, 2, 3};

 

-----------------------------------

 

(2)C语言规定返回值为整型的函数在调用之前可以不必声明

 如:

#include <stdio.h>

main()                      //在main函数前没有声明swap 函数
{
 int x=3,y=4;
 int *p1,*p2;
 p1=&x;
 p2=&y;
 swap(p1, p2);
 printf("%d/n%d/n", x,y);
}

int swap(int *p1, int *p2)
{
 int z;
 z=*p1;
 *p1=*p2;
 *p2=z;
}
输出:

4

 

 

----------------------------------

 

(3)struct 结构体小结

 

struct student{

                int num;

                char name[100];

                int score[3];

}stu[5], stu1;

 

赋值只能在初始化时赋值

如:

struct student{

                int num;

                char name[100];

                int score[3];

}stu1 = {1, "gu", 99, 99, 98};      //数组也一样       //字符数组加引号

 

 

不可以这样赋值:

struct student{

                int num;

                char name[100];

                int score[3];

}stu1;     

stu1 = {1, "gu", 99, 99, 98}; 

 

不可以这样整体引用数组:

for(i=0; i<5; i++)

{

      scanf("%s/n", &stu[i]);

      printf("/n");

}

 

也不可以这样:

for(i=0; i<5; i++)

{

      printf("%s/n", stu[i]);

}

 

=======================

但可以这样:

struct student
{
        int number;
        char name[10];
        int score[3];
        int average;
}stu[5], stu1;

 

 for(i=0; i<4; i++)
  for(j=0; j<5-i; j++)
  {
   if(stu[j].average > stu[j+1].average)         //结构体不可以整体比较,如:不可以这样:  if(stu[j]>stu[j+1])

 

                                                                         //只能用里面的成员比较,如:stu[j].average > stu[j+1].average
   {
    stu1 = stu[j];
    stu[j] = stu[j+1];
    stu[j+1] = stu1;                         //可以这样用,结构体之间可以互相赋值
   }
  }

 

 for(i=0; i<5; i++)
 {
  if(fread(&stu[i], sizeof(struct student), 1, fp1) != 1)
  {
   printf("error !/n");
   exit(0);
  }
 }

 

============================================

(4)break的一些注意点

 

#include <stdio.h>

#include <stdio.h>

main()
{
 int i;
 while(1)
 {
  printf("input i:/n");
  scanf("%d", &i);
  switch(i)
  {
   case 1:printf("/nguwansheng1/n");break;
   case 2:printf("/nguwansheng2/n");break;
   case 3:printf("/nguwansheng3/n");break;
   case 4:printf("/nguwansheng4/n");break;
   default :printf("error/n"); 
  }
 }
}

输入:

1

输出:

guwansheng1

输入8

输出:

error

 

输入1后输出guwansheng1后跳出switch但没有跳出while,只跳出内层的那个(switch),跳出后会继续提示:input i:

 

#include <stdio.h>

main()
{
 int i;
 while(1)
 {
  printf("input i:/n");
  scanf("%d", &i);
  switch(i)
  {
   case 1:printf("/nguwansheng1/n");break;
   case 2:printf("/nguwansheng2/n");break;
   case 3:printf("/nguwansheng3/n");break;
   case 4:printf("/nguwansheng4/n");break;
   default :printf("error/n"); 
  }
  printf("i love my mother!/n");
  if(i==1)
  {
   break;                                       //这个break使整个程序结束
  }
 }
}
输入:1

输出:

guwansheng1

i love my mother!

 

跳出while,整个程序结束了,下面没有提示

 

 

抱歉!评论已关闭.