需要帮助在C中创建具有特定数量的\ n的函数

时间:2022-05-18 01:55:35

This is how my code looks right now:

这就是我的代码现在的样子:

#include <stdio.h>

void print_lines(int n); 

int main() {
printf("%i", print_lines(7));
return 0;
}

void print_lines(int n) {
    int i;
    scanf("%i", &n);
    for (i = 1; n != 0; --1) 
        printf("\n");
}

The aim is that the function prints out as many new lines as the user puts in with the scan f function. what am I doing wrong?

目的是该函数打印出与用户使用scan f函数一样多的新行。我究竟做错了什么?

3 个解决方案

#1


1  

Here is what you wish:

这是你想要的:

#include <stdio.h>

void print_lines(int n); 

int main() {
print_lines(7);
return 0;
}

void print_lines(int n) {
    int i;
    for (i = n; i >= 1; --i) 
        printf("\n");
}
  • If the return type of print_lines is void, you cannot use it in an expression as if you were using its value.
  • 如果print_lines的返回类型为void,则不能在表达式中使用它,就像使用其值一样。
  • If you are passing a value (7) to the function, then you need not read it again using scanf.
  • 如果要将值(7)传递给函数,则无需使用scanf再次读取它。
  • If you are already printing in print_lines, then you need not use printf in main. Just the function call is enough.
  • 如果您已经在print_lines中打印,则无需在main中使用printf。只是函数调用就足够了。
  • for (i = 1; n != 0; --1) won't get you anywhere. This line alone has too many errors. You are initializing i, testing for n and incrementing 1 (which is not possible in C).
  • for(i = 1; n!= 0; --1)不会让你到任何地方。仅此一行就有太多错误。您正在初始化i,测试n并递增1(这在C中是不可能的)。

Try reading some basics for better understanding.

尝试阅读一些基础知识以便更好地理解。

Another "trick" might be:

另一个“技巧”可能是:

printf("%.*s\n", 7, "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");

This would print the first 7 characters from the string of 20 characters given as third argument.

这将打印作为第三个参数给出的20个字符的字符串中的前7个字符。

#2


1  

I think a better implementation of what you're trying to get at is this:

我认为更好地实现你想要达到的目的是:

#include <stdio.h>

void print_lines(int n); 

int main() {
    /* take input here (how many lines etc) */
    print_lines(7));
    return 0;
}

void print_lines(int n) {
    int i;
    for (i = 0; i < n; ++i) 
        printf("\n");
}

The variant where you want to use inside of printf would be the following:

要在printf中使用的变体如下:

#include <stdio.h>

void print_lines(int n); 

int main() {
    /* take input here (how many lines etc) */
    char* lines = print_lines(7);
    printf("%s", lines);
    free(lines) // <-- important
    return 0;
}

char* print_lines(int n) {
    int i;
    char* res = malloc(sizeof(char) * (n + 1)); // <-- important, for \0 termination
    for (i = 0; i < n; ++i) 
        res[i] = '\n';
    res[n] = '\0';
    return res;
}

However I'd rather use a more generic approach, where you can get N of any character supplied to the function as a secondary parameter. I'll leave that part to you.

但是,我宁愿使用更通用的方法,在这种方法中,您可以将提供给函数的任何字符的N作为辅助参数。我会把那部分留给你。

EDIT: Here's a version with a user-created buffer:

编辑:这是一个带有用户创建缓冲区的版本:

#include <stdio.h>

void print_lines(int n); 

int main() {
    /* take input here (how many lines etc) */
    char buf[8]; // if you use GCC you can use char buf[N], these are variable length arrays, if not, use a similar malloc method above
    print_lines(7, buf);
    printf("%s", buf);
    return 0;
}

void print_lines(int n, char buf[]) {
    int i;
    for(i = 0; i < n; ++i)
        buf[i] = '\n';
    buf[n] = '\0';
}

And finally, the fantasy solution StoryTeller suggested:

最后,幻想解决方案StoryTeller建议:

#include <stdio.h>

void most_generic_printN(int n, char c, FILE* f) {
    int i;
    for(i = 0; i < n; ++i)
        fprintf(f, "%c", c);
}

int main() {
    most_generic_printN(10, 'a', stdout);
    return 0;
}

In the above solution, stdout is the standard output stream, which is what you see as the console. You can redirect this to be a file and such. Play around with it!

