0115(C language)

本文详细解析了C语言中结构体的定义与使用方式,包括typedef与结构体结合的应用。此外,还介绍了如何使用fflush清理输入缓冲区,避免因连续输入造成的程序流程错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

1.about  struct:

typedef与结构结合使用
typedef struct tagMyStruct
{
 int iNum;
 long lLength;
} MyStruct;
  这语句实际上完成两个操作:

  1) 定义一个新的结构类型
struct tagMyStruct
{
 int iNum;
 long lLength;
};
  分析:tagMyStruct称为“tag”,即标签,实际上是一个临时名字,struct 关键字和tagMyStruct一起,构成了这个结构类型,不论是否有typedef,这个结构都存在。
  我们可以用struct tagMyStruct varName来定义变量,但要注意,使用tagMyStruct varName来定义变量是不对的,因为struct tagMyStruct合在一起才能表示一个结构类型。
  2) typedef为这个新的结构起了一个名字,叫MyStruct
typedef struct tagMyStruct MyStruct;
  因此,MyStruct实际上相当于struct tagMyStruct,我们可以使用MyStruct varName来定义变量。
2.fflush:

Example
/* FFLUSH.C */

#include <stdio.h>
#include <conio.h>

void main( void )
{
   int integer;
   char string[81];

   /* Read each word as a string. */
   printf( "Enter a sentence of four words with scanf: " );
   for( integer = 0; integer < 4; integer++ )
   {
      scanf( "%s", string );//这里scanf 以空格分开
      printf( "%s/n", string );
   }

   /* You must flush the input buffer before using gets. */
   fflush( stdin );//这里是为了清除空格的影响
   printf( "Enter the same sentence with gets: " );
   gets( string );
   printf( "%s/n", string );
}
Output
Enter a sentence of four words with scanf: This is a test
This
is
a
test
Enter the same sentence with gets: This is a test
This is a test
//

 引用一段说明

在C语言中,如果使用字符型变量(就是char型)时在有连续输入的情况下,很容易因为出现垃圾字符而导致程序的流程非法。
看下面,简单的一段代码:
#include "stdio.h"
main()
{
char a,b;
printf("input a:");
scanf("%c",&a); /*或a=getchar();*/
printf("a=%c/n",a);
printf("input b:");
scanf("%c",&b); /*或b=getchar();*/
printf("b=%c/n",b);
}
这段代码确实挺简单的,但是却隐藏着很难发现的问题。当在执行了printf("input a:");这句后要求我们做输入的时候,随便输入一个数,然后按回车,程序继续往下执行。没等我们做第二次输入的时候程序就已经结束了。把scanf()换成getchar()也一样存在问题。
为什么会有这样的问题呢?那是因为在我们做了第一次输入时,并按下回车,使程序继续执行。而那个回车也随之进入了流当中。而遇到第二个scanf()时,scanf()把回车当作是第二次输入的字符而接收。因此程序也就忽略了输入而继续执行了。这个回车就是在做输入时产生的垃圾字符了。我第一次遇到它时,是我在写循环时遇到的,由于垃圾字符的原因,使得循环提早结束,而破坏了我预期的流程(第一次见垃圾字符时郁闷死我了)。那怎么才能防止垃圾字符的破坏,或是避免它的呢?而我的解决方法只有两种(有其他种的话,一定要告我)。
第一种方法是多定义一个字符变量,如char c;这样。把变量c分别放到输入后面。如:
a=getchar();
c=getchar();
……
b=getchar();
c=getchar();
这样可以使垃圾字符自动进入变量c中,而不破坏我们正确的流程和输入。如果是scanf()的话,可以按照下面的方式来写:scanf(”%c%c”,&a,&c);这样的效果跟使用getchar()是一样的。
这样的方法无疑是浪费了一个内存空间,而且增加了很多的冗余代码。
第二种方法是使用getche()函数,而不使用scanf()和getchar()。这个函数也是接收字符的,但是它不等待回车的输入!而直接把字符送入流中,这样就可以避免垃圾字符的出现了。

其实还有一种方法是:在接收第一个输入后: fflush(stdin); 像本例 这样。

 

关于fflush:

Example

/* FFLUSH.C */

#include <stdio.h>
#include <conio.h>

void main( void )
{
   int integer;
   char string[81];

   /* Read each word as a string. */
   printf( "Enter a sentence of four words with scanf: " );
   for( integer = 0; integer < 4; integer++ )
   {
      scanf( "%s", string );
      printf( "%s/n", string );
   }

   /* You must flush the input buffer before using gets. */
   fflush( stdin );
   printf( "Enter the same sentence with gets: " );
   gets( string );
   printf( "%s/n", string );
}

Output

Enter a sentence of four words with scanf: This is a test
This
is
a
test
Enter the same sentence with gets: This is a test
This is a test

for( integer = 0; integer < 4; integer++ )
 {
    scanf( "%s", string );
    printf( "%s/n", string );
 }
 scanf( "%s", string); // 以 空格或者/t字符分界,并且遇到/n停止 /n留在输入流中
 this is the piggip/n  分别需要循环四次读入字符并且/n留在输入流中
/* You must flush the input buffer before using gets. */
fflush( stdin );
fflush( stdin ); 清空输入流,因为此时输入流有一个/n 或者其他字符,比如在上次
提示中用户输入了四个以上的token ,such as:this is the piggip's post/n
那么那个循环中只读入四个token,输入流中还有post/n 字符,所以为了下一步的正确
字符串读入,必须清空输入流。
printf( "Enter the same sentence with gets: " );
gets( string );

gets( string ); 是从输入流中读入字符串,并且默认遇到/n 等才结束,此时/n从输入流
中读入,但是不写入到string中,而是以/0 取而代之写入string中。
printf( "%s/n", string ); // 这个就很容易理解了吧

对了参数名字不要用string,因为c++ 标准库中有一个string 类,取变量名为string 很
容易让人误会的。以上的string 是 char string[size];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值