简体   繁体   English

C、function 调用 printf 后的命令流程

[英]Command Flow in C, function call after printf

So I have this super simple C code here taking a user input and prints it out followed by a "T-Plus" while loop.所以我有这个超级简单的 C 代码在这里接受用户输入并将其打印出来,然后是“T-Plus”while循环。 In this case I chose a random name for testing "whoa", but the while loop is not called.在这种情况下,我选择了一个随机名称来测试“哇”,但不会调用 while 循环。 My question is, why does the "T-Plus: %d\n" while loop print not be called after the printf() function?:我的问题是,为什么在 printf() function 之后不调用“T-Plus: %d\n”while 循环打印?:

#include <stdio.h>

char getString();
void tcount(void);


int main(void)
{
    tcount();
}

void tcount(void)
{
    // class scanf user input
    printf("%s", getString());

    int i = 1;
    do
    {
        printf("T-Plus: %d\n", i);
        i++;
    } while( i < 51 );
}

char getString()
{
    char name;
    printf("Please a string name: \n");
    scanf("%s", &name);
    return name;
}

Now when I run it, this becomes the output:现在当我运行它时,它变成了 output:

$ ./namecount
Please a string name:
whoa

but the T-Plus: string does not get called.但不会调用 T-Plus: 字符串。

I see two issues here:我在这里看到两个问题:

1) In function getString() you are trying to read/scan a string in a char, you need memory to store the string and a terminating char, so you can use either of these two ways 1)在 function getString()中,您尝试读取/扫描字符中的字符串,您需要 memory 来存储字符串和终止字符,因此您可以使用这两种方式中的任何一种

Use a char array eg char name[50];使用 char 数组,例如char name[50]; or或者

Use a char pointer and allocate memory using malloc eg char *p_name = malloc(sizeof(char)*50);使用 char 指针并使用 malloc 分配 memory 例如char *p_name = malloc(sizeof(char)*50);

2) You are then trying to return this string which is stored in local variable (which would get destroyed as soon as function ends) so you should use the second approach (use malloc) and return the pointer. 2)然后您尝试返回存储在局部变量中的这个字符串(一旦 function 结束,它将被销毁)所以您应该使用第二种方法(使用 malloc)并返回指针。

So your code would look like:所以你的代码看起来像:

#include <stdio.h>
#include <stdlib.h>

char * getString();
void tcount(void);


int main(void)
{
    tcount();
}

void tcount(void)
{
    // class scanf user input
    char *p_name = getString();
    printf("%s", p_name);
    free(p_name);

    int i = 1;
    do  
    {   
        printf("T-Plus: %d\n", i); 
        i++;
    } while( i < 51 );
}

char *getString()
{
    char *p_name = malloc(sizeof(char)*50);
    printf("Please a string name: \n");
    scanf("%s", p_name);
    return p_name;
}

Above answer did not work, Okay so I've edited the code like this, it compiles fine.上面的答案没有用,好的,所以我已经编辑了这样的代码,它编译得很好。 But raises a segmentation fault though.但是会引发分段错误。

#include <stdio.h>
#include <stdlib.h>

char * getString();
void tcount(void);

int main(void)
{
    tcount();
}

void tcount(void)
{
    // class scanf user input
    char *name = getString();
    printf("%s", name);
    free(name);

    int i = 1;
    do
    {
        printf("T-Plus: %d\n", i);
        i++;
    } while( i < 51 );
}


char * getString()
{
    char *p_name[50];
    printf("Please a string name: \n");
    scanf("%49s", (char *) &p_name);
    return *p_name;
}

When the program is run, it asks for your input but still raises a Segmentation fault (core dumped).当程序运行时,它会询问您的输入,但仍会引发分段错误(核心转储)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM