[英]Concatenating char string in C
I've scoured the internet to no avail. 我搜寻了互联网无济于事。 I do not understand what this question is asking. 我不明白这个问题在问什么。
void case_three(int x, int y, char *actualResult) {
int i, j, s, t, p, q;
s = i = x; // initialize variables with value from x
t = j = y; // initialize variables with value from y
p = func(++i, ++j);
q = mac(++s, ++t);
// Copy the output to actualResult below...
printf("\n\n"); //first variable increment
printf("Q3: Result from func(x, y) = %d and mac(x, y) = %d.", p, q);
// Replace the quoted content in the following strcpy statement with the actual output from last printf statement above.
// Do not alter the text or add any spaces to it.
strcpy(actualResult, "Q3: Result from func(x, y) = %d and mac(x, y) = %d", p, q);
printf("\n\n");
printf(actualResult);
}
When I run the code in VS I get 1 and 1 for the func
and mac
solutions. 当我在VS中运行代码时,我得到的func
和mac
解决方案分别为1和1。 When I print the actualResult
string, I get HUGE numbers that change everytime I execute it. 当我打印actualResult
字符串时,我得到的巨大数字每次执行时都会改变。 In addition, when I attempt to compile in gcc, I get a error: too many arguments to function strcpy
at the strcpy(actualResult, "Q3: Result from func(x, y) = %d and mac(x, y) = %d", p, q);
另外,当我尝试在gcc中进行编译时,我得到一个error: too many arguments to function strcpy
在strcpy(actualResult, "Q3: Result from func(x, y) = %d and mac(x, y) = %d", p, q);
line. 线。
So, I need to copy the output from the printf
function to the char string actualResult
but don't know how to do it correctly. 因此,我需要将输出从printf
函数复制到char字符串actualResult
但不知道如何正确执行。
Any help is appreciated. 任何帮助表示赞赏。
Very simple: use "sprintf()" instead of "printf()" to do formatted output to a string. 非常简单:使用“ sprintf()”代替“ printf()”将格式化输出输出到字符串。
You don't need "strcpy()", and you can't use strcpy with formatting commands. 您不需要“ strcpy()”, 也不能将 strcpy与格式化命令一起使用。
EXAMPLE: 例:
/* The exact same output will go to your terminal as to the string "actualResult" */
printf("Q3: Result from func(x, y) = %d and mac(x, y) = %d.", p, q);
sprintf(actualResult, "Q3: Result from func(x, y) = %d and mac(x, y) = %d.", p, q);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.