C代码 [英] C code

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

问题描述

理查德可以或其他任何人告诉我这段代码有什么问题。这是

只是一个简单的错误我很有信心。


#include< stdio.h>


int main(int argc,char * argv []){


char name [] ="输入代码 - " ;;

printf("%s"& name);

fflush(stdout);

char输入[40];

scanf( "%s"& input);

printf("%s"& input);}


现在,如果我输入内容喜欢niceday它是打印但是美好的一天只有

很好打印。空间是个问题。这些函数中是scanf或printf'的字符串

字面值吗?


Bill

解决方案

Bill Cunningham写道:


Richard你能否或其他任何人告诉我这段代码有什么问题。

我确定这只是一个简单的错误。



哪个理查德?我能想到的至少有四个常客。


#include< stdio.h>


int main(int argc,char * argv []){


char name [] ="输入代码 - " ;;

printf("%s", &安培;名称);



为什么你有一个&在名字之前?


fflush(stdout);

char输入[40];

scanf(" %S",&安培;输入);



scanf()停止在第一个空格处读取字符串。在这种情况下使用它也是不安全的,因为它的输入没有限制。


printf("%s" ;,&输入);}


现在,如果我输入像niceday这样的东西,它会被打印但是很美好的一天

只打印好的。空间是个问题。



见上文。使用标准的fgets()或精美的第三方之一

" getline"偶尔发布的类型函数。


Brian




" Default User" ; < de *********** @ yahoo.comwrote in message

news:5p ************ @ mid.individual.net。 ..


Bill Cunningham写道:


>理查德你能否或其他任何人告诉我这段代码有什么问题。
这只是一个简单的错误我很有信心。



哪个理查德?我能想到的至少有四个常客。


> #include< stdio.h>

int main(int argc, char * argv []){char name [] ="输入代码 - " ;;
printf("%s",& name);



为什么你有一个&在名字之前?


> fflush(stdout);
char输入[40];
scanf("%s",& input);



scanf()停止在第一个空格处读取字符串。在这种情况下使用它也是不安全的,因为它的输入没有限制。


> printf("%s"& input);}

现在,如果我输入像niceday这样的东西,它会被打印出来但是很美好的一天
只打印好的。空间是个问题。



见上文。使用标准的fgets()或精美的第三方之一

" getline"类型函数偶尔发布在这里。


Brian



好​​的,我明白了。我指的是richard heathfield。


Bill


Bill Cunningham写道,On 11/11/07 23:51:


Richard你或其他任何人都可以告诉我这段代码有什么问题。它是

只是一个简单的错误我很有信心。



我认为没有理由你专门向理查德提出这个问题,

,特别是因为你没有说明理查德。


#include< stdio.h>


int main(int argc,char * argv []){


char name [] ="输入代码 - " ;;



name是一个char数组。


printf("%s",& name) );



所以& name是指向char数组的指针,而不是指向char的指针。是的,

它指向相同的内存位置,但它是*非常*重要的

差异。


fflush(stdout);

char输入[40];

scanf("%s",& input);



这里的错误相同。其他错误包括:

1)绝不使用%s作为scanf格式说明符。如果名称

太长会怎么样?

2)总是检查stdio.h中输入函数返回的值,否则为
否则你不会知道是否有任何东西。这对于s​​canf来说尤其重要。

3)在你知道C之前,不要使用scanf。如果你想阅读一个

行,请使用fgets(not gets)和*然后*使用你想要的任何工具解析它,可能包括sscanf。
< blockquote class =post_quotes>
printf("%s",& input);}


现在,如果我输入像niceday这样的东西,它会被打印出来但是美好的一天然后只打印好
不错。空间是个问题。这些函数中是scanf或printf'的字符串

literal吗?



这是scanf正在做的事情,即停在第一个

空白处,因为它没有被告知做任何事情不一样。


如果你没有*好*的教科书,我推荐K& R2,如果你查看比较的

