Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何运行我自己的打印到std输出的C程序?_C_C99 - Fatal编程技术网

如何运行我自己的打印到std输出的C程序?

如何运行我自己的打印到std输出的C程序?,c,c99,C,C99,我只用了2-3次C。下面的hello world教程没有帮助。该函数应该只打印到std out控制台 #include <stdio.h> void my_putstr(char* param_1) { char *t ; for (t = param_1; *t != '\0'; t++) { printf("%s", t); } } int main(){ my_putstr("abc"); return 0; }

我只用了2-3次C。下面的hello world教程没有帮助。该函数应该只打印到std out控制台

#include <stdio.h>

void my_putstr(char* param_1) {
    char *t ;

    for (t = param_1; *t != '\0'; t++) {
        printf("%s", t);
    }

}

int main(){
    my_putstr("abc");
    return 0;
}
但它仍然给了我“main”的错误:

我有主要的功能。怎么了

gcc file.c -o file
gcc file
第二行将尝试编译用第一行创建的可执行文件,因为它不是C源代码(a),所以不会结束得太好:-)

您需要使用以下内容运行该文件:

./file
void my_putstr(char *str) {
    // Output each character, one at a time.

    for (char *ptr = str; *ptr != '\0'; ptr++)
        putchar(*ptr);

// Output newline (if desired).

    putchar('\n');
}

另外,您应该努力提高程序的可读性,例如:

#include <stdio.h>

// my_putstr:
//     Output the given string multiple times, each time starting
//     at the next character. So, for "1234", it would output
//     "1234 234 34 4" (without the spaces).

void my_putstr(char *str) {
    // Start at position 0, 1, m2, etc until no more string left.

    for (char *ptr = str; *ptr != '\0'; ptr++) {
        printf("%s", ptr);
    }
}

int main(void) {
    my_putstr("abc");
    return 0;
}


(a)
gcc
程序完全能够接受其他输入文件类型(如对象文件、汇编文件等),但我不确定完成的可执行文件是否属于这些类型之一。

gcc file.c-o file
后接
/file
gcc
是一个编译器,因此,一旦您使用它创建了
文件
,您就可以执行
文件
。要运行可执行文件:
/file
请注意
printf(“%s”,t)应该是
printf(“%c”,*t)是教程中的吗?@WeatherVane正确的语句可能是
printf(“%s\n”,t)
为其添加了一点透视图:这种调用在解释语言中很常见,其中脚本“执行”由解释程序显式执行。
#include <stdio.h>

// my_putstr:
//     Output the given string multiple times, each time starting
//     at the next character. So, for "1234", it would output
//     "1234 234 34 4" (without the spaces).

void my_putstr(char *str) {
    // Start at position 0, 1, m2, etc until no more string left.

    for (char *ptr = str; *ptr != '\0'; ptr++) {
        printf("%s", ptr);
    }
}

int main(void) {
    my_putstr("abc");
    return 0;
}
void my_putstr(char *str) {
    // Output each character, one at a time.

    for (char *ptr = str; *ptr != '\0'; ptr++)
        putchar(*ptr);

// Output newline (if desired).

    putchar('\n');
}