如何在Main - C程序中调用函数 [英] How to call functions in Main - C Program

查看:407
本文介绍了如何在Main - C程序中调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我正在练习C编程。我想在Main中调用一个函数,该怎么做?请帮忙。



详细信息:



从Recurression.cpp到MainC.cpp的调用函数(打印)



我正在使用以下环境:



Visual Studio C ++ 2010 Express ---> Win32控制台应用程序(Visual C ++)

Hello,

I am practicing C programming. I want to call a function in Main, how to do that? please help.

Detail:

Call Function (Print) from Recurression.cpp to MainC.cpp

I am using following Environment:

Visual Studio C++ 2010 Express ---> Win32 Console Application (Visual C++)

Recurression.cpp
 ------------

#include <stdio.h>
 
void print(int);
 

void print(int n)
{
  static int c = 1;
 
  if (c == n+1)
    return;
 
  printf("%d\n", c);
  c++;
  print(n);
}

MainC.cpp
-------

#include <stdio.h>

int main()
{
  int n;
 
  scanf("%d", &n);
 
  print(n);
 
  return 0;
}



谢谢


Thanks

推荐答案

一些好消息:你已经调用了这个函数行 print(n); 现在是时候了解它是什么。 :-)



请阅读初级C手册。从更一般的语境中解释这些基本的东西,对于那些没有这样做的人来说是非常困难的,几乎是不可能的。反正没什么意义。先阅读。



-SA
Some good news for you: you already call this function in the line print(n); now it''s the time to understand what is it. :-)

Please read the elementary C manual. Explaining such elementary things out of the more general context to the one who did not do it is extremely difficult, almost impossible. Makes little sense anyway. Read it first.

—SA


一些可以帮到你的小改动:

Some minor changes that will help you:
// Recurression.cpp
//  ------------
 
#include <stdio.h>
 
void print(int n)
{
  static int c = 1;
 
  if (c == n+1)
    return;
 
  printf("%d\n", c);
  c++;
  print(n);
}
</stdio.h>





需要定义打印模块Main.cpp中的函数。



Need to define the print function in module Main.cpp.

// MainC.cpp
// -------
 
#include <stdio.h>
 
void print(int);
 
int main()
{
  int n;
 
  scanf("%d", &n);
 
  print(n);
 
  return 0;
}
</stdio.h>


这篇关于如何在Main - C程序中调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