我的任务是使用printf显示程序。我的解决方案是通过引号遍历每一行。但是当我编译它时我就卡住它要我宣布" d"
我在打印整个程序时遇到错误。它希望我定义" d" 请帮帮我。
#include <stdio.h>
int main(void)
{
printf(
"/* This program reads two integers from the keyboard and prints their product."
"Written by: A Katheravan"
"Date : 10/02/2012"
"*/"
"#include <stdio.h>"
"int main (void)"
"{"
"//Local Definitions"
"int number1;"
"int number2;"
"int result;"
"//Statements"
"scanf("%d", &number1);"
"scanf ("%d", &number2);"
"result = number1 * number2;"
"printf("%d", result);"
"return 0;"
"}"
"//main");
return 0;
}
答案 0 :(得分:2)
以下内容可能对您有用:
FILE * fp = fopen("program.c", "r");
char c;
while ((c = fgetc(fp)) != EOF)
printf("%c", c);
fclose(fp);
答案 1 :(得分:0)
那些产生自己的源代码作为输出的程序被称为“Quines”。
根据维基百科Quines
“quine是一个计算机程序,它不接受输入并生成自己的源代码副本作为其唯一的输出”
查看示例并完整解释Quines
这是经典的C Quine
char*f="char*f=%c%s%c;
main()
{
printf(f,34,f,34,10);
}%c";
main(){printf(f,34,f,34,10);}
答案 2 :(得分:0)
#include <stdio.h>
int main(void)
{
printf("%s",
"#include <stdio.h>\n"
"\n"
"int main (void)\n"
"\n"
"{\n"
"\n"
"//Local Definitions\n"
"\n"
"int n1;\n"
"int n2;\n"
"int result;\n"
"\n"
"//Statements\n"
"\n"
"scanf(\"%d\", &n1);\n"
"scanf (\"%d\" , &n2);\n"
"result = n1 * n2;\n"
"printf(\"%d\", result);\n"
"return 0;\n"
"\n"
"}\n"
"//main\n");
return 0;
}
这可能不太正确,因为OP在发布时没有格式化代码。插入空格和/或制表符以获得所需的缩进。
编辑:这是对原始问题的回答,但OP已将问题改为(错误的)尝试回答问题。
答案 3 :(得分:0)
嗯,基于对你想要得到的东西的一些刻板思考,我终于认为你想要这个:
#include <stdio.h>
int main(void)
{
printf(
"/* This program reads two integers from the keyboard and prints their product.\n"
"Written by: A Katheravan\n"
"Date : 10/02/2012\n"
"*/\n\n"
"#include <stdio.h>\n\n"
"int main (void)\n"
"{\n"
"\t//Local Definitions\n\n"
"\tint number1;\n"
"\tint number2;\n"
"\tint result;\n\n"
"\t//Statements\n\n"
"\tscanf(\"%%d\", &number1);\n"
"\tscanf (\"%%d\", &number2);\n"
"\tresult = number1 * number2;\n"
"\tprintf(\"%%d\", result);\n"
"\treturn 0;\n\n"
"}\n"
"//main\n"
);
return 0;
}
要在字符串中打印"
,您需要将其转义:\"
,这是您在字符串中的问题。
我还在每一行都包含换行符和标签,因此代码不会在控制台的一行中打印出来。
另外,要在%
上打印printf
,您必须使用%%
,因为您稍后会了解%
是为格式化字符串而保留的。
\n
是“新行”,\t
是“TAB”,仅供参考。