关于C语法的几个问题 [英] Few questions about C syntax

查看:96
本文介绍了关于C语法的几个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C语法的几个问题。


  1. CH =(字符*)malloc的(的sizeof(char)的* strlen的(SRC));
    什么是第一个括号表示(字符*)?


  2. C =残培();

    开关(C){

    情况下,1:{

    我的老师问为什么'这种类型的'使用了引号,而不是双。我说,这是C语法,如果使用字符变量。但他说NO!为什么使用单引号,而不是增加一倍?


  3. 当使用 scanf函数什么也没有发生,它已被用于两次得到的东西扫描

    有时。这是什么问题的原因是什么?
    例如:

     的printf(请输入字符串\\ n);
    scanf函数(%S,&安培; STR);
    的printf(请输入字符\\ n);
    scanf函数(%C,&安培; CH); //不扫描我的字符
    scanf函数(%C,&安培; CH); //这个二线做扫描我的字符



解决方案

其他人已经回答正确侑前两个问题,所以我会回答你的第三个问题:

当你输入一个字符键,按下回车键,两个字符放入输入缓冲区,字符换行字符。

您需要考虑到这两种。所以首先 scanf函数消耗换行符,另一个在读取字符。

按步骤code分析步骤:

 的printf(请输入字符串\\ n);
scanf函数(%S,&安培; STR);

通过以​​上两种说法,你看输入字符串和程序等待您的输入。
让我们假设你输入的字符 C 和pressed输入一次。当您执行此操作时,输入缓冲区接收两个字符:


  1. 字符 C 您输入&功放;

  2. 换行符 \\ n

scanf函数语句读取一个字符( C )从输入buffer.Thus,在换行字符保留在Inuput缓冲读。

 的printf(请输入字符\\ n);
scanf函数(%C,&安培; CH); //不扫描我的字符

通过以​​上两种说法,输入字符被显示,但在 scanf函数只是跳过(不等待任何UER输入),这是因为在输入缓冲区中的未读换行字符由该读 scanf函数

所以要适当地接收你所需要的一个输入字符的额外 scanf函数

  scanf函数(%C,&安培; CH); //这个二线做扫描我的字符

I have a few questions about C syntax.

  1. ch = (char *) malloc( sizeof( char ) * strlen(src) ); What do the first brackets mean (char *) ?

  2. c=getch();

    switch(c) {

    case '1' :{

    My teacher asked why 'this type' quotation marks are used and not "double". I said that it is C syntax if using char variable. But he said NO! Why are single quotation marks used and not double?

  3. Sometimes when using scanf nothing happens and it has be used two times to get something scanned. What is the reason of this problem? For example:

    printf("enter string \n");   
    scanf("%s",&str);
    printf("enter char \n");  
    scanf("%c",&ch);  //does not scan my char
    scanf("%c",&ch);  //with this second line do scan my char
    

解决方案

Others have already answered yor First two Questions correctly, So I will answer your third Q:

When you enter a character and hit the ENTER key, two characters are placed in the input buffer, the character and the newline character.

You need to account for both of these. So First scanf consumes the newline and another reads the character.

Step by Step Code Analysis:

printf("enter string \n");   
scanf("%s",&str);

With above two statements, You see Enter the string and program waits for your input. Let us assume you enter the character C and pressed Enter once. When you perform this action, the input buffer receives two characters:

  1. The character C that you entered &
  2. The newline character \n

The scanf statement reads just one character(C) from the input buffer.Thus, the newline character remains unread in the Inuput buffer.

printf("enter char \n");  
scanf("%c",&ch);  //does not scan my char

With above two statements, enter char gets displayed but the scanf just skips(not wait for any uer input), this is because the unread newline character in the Input buffer is read by this scanf.

So to appropriately receive the next input character you need an additional scanf.

scanf("%c",&ch);  //with this second line do scan my char

这篇关于关于C语法的几个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