控制台编写 [英] console writing

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

问题描述

我有如下代码块,我想得到类似输出

I have codeblock like below i wanna get like an output

hello
ello
llo
lo
o


并且我不想添加任何其他C语句.我在哪里弄错了?


and I DON NOT want to add any other C statement. Where i did mistake?

char str[] = "hello";
char *p = str;
int i =0
for (i=0;i<5;i++)
{
printf("%c",*p++);
}


我当前的输出是:


my current output is:

h
e
l
l
o



%s用于scanf函数,也用于printf(),如清楚地记录在MSDN和每本有关C编程语言的书中.



%s for scanf function, also for printf(), as clearly documented in MSDN and every book ever written about the C programming language.

推荐答案

printf("%s\n", p++);

好的,我搞砸了,忘了换行符;但是,嘿,其他人都没有注意到;)[/edit]
printf("%s\n", p++);

[edit]OK, I messed up and forgot the newline; but, hey, nobody else noticed ;) [/edit]


错误包括:
缺少一个;在int = 0之后.
您一次只打印1个字符
没有换行符(根据您的要求)

不幸的是,如果不添加另一个循环来计算要打印的字符,就无法解决此问题.我会这样做(使用您的限制):

Mistakes include:
Missing a ; after int i =0.
You are only printing 1 char at a time
There is no line feed (as per you requirement)

Unfortunately you cannot fix this without adding another loop to count the chars to print. I would do it like this (using your restrictions):

char str[] = "hello";
int iLength = 5;

for (int i=0; i<iLength; i++)
{
	for (int j = i; j < iLength; j++)
		printf("%c", (int)str[j]);

	printf ("\r\n");
}


正如Richard所指出的那样,您必须替换语句
As already pointed out by Richard, you''ve to replace the statement
printf("%c", *p++);


仅打印单个字符*p(请参阅MSDN [ ^ ])和以下一个


that prints just the single character *p (see MSDN [^]) with the following one

printf("%s\n", p++);

输出从(递增)地址p开始的字符串.
:)

that outputs a string starting from (increasing) address p.
:)


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

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