gpt4 book ai didi

使用外部函数的C编译错误

转载 作者:太空宇宙 更新时间:2023-11-04 06:25:05 25 4
gpt4 key购买 nike

我是 C 的新手,感觉好像缺少一些格式化语法,但我无法弄清楚到底是什么。编译程序时用

gcc -o test test.c myprintf.c

出现以下错误

expected â;â, â,â or â)â before âsâ

如果这是我的一些简单忽略,我很抱歉,但格式对我来说似乎是正确的。这也是家庭作业的一部分,但是我测试了我负责单独创建的每个功能,它们工作得很好。我省略了我负责编写的功能,如果您认为需要它们,我会编辑它们,因为它似乎甚至无法访问它们。错误直接发生在我的 first 和 section 函数之间(printint 和 printstring 之间的空行。

最简单的测试文件——test.c

#include <stdio.h>

extern void myprintf(const char *, ...);

int main()
{
myprintf("Nothing much\n");

return 0;
}

myprintf.c

#include <stdarg.h>
#include <stdio.h>


void printint(int i){
int temp;
/*some while loop that shrinks i while outputting the correct char*/

int ascii = i + '0';
putchar(ascii);
}

void printstring(char[] s){
int i = 0;
/*some while loop that moves through a string using putchar to output the corresponding character*/
}

void printhex(int k){
/*long complicated program involving a series of loops that change a decimal into a hexidecimal, obviously without using any built in printf or modifiers*/
}

/*This is the code provided that uses my 3 functions. I assume it all to be correct*/

void myprintf(const char *fmt, ...) {
const char *p;
va_list argp;
int i;
char *s;

va_start(argp, fmt);

for (p = fmt; *p != '\0'; p++) {
if (*p != '%') {
putchar(*p);
continue;
}
switch (*++p) {
case 'c':
i = va_arg(argp, int);
putchar(i);
break;

case 'd':
i = va_arg(argp, int);
printint(i);
break;

case 's':
s = va_arg(argp, char *);
printstring(s);
break;

case 'x':
i = va_arg(argp, int);
printhex(i);
break;

case '%':
putchar('%');
break;
}
}
va_end(argp);
}

最佳答案

下面一行是错误的:

void printstring(char[] s){

将其更改为:

void printstring(char s[]){

关于使用外部函数的C编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28202691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com