为了真正符合标准,C中的所有函数(main除外)都必须有原型,即使它们只是在同一个翻译单元中定义之后才使用它们吗?
我写了一个简单的程序来查找3个数字的Sum,average,maximum和最小数.它允许用户输入三个(整数)数字并返回总和,平均值,最大值和最小值.它没有错误,只有警告.这是我的源代码:
main.c中:
#include <stdio.h>
int main()
{
int num1, num2, num3, sum, max, min, avg;
printf("Enter Three \"Integer\" Numbers:");
scanf("%i%i%i", &num1, &num2, &num3);
sum = summation(&num1, &num2, &num3);
avg = average(&sum);
max = max_val(&num1, &num2, &num3);
min = min_val(&num1, &num2, &num3);
printf("Sum: %i Avg: %i MAX: %i MIN: %i", sum, avg, max, min);
return 0;
}
int summation(int *n1, int *n2, int *n3)
{
int s;
s = *n1 + *n2 + *n3;
return s;
}
int average(int *s)
{ …Run Code Online (Sandbox Code Playgroud) final code2.c:9:1: warning: implicit declaration of function 'choice' is invalid in C99 [-Wimplicit-function-declaration]
choice();
^
final code2.c:12:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
choice()
^~~~~~
final code2.c:23:1: warning: implicit declaration of function 'wrong' is invalid in C99 [- Wimplicit-function-declaration]
wrong();
^
final code2.c:25:1: warning: implicit declaration of function 'formula1' is invalid in C99 [-Wimplicit-function-declaration]
formula1();
^
final code2.c:27:1: warning: implicit declaration of function 'formula2' is invalid in C99 [-Wimplicit-function-declaration]
formula2();
^
final code2.c:29:1: warning: implicit declaration of function …Run Code Online (Sandbox Code Playgroud) 这不是一个关于如何解决 C 程序中出现的“隐式函数声明”警告的问题,这个问题已经回答了很多次。
我知道这是一个编译器警告,我想知道为什么这是一个警告而不是错误?如果编译器看不到该函数,那么在运行时调用该函数时会发生什么?链接器最终能解决这个问题吗?或者我们是否假设调用产生此类警告的函数的行为是未知的?
我正在尝试构建我的库,但是一个名为 evutil.c fom libevent 的文件给我带来了困难。
libevent/evutil.c: error: implicit declaration of function 'pipe2' is invalid in C99
涉及的代码是:
if (pipe2(fd, O_NONBLOCK|O_CLOEXEC) == 0)
return 0;
Run Code Online (Sandbox Code Playgroud)
我现在无法将代码更新为 c11。我应该如何更改代码以不再出现此错误?
我经历了以下线程:
可能我的问题是相关的。但是,虽然他们提供了应该在使用函数之前声明函数原型的解决方案,但我想探索当函数名称不匹配时会发生什么。在我的测试中,它仍然可以正常工作。
主 C 文件
#include "node.h"
int main(){
nd *head=NULL;
nd *tail=NULL;
create_node(&head, &tail, 10);
create_node(&head, &tail, 20);
create_node(&head, &tail, 15);
create_node(&head, &tail, 35);
create_node(&head, &tail, 5);
create_node(&head, &tail, 25);
print_list(head, tail);
create_node(&head, &tail, 55);
create_node(&head, &tail, 52);
create_node(&head, &tail, 125);
printf("%d\n",tail->data);
printf("%d\n",head->data);
print_list(head, tail);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
node.h 文件
#ifndef NODE_H
#define NODE_H
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node *next;
struct node *prev;
}nd;
void insert_node(nd **head, nd **tail, int data); …Run Code Online (Sandbox Code Playgroud) 我这里有两个问题对我的C程序:1) main(),行C = enterChar();,N = enterNum();,leftJustifiedPic(C, N);,rightJustifiedPic(C, N);全都给我implicit declaration of function.那有什么意思?我已经习惯了Java,在C代码方面它有点不同吗?
2)在方法enterChar()中,我得到conflicting types for 'enterChar'错误并再次不明白它意味着什么以及为什么会发生.我正在研究Eclipse(Cygwin-GCC),如果它与问题有关.
请问smb请告诉我这类错误和警告?我很感激!
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Welcome to the menu!");
printf("The menu is:\n1. Enter/Change Character\n2. Enter/Change Number\n3. Print Triangle Type 1(Left Justified)\n4. Print Triangle Type 2(Right Justified)\n5. Quit");
printf("\n");
printf("Now enter a number from the menu from 1 through 5: \n");
int num = 0;
scanf("%d", &num);
char C;
int …Run Code Online (Sandbox Code Playgroud) 最近我学会了C中的隐式函数声明.主要观点很明确,但我对在这种情况下理解链接过程有些麻烦.
请考虑以下代码(文件ac):
#include <stdio.h>
int main() {
double someValue = f();
printf("%f\n", someValue);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试编译它:
gcc -c a.c -std=c99
Run Code Online (Sandbox Code Playgroud)
我看到关于隐式声明功能的警告f().
如果我尝试编译和链接:
gcc a.c -std=c99
Run Code Online (Sandbox Code Playgroud)
我有一个未定义的引用错误.一切都很好.
然后我添加另一个文件(文件bc):
double f(double x) {
return x;
}
Run Code Online (Sandbox Code Playgroud)
并调用下一个命令:
gcc a.c b.c -std=c99
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是,一切都成功连接 当然在./a.out调用后我看到了垃圾输出.
所以,我的问题是:如何将隐式声明函数的程序链接起来?在编译器/链接器的引擎下我的例子会发生什么?
试图解决一个练习,我必须每隔5秒在35°C和-10°C之间打印一个随机温度值,然后是日期和时间.一切看起来都按预期工作,但是当我在测试脚本中输入代码时,我会得到以下错误.
这是我的代码:
#include<stdio.h>
#include<unistd.h>
#include<time.h>
#define temp_max 35
#define temp_min -10
#define FREQUENCY 5
int main(void)
{
srand(time(NULL));
while(1)
{
int number = rand() % (temp_max*100 - temp_min*100) + temp_min*100;
double temperature = (double) number;
temperature /= 100;
printf("Temperature=%1.2f @ ",temperature);
fflush(stdout);
system("date");
sleep(FREQUENCY);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这些是我的结果:

这是测试脚本检查的内容:

由于我无法看到自己的错误,任何帮助将不胜感激.
为什么我的代码有效?我在声明它之前调用函数generateNumber,并且我没有在文件的开头设置原型,所以通常它不应该工作,是吗?
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, const char * argv[]) {
int max = 100;
int min = 1;
int mysteryNumber = generateNumber(min, max);
int enteredNumber = min-1;
do{
printf("Enter a number !\n");
scanf("%d", &enteredNumber);
if (enteredNumber > mysteryNumber) {
printf("It's less !");
}else if(enteredNumber < mysteryNumber){
printf("It's more !");
}
}while (enteredNumber != mysteryNumber);
printf("Congratulations, the mystery number was %d \n", mysteryNumber);
return 0;
}
int generateNumber(int min, int max){
srand(time(NULL));
return (rand() % …Run Code Online (Sandbox Code Playgroud) 每当我运行程序"Assignment在没有强制转换的情况下从Integer发出指针"时,我都会收到此错误.我的代码写在下面....请帮助......谢谢
struct student {
char studentID[6];
char name[31];
char course [6];
};
struct student *array[MAX];
struct student dummy;
int recordCtr=0;
int read(){
FILE *stream = NULL;
int ctr;
char linebuffer[45];
char delims[]=", ";
char *number[3];
char *token = NULL;
stream = fopen("student.txt", "rt");
if (stream == NULL) stream = fopen("student.txt", "wt");
else {
printf("\nReading the student list directory. Wait a moment please...");
while(!feof(stream)){
array[recordCtr]=(struct student*)malloc(sizeof(struct student));
while(!feof(stream)) {
fgets(linebuffer, 46, stream);
token = strtok(linebuffer, delims); //This is where the error …Run Code Online (Sandbox Code Playgroud) char** strsep(const char* str)
{
char** returnStrings = NULL;
for (int i = 0; i < strlen(str); i++)
{
if (str[i] == ' ') returnStrings[sizeof(returnStrings)/sizeof(returnStrings[0])] = "";
else returnStrings[sizeof(returnStrings)/sizeof(returnStrings[0])] += str[i];
}
return returnStrings;
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试制作一个可以将句子(字符串)拆分为单词数组(也是字符串)的函数,但它不会编译并显示error: implicitly declaring library function 'strlen' with type 'unsigned long (const char *)' [-Werror,-Wimplicit-function-declaration]. 它说我正在输入一个 unsigned long 作为 strlen() 的参数,但我不是。我有#include <stdio.h>我的代码。