C语言中怎样调用函数(举个例子)
- 提问者网友:謫仙
- 2021-05-10 15:38
- 五星知识达人网友:一叶十三刺
- 2021-05-10 17:16
main(){
a();
}
a(){
int a = 0;
}
这样,在main中调用a函数。
- 1楼网友:不甚了了
- 2021-05-10 18:47
#include <stdio.h>
void fun(void)//自己写的函数 { printf("Hello World\n"); }
int main() { fun();//调用函数,后边加()表示执行,fun就是上边的函数名 return 0; }
- 2楼网友:煞尾
- 2021-05-10 18:24
这是一段C程序基于CUDA开发的!重点请看主函数! #include <stdio.h> #include <stdlib.h>
#include <cutil.h>
#include "clock_kernel.cu"
#define NUM_BLOCKS 64 #define NUM_THREADS 256 int main(int argc, char** argv) { CUT_DEVICE_INIT();
float * dinput = NULL; float * doutput = NULL; clock_t * dtimer = NULL;
clock_t timer[NUM_BLOCKS * 2]; float input[NUM_THREADS * 2];
for (int i = 0; i < NUM_THREADS * 2; i++) { input[i] = i; }
CUDA_SAFE_CALL(cudaMalloc((void**)&dinput, sizeof(float) * NUM_THREADS * 2)); CUDA_SAFE_CALL(cudaMalloc((void**)&doutput, sizeof(float) * NUM_BLOCKS)); CUDA_SAFE_CALL(cudaMalloc((void**)&dtimer, sizeof(clock_t) * NUM_BLOCKS * 2));
CUDA_SAFE_CALL(cudaMemcpy(dinput, input, sizeof(float) * NUM_THREADS * 2, cudaMemcpyHostToDevice));
timedReduction<<<NUM_BLOCKS, NUM_THREADS, sizeof(float) * 2 * NUM_THREADS>>>(dinput, doutput, dtimer);
//CUDA_SAFE_CALL(cudaMemcpy(output, doutput, sizeof(float) * NUM_BLOCKS, cudaMemcpyDeviceToHost)); CUDA_SAFE_CALL(cudaMemcpy(timer, dtimer, sizeof(clock_t) * NUM_BLOCKS * 2, cudaMemcpyDeviceToHost));
CUDA_SAFE_CALL(cudaFree(dinput));(函数调用,调用的函数已经在头文件中包含) CUDA_SAFE_CALL(cudaFree(doutput)); CUDA_SAFE_CALL(cudaFree(dtimer));
// This test always passes. printf( "Test PASSED\n");
// Compute the difference between the last block end and the first block start. clock_t minStart = timer[0]; clock_t maxEnd = timer[NUM_BLOCKS];
for (int i = 1; i < NUM_BLOCKS; i++) { minStart = timer[i] < minStart ? timer[i] : minStart; maxEnd = timer[NUM_BLOCKS+i] > maxEnd ? timer[NUM_BLOCKS+i] : maxEnd; }
printf("time = %d\n", maxEnd - minStart);
CUT_EXIT(argc, argv); }
如果您是入门的话推荐您看谭浩强的C程序设计,如果还有不明白的地方加我,我可以做你的技术支援!