参考书目。 lang.c常见问题解答 http://c-faq.com/ 你会发现

详细信息。另外查看常见问题解答会是一个好主意,因为它有一个很简单的问题来回答这个问题。

-

Flash Gordon


Richard can you or anyone else tell me what''s wrong with this code. It''s
just a simple error I''m sure.

#include <stdio.h>

int main(int argc,char *argv[]){

char name[]="Enter Code -";
printf("%s",&name);
fflush(stdout);
char input[40];
scanf("%s",&input);
printf("%s",&input);}

Now if I enter something like niceday it is printed but nice day then only
nice is printed. The space is the question. Is it scanf or printf''s string
literal in those functions?

Bill

解决方案

Bill Cunningham wrote:

Richard can you or anyone else tell me what''s wrong with this code.
It''s just a simple error I''m sure.

Which "Richard"? There are least four regulars I can think of.

#include <stdio.h>

int main(int argc,char *argv[]){

char name[]="Enter Code -";
printf("%s",&name);

Why do you have an & before name?

fflush(stdout);
char input[40];
scanf("%s",&input);

scanf() stops reading a string at the first whitespace. It''s also
unsafe to use in this situation because its input isn''t bounded.

printf("%s",&input);}

Now if I enter something like niceday it is printed but nice day then
only nice is printed. The space is the question.

See above. Use the standard fgets() or one of the fine third-party
"getline" type functions posted here occasionally.


Brian



"Default User" <de***********@yahoo.comwrote in message
news:5p************@mid.individual.net...

Bill Cunningham wrote:

> Richard can you or anyone else tell me what''s wrong with this code.
It''s just a simple error I''m sure.


Which "Richard"? There are least four regulars I can think of.

>#include <stdio.h>

int main(int argc,char *argv[]){

char name[]="Enter Code -";
printf("%s",&name);


Why do you have an & before name?

> fflush(stdout);
char input[40];
scanf("%s",&input);


scanf() stops reading a string at the first whitespace. It''s also
unsafe to use in this situation because its input isn''t bounded.

> printf("%s",&input);}

Now if I enter something like niceday it is printed but nice day then
only nice is printed. The space is the question.


See above. Use the standard fgets() or one of the fine third-party
"getline" type functions posted here occasionally.


Brian

OK I see. I was referring to richard heathfield.

Bill


Bill Cunningham wrote, On 11/11/07 23:51:

Richard can you or anyone else tell me what''s wrong with this code. It''s
just a simple error I''m sure.

I see no reason for you to address this specifically to Richard,
especially as you don''t specify which Richard.

#include <stdio.h>

int main(int argc,char *argv[]){

char name[]="Enter Code -";

name is an array of char.

printf("%s",&name);

So &name is a pointer to an array of char, NOT a pointer to char. Yes,
it points at the same memory location, but it is a *very* important
difference.

fflush(stdout);
char input[40];
scanf("%s",&input);

Same mistake here. Other mistakes include:
1) NEVER use %s as a scanf format specifier. What happens if the name is
too long?
2) ALWAYS check the value returned by the input functions in stdio.h,
otherwise you will not know if anything was obtained. This is especially
important for scanf.
3) Until you know C a lot better don''t use scanf. If you want to read a
line use fgets (not gets) and *then* parse it using whatever tool you
want, possibly including sscanf.

printf("%s",&input);}

Now if I enter something like niceday it is printed but nice day then only
nice is printed. The space is the question. Is it scanf or printf''s string
literal in those functions?

It is scanf doing what it is meant to do, i.e. stopping at the first
white space since it was not told to do anything different.

If you don''t have a *good* text book I recommend K&R2, if you check the
bibliography of the comp.lang.c FAQ at http://c-faq.com/ you will find
the full details. Also checking the FAQ would be a good idea as it has
answers to a *lot* of simple questions like this.
--
Flash Gordon


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

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