C语言中怎么判定输入是否为空

原题如下
Calculate a + b

Input
The input will consist of a series of pairs of integers a and b,separated by a space, one pair of integers per line.

Output
For each pair of input integers a and b you should output the sum of a and b in one line,and with one line of output for each line in input.

Sample Input
1 5

Sample Output
6

Hint:

Q:Where is the input and the output?
A:Your program shall read input from stdin('Standard Input') and write output to stdout('Standard Output').For example,you can use 'scanf' in C or 'cin' in C++ to read from stdin,and use 'printf' in C or 'cout' in C++ to write to stdout.
User programs are not allowed to open and read from/write to files, you will get a "Runtime Error" if you try to do so.
不行...提交了答案还是提示Runtime Error

1.使用strlen函数来判断输入是否为空,如果返回值为0,就是空。
  strlen做的是一个计数器的工作,它从内存的某个位置(可以是字符串开头,中间某个位置,甚至是某个不确定的内存区域)开始扫描,直到碰到第一个字符串结束符'\0'为止,然后返回计数器值(长度不包含“\0”)。
原  型:extern unsigned int strlen(char *s);
头文件:string.h
格  式:strlen (字符数组名)
功  能:计算字符串s的(unsigned int型)长度,不包括'\0'在内
说  明:返回s的长度,不包括结束符NULL。

2.例程:

#include <stdio.h>
#include <string.h>
int main(){
    char s[1000];
    while (gets(s)!=NULL){ //循环读入s
        if(0<strlen(s))    //根据判断输出输入是否为空
            printf ("%s 不为空",s);
        else
            printf ("%s 为空",s);
    }
    return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-08-03
ACM吧

#include <iostream>
#incldue <cstdlib>
using namespace std;

int main()
{
int a,b;
while(cin>>a>>b)
cout<<a+b<<endl;

}

就行了

或者while里改成
while((scanf(%d %d,&a,&b))!=EOF)

scanf()里规定了输入两个数之间是空格

而用cin可以不用管 c++的io流很强大 不需要做这种考虑
但是scanf明显比cin快本回答被提问者采纳
第2个回答  2009-08-03
既然是英文题我很喜欢,虽然分不多,这道题说输入函数可以用cin 或scanf
我就用scanf解答
scanf("%d %d",&a,&b); 这样输入的时候是a b 注意是空格之间的格式 哦了
楼上的没看懂题吧

lz可能想判断中间的空格吧 ,不过这题不是这样的
第3个回答  2009-08-03
不用考虑输入为空,程序会暂停等待输入
第4个回答  2009-08-03
if(变量=NULL)
{
}
相似回答