gpt4 book ai didi

c - 更有效的写法?

转载 作者:行者123 更新时间:2023-11-30 18:30:39 24 4
gpt4 key购买 nike

我正在读一本关于 C 的书,它要求我做的问题是:

Write a program that uses printf to display the following picture on the screen

然后是一个带星号的复选标记。因此,我首先想到的是编写一堆使用格式宽度说明符的 printf() 语句。所以我想出了这个:

int main() {
printf("This program prints a checkmark using asteriks\n");

char asterik = '*';


printf("%10c\n", asterik);
printf("%9c\n", asterik);
printf("%8c\n", asterik);
printf("%7c\n", asterik);
printf("%6c\n", asterik);
printf("%c %3c\n", asterik, asterik);
printf("%2c %c\n", asterik, asterik);
printf("%3c\n", asterik);

return 0;

}

我知道这段代码很丑陋,恶心了所有 9 码,但我真的想知道是否有一种更有效、更干净的方法?我不确定作者是否打算这样写,考虑到这一课是在第二章中提出的,我们只是回顾了一些非常基本的事情,与此无关。 (多亏了K&R,我只记得如何调整宽度)

最佳答案

只有一个字符串文字可能会更好,如下所示:

const char checkmark[] =
" *\n"
" * \n"
" * \n"
"* * \n"
" * * \n"
" * \n"
;

printf("%s", checkmark);

关于c - 更有效的写法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29526040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com