在上面的解决方案中,stdout是标准输出流,您可以将其视为控制台。您可以将其重定向为文件等。玩弄它!

#3


0  

"While N goes to 0, print a newline."

“当N变为0时,打印换行符。”

void print_lines(int n)
{
    while (n --> 0) printf("\n");
}

#1


1  

Here is what you wish:

这是你想要的:

#include <stdio.h>

void print_lines(int n); 

int main() {
print_lines(7);
return 0;
}

void print_lines(int n) {
    int i;
    for (i = n; i >= 1; --i) 
        printf("\n");
}
  • If the return type of print_lines is void, you cannot use it in an expression as if you were using its value.
  • 如果print_lines的返回类型为void,则不能在表达式中使用它,就像使用其值一样。
  • If you are passing a value (7) to the function, then you need not read it again using scanf.
  • 如果要将值(7)传递给函数,则无需使用scanf再次读取它。
  • If you are already printing in print_lines, then you need not use printf in main. Just the function call is enough.
  • 如果您已经在print_lines中打印,则无需在main中使用printf。只是函数调用就足够了。
  • for (i = 1; n != 0; --1) won't get you anywhere. This line alone has too many errors. You are initializing i, testing for n and incrementing 1 (which is not possible in C).
  • for(i = 1; n!= 0; --1)不会让你到任何地方。仅此一行就有太多错误。您正在初始化i,测试n并递增1(这在C中是不可能的)。

Try reading some basics for better understanding.

尝试阅读一些基础知识以便更好地理解。

Another "trick" might be:

另一个“技巧”可能是:

printf("%.*s\n", 7, "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");

This would print the first 7 characters from the string of 20 characters given as third argument.

这将打印作为第三个参数给出的20个字符的字符串中的前7个字符。

#2


1  

I think a better implementation of what you're trying to get at is this:

我认为更好地实现你想要达到的目的是:

#include <stdio.h>

void print_lines(int n); 

int main() {
    /* take input here (how many lines etc) */
    print_lines(7));
    return 0;
}

void print_lines(int n) {
    int i;
    for (i = 0; i < n; ++i) 
        printf("\n");
}

The variant where you want to use inside of printf would be the following:

要在printf中使用的变体如下:

#include <stdio.h>

void print_lines(int n); 

int main() {
    /* take input here (how many lines etc) */
    char* lines = print_lines(7);
    printf("%s", lines);
    free(lines) // <-- important
    return 0;
}

char* print_lines(int n) {
    int i;
    char* res = malloc(sizeof(char) * (n + 1)); // <-- important, for \0 termination
    for (i = 0; i < n; ++i) 
        res[i] = '\n';
    res[n] = '\0';
    return res;
}

However I'd rather use a more generic approach, where you can get N of any character supplied to the function as a secondary parameter. I'll leave that part to you.

但是,我宁愿使用更通用的方法,在这种方法中,您可以将提供给函数的任何字符的N作为辅助参数。我会把那部分留给你。

EDIT: Here's a version with a user-created buffer:

编辑:这是一个带有用户创建缓冲区的版本:

#include <stdio.h>

void print_lines(int n); 

int main() {
    /* take input here (how many lines etc) */
    char buf[8]; // if you use GCC you can use char buf[N], these are variable length arrays, if not, use a similar malloc method above
    print_lines(7, buf);
    printf("%s", buf);
    return 0;
}

void print_lines(int n, char buf[]) {
    int i;
    for(i = 0; i < n; ++i)
        buf[i] = '\n';
    buf[n] = '\0';
}

And finally, the fantasy solution StoryTeller suggested:

最后,幻想解决方案StoryTeller建议:

#include <stdio.h>

void most_generic_printN(int n, char c, FILE* f) {
    int i;
    for(i = 0; i < n; ++i)
        fprintf(f, "%c", c);
}

int main() {
    most_generic_printN(10, 'a', stdout);
    return 0;
}

In the above solution, stdout is the standard output stream, which is what you see as the console. You can redirect this to be a file and such. Play around with it!

在上面的解决方案中,stdout是标准输出流,您可以将其视为控制台。您可以将其重定向为文件等。玩弄它!

#3


0  

"While N goes to 0, print a newline."

“当N变为0时,打印换行符。”

void print_lines(int n)
{
    while (n --> 0) printf("\n");
}