printf()和字符数组 [英] printf() and character arrays

查看:78
本文介绍了printf()和字符数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看这段代码:


#include< stdio.h>


int main(void){


char name [50];

printf(你叫什么名字?\ n);

fgets(name,sizeof (姓名),stdin);

printf(你的名字是%s。\ n,姓名);


返回0;


}


我在另一行上得到逗号。我知道为什么,因为名字末尾的''\ n'',它是

(这至少是我认为的b / b
)但是我怎么能把它删除?


我还有另外一个问题:真正的程序员如何处理内存分配?以前,何时? />
我已经提出问题,人们已经打电话给我的节目

''玩具应用程序'',他们是,因为

他们没有任何内存分配。

程序员如何使用

许多变量来处理大型应用程序?

解决方案

Eirik写道:


看看这段代码:

#include< stdio.h>
int main(void){

char name [50];
printf(你叫什么名字?\ n);
fgets(name,sizeof( name),stdin);
printf(你的名字是%s。\ n,名字);

返回0;
}

我在另一行上得到逗号。


逗号?逗号是什么?我会失明吗?

我知道为什么,因为名字末尾的''\ n''(这至少是什么<) br />我想,但是我怎么能把它删除?


搜索它并用''\ 0''替换它。这里是一个

方式:


char * p;

...

p = strchr(name,''\ n '');

if(p!= NULL)

* p =''\ 0'';

我有另一个问题:真正的程序员如何处理真正的程序员处理内存分配?以前,当我提出问题时,人们已经打电话给我的程序
''玩具应用程序',他们是,因为
他们没有任何内存分配。一个
程序员如何处理一个带有很多变量的大应用程序呢?




这取决于。


有些应用程序只需要调用malloc()和朋友,因为需要

。其他应用程序包装f ancier应用专用内存

周围的经理。还有一些人不使用malloc()

和朋友,但替换他们自己的完整

内存管理包(当然,当他们这样做时

他们冒险在

标准所映射的领域之外)。


我个人编写的最大的应用程序代码

是一个相对温和的事件,大约三百万

源代码行,~80%C和~20%Lisp 。它使用了一个大杂烩

的不同分配器(完全非标准方式):


- malloc()和朋友,

- 系统特定的API,如sbrk()和mmap(),

- 相关固定池的特殊分配器 -

大小内存块,


- 可变大小块池的另一个分配器,


可变大小的分配器,

- Lisp解释器的内存管理,在上面的所有内容上建立了

(和当然,包括

垃圾收集),


- 各个子系统中的特殊内存管理器,

建立在所有的以上,


- ......可能还有一些我已经忘记或者从来没有想过要知道的事情。


我不是说这种混合物是合适的

对于所有大的应用。然而,这是我的印象,

许多大型节目因为malloc()和朋友的简单性而对
的限制感到不满,并发现自己/>
建立发烧友(以及不那么简单)的内存管理者使他们的生活变得更加轻松。有时候这些发烧友的套餐会使用

malloc()和朋友作为他们的基础,有时候他们不会花钱。


-
Er*********@sun.com


Eirik< hx ****************************** @ xyxaxhxoxo.no> ;写道:

看看这段代码:

#include< stdio.h>

