C-函数隐式声明时间,rand,printf,我所有的函数调用

标签 c declaration implicit

这件事困扰了我一整天。我是 C 的新手,我无法让它工作,我也不知道为什么。我有 3 个文件..

我还想为间距道歉,因为这是我第一次使用堆栈溢出...但是无论如何,这是代码...

assignment_1.h-------------------------------------------- --------------

#ifndef ASSIGNMENT_1_H
#define ASSIGNMENT_1_H

#define NULL 0

int CalculateFactorial(int input);
int CalculateFibonacci(int input);
void ReverseArray(int size, int array[]);
void ShuffleArray(int size, int array[]);

#endif

Main.c---------------------------------------- ----------------------------

#include "assignment_1.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


int main(){

int fact = CalculateFactorial(15);
int fib = CalculateFibonacci(15);

printf("Factorial: %d\n", fact);
printf("Fib: %d\n", fib);

int array[] = {1,2,3,4,5,6};
int size = 6;

ReverseArray(size, array);
ShuffleArray(size, array);

return 0;


}/*end main*/

assignment_1.c-------------------------------------------- ------------------

#include "assignment_1.h"


int CalculateFactorial(int input){

if(input<=0)    return 0;


int factorial = input;
int multiplier = input;

while(multiplier > 1){
    factorial *= multiplier;
    --multiplier;
}/*end while*/

return factorial;

}/*end calcfact*/



int CalculateFibonacci(int input){

if(input<=0)    return 0;

else if(input == 1)     return 1;

return CalculateFibonnaci(input-1) + CalculateFibonacci(input-2);

}/*end calcfib*/



void ReverseArray(int size, int array[]){

int last = size-1;
int first = 0;
int temp;

while (last-first > 1){     /*stops the loop if size is even*/

temp = array[last];
array[last] = array[first];
array[first] = temp;

++first;
--last;

if(last-first == 2)     break; /*stops loop if size is odd*/

}/*end while*/

int i;
for (i = 0; i< size;++i){
    printf("%d, ",array[i]);
}
printf("\n");

}/*end reverse*/



void ShuffleArray(int size, int array[]){

srand((unsigned int)time(NULL));

int i;
for (i = 0; i < size; ++i){

    int idx = rand()%size; /*random unsigned int between 0 and the 
                        max index of the array*/

    int temp = array[i];
    array[i] = array[idx];
    array[idx] = temp;

}/*end for loop*/

for (i = 0; i< size;++i){
    printf("%d, ",array[i]);
}
printf("\n");

}/*end shuffle*/

我一直收到一大堆错误,都说警告:函数''的隐式声明]

我非常感谢任何帮助...

最佳答案

由于您将 assignment_1.c 单独编译为 main.c(如果您计划在编译后使用链接器将两者链接在一起,这是合理的),您需要在该文件中包含这些 header ,< em>也是;它们不会自动从一个翻译单元转移到另一个翻译单元。

将这些插入 assignment_1.c:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

我发现了一个额外的错误,即 NULL 的定义您在 assignment_1.h 中指定; 应该永远定义NULL你自己,因为它是一个标准符号。这就像写你自己的 printfscanf .

NULL<stddef.h> 中定义 header 。当你想使用 NULL 时也包括它.

关于C-函数隐式声明时间,rand,printf,我所有的函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42215938/

相关文章:

c - 重新分配原始数组后,数组元素会发生什么?

C - 在函数中返回 2 个值并尝试使用它们

Java 编译错误 : class Appletprac is public, 应在名为 Appletprac.java 的文件中声明

scala - Scala:隐式传递一个隐式参数,显式传递另一个。可能吗?

Scala 隐式包含 Map 的 Option

c - 在 C 中将文件发送到标准输出

计算字符串中某个数字出现的次数

c++ - 什么时候 namespace::function() 声明有用?

c++ - 在 Xcode 下的 C++ 中声明 int 数组

c++ - 竞争隐式和模板复制构造函数