int main(void){

char name [50];
printf(你叫什么名字?\ n);
fgets(name,sizeof(name),stdin);


/ *这会删除数组中的最后一个字符(除非你输入的字符数太多才能输入,否则它将是\ n

*数组)。它会更好地测试最后一个字符是否为换行符。

* /

name [strlen(name) - 1] =''\''';

printf(你的名字是%s。\ n,名字);

返回0;

}

我在另一行上得到了逗号。我知道为什么,因为名字末尾的''\ n''(这至少是我想的那个,但我该如何删除它?




Stig


-

brautaset.org


Eric Sosman写道:

Eirik写道:


看看这段代码:
#include< stdio.h>

int main(无效){

char name [50];
printf("你叫什么名字? \ n");
fgets(name,sizeof(name),stdin);
printf(你的名字是%s。\ n,名字);

返回0;



我的逗号分开了。



逗号?逗号是什么?我是盲目?




可能是一个愚蠢的欧洲人,他们说,,当他们显然意味着

。 :-)


< snip>


-nrk。


Look at this code:

#include <stdio.h>

int main(void) {

char name[50];
printf("What is your name?\n");
fgets(name, sizeof(name), stdin);
printf("Your name is %s.\n", name);

return 0;

}

I get the comma on a separate line. I know why, it is
because of the ''\n'' at the end of "name"(that''s at least what
I think, but how can I remove it?

I have another question: How do real programmers in real
programs deal with memory allocation? Previously, when
I have asked questions, people have called my programs
''toy applications'', which they are, because
they don''t have any memory allocation. How does a
programmer that is working on a big application with
many variables do?

解决方案

Eirik wrote:


Look at this code:

#include <stdio.h>

int main(void) {

char name[50];
printf("What is your name?\n");
fgets(name, sizeof(name), stdin);
printf("Your name is %s.\n", name);

return 0;

}

I get the comma on a separate line.
Comma? What comma? Am I going blind?
I know why, it is
because of the ''\n'' at the end of "name"(that''s at least what
I think, but how can I remove it?
Search for it and replace it with ''\0''. Here''s one
way:

char *p;
...
p = strchr(name, ''\n'');
if (p != NULL)
*p = ''\0'';
I have another question: How do real programmers in real
programs deal with memory allocation? Previously, when
I have asked questions, people have called my programs
''toy applications'', which they are, because
they don''t have any memory allocation. How does a
programmer that is working on a big application with
many variables do?



"It depends."

Some applications just call malloc() and friends as
needed. Others wrap fancier application-specific memory
managers around them. Still others don''t use malloc()
and friends at all, but substitute their own complete
memory management packages (of course, when they do this
they have ventured outside the territory mapped by the
Standard).

The biggest application I personally have written code
for was a relatively modest affair of about three million
source lines, ~80% C and ~20% Lisp. It used a hodgepodge
of different allocators (in entirely non-Standard fashion):

- malloc() and friends,

- system-specific APIs like sbrk() and mmap(),

- a special allocator for pools of related fixed-
size memory blocks,

- another allocator for pools of variable-size blocks,

- replacements for malloc() and friends, built atop
the variable-sized allocator,

- the memory management of the Lisp interpreter, built
atop all of the above (and, of course, including
garbage collection),

- idiosyncratic memory managers in individual subsystems,
built atop all of the above,

- ... and probably a few more I''ve forgotten or was never
aware of in the first place.

I''m not saying that this sort of a melange is appropriate
for all "big" applications. However, it''s my impression that
many large programs chafe at the restrictions that come along
with the simplicity of malloc() and friends, and find themselves
building fancier (and less simple) memory managers to make
life easier for them. Sometimes these fancier packages use
malloc() and friends as their underpinnings, and sometimes
they don''t.

--
Er*********@sun.com


Eirik <hx******************************@xyxaxhxoxo.no> wrote:

Look at this code:

#include <stdio.h>

int main(void) {

char name[50];
printf("What is your name?\n");
fgets(name, sizeof(name), stdin);
/* this removes the last character in the array (which will be the \n
* unless you typed in too many characters to fit in the array). It
* would be better to test if the last character is a newline.
*/
name[strlen(name) - 1] = ''\0'';
printf("Your name is %s.\n", name);

return 0;

}

I get the comma on a separate line. I know why, it is
because of the ''\n'' at the end of "name"(that''s at least what
I think, but how can I remove it?



Stig

--
brautaset.org


Eric Sosman wrote:

Eirik wrote:


Look at this code:

#include <stdio.h>

int main(void) {

char name[50];
printf("What is your name?\n");
fgets(name, sizeof(name), stdin);
printf("Your name is %s.\n", name);

return 0;

}

I get the comma on a separate line.



Comma? What comma? Am I going blind?



Probably a daft European thing, where they say "," when they obviously mean
"." :-)

<snip>

-nrk.


这篇关于printf()和字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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