TransWikia.com

How to call function from other files like headers in C?

Stack Overflow Asked by oussama lahouar on December 31, 2020

I’m trying to build a simple program that tells the user their Zodiac sign and life path number. The problem is even when given a wrong birth date it keeps writing the life path number. I am using Euclidean division exmp 12/02/1999

year 1999 = 1 + 9 + 9 + 9 = 28  2 + 8 = 10  1 + 0 = 1

I want to call this function from the main.c. How can I do that? thanks

#include <stdio.h>
#include <stdlib.h>
    
int year_verif(year,q3,ql,r,q2,q1,q4,xy,xt,xt2)
{
  printf("Enter your birth yearn");
  scanf("%d", &year);
  while (year <= 1920 || year >= 2020) {
    printf("Invalid Year of Birth n");
    printf("Re-enter your birth yearn");
    scanf("%d", &year);
  }
    
  q1 = year / 1000;
  r = year % 1000;
  q2 = r / 100;
  r = r % 100;
  q3 = r / 10;
  r = r % 10;
  q4 = r;
  xy = q1 + q2 + q3 + q4;
    
  if (xy >= 10 )
  {
    xt = xy / 10;
    xt2 = xy % 10;
    xy = xt + xt2;
  }

  printf("year ""%dn",xy);
  return 0;
}

2 Answers

You only need one parameter in your function. Also, you need to declare your local variables and parameters (you can do the way you are doing, but that's legacy code that should be avoided for new programs, just to compile very old code) Here is a possible way of what you are trying to do, to compute the verification year number of the passed value. I think this is what you are trying to do in your program, but I'm not sure. Anyway, it seems to work, and illustrates you how you can define two functions in a single compilation unit, and call year_verif() from main() and passing it one parameter (the year value, computed in main())

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

int year_verif(int yr)
{

  int q1 = yr / 1000;
  int r = yr % 1000;
  int q2 = r / 100;
  r = r % 100;
  int q3 = r / 10;
  r = r % 10;
  int q4 = r;
  int xy = q1 + q2 + q3 + q4;

  if (xy >= 10 )
  {
    int xt = xy / 10;
    int xt2 = xy % 10;
    xy = xt + xt2;
  }

  return xy;

}

int main(void)
{
  printf("Enter your birth yearn");
  int year;
  int res = scanf("%d", &year); /* scanf returns the number of matched
                                 * parameters, so it should be 1 on
                                 * good reading. */
  while (res == 1 && (year <= 1920 || year >= 2020)) {
    printf("Invalid Year of Birth n");
    printf("Re-enter your birth yearn");
    res = scanf("%d", &year);
  }
  if (res == 1)
    printf("year_verif(%d) => %dn", year, year_verif(year));
  return 0;
}

It compiles and runs like this:

$ a.out
Enter your birth year
13600
Invalid Year of Birth 
Re-enter your birth year
142
Invalid Year of Birth 
Re-enter your birth year
1962
year_verif(1962) => 9
$ _

Answered by Luis Colorado on December 31, 2020

"I want to call this function from the main.c."

Jump to bottom if you simply want the answer to this question. Otherwise, following are some observations and suggestions...

As mentioned in comments under your question, some implementations of the C standard (eg. Clang, GCC, and MSVC) allow functions declared with identifier lists, such as this

int year_verif(year,q3,ql,r,q2,q1,q4,xy,xt,xt2)

...but always expect explicit typed declarations before use. For example, called in main():

int main(void)
{
    int year = 0;
    int q3 = 0;
    int ql = 0;//note - que ell
    int r = 0;
    int q2 = 0;
    int q1 = 0;// note que one
    int q4 = 0;
    int xy = 0;
    int xt = 0;
    int xt2 = 0;
    year_verif(year,q3,ql,r,q2,q1,q4,xy,xt,xt2);  

(Your original code is missing this step and needs to be added.)

Although identifier lists are accommodated by the standard, it is more common and acceptable to use parameter type lists (differences are discussed here) in the function argument list,

int year_verif(int year,int q3,int ql,int r,int q2,int q1,int q4,int xy,int xt,int xt2);

If you need updated values of variables to be passed back to the calling function (otherwise ignore this section.), one other important addition is needed before any of these variables can be used to pass back results of the work done in the function. The changed value of a variable can only be returned via a pointer argument, as shown in the following adaptation of the original prototype:

int year_verif(int *year,int *q3,int *ql,int *r,int &q2,int *q1,int *q4,int *xy,int *xt,int *xt2);

Now when called in main() the address of (&) each argument will be passed so that the updated values can be passed back:

year_verif(&year,&q3,&ql,&r,&q2,&q1,&q4,&xy,&xt,&xt2);

One more suggestion however, since there are so many arguments, it would be more readable and otherwise manageable to collect all arguments into one construct, then pass that single object:

typedef struct {
   int year;
   int q3;
   int ql;//note - que ell
   int r;
   int q2;
   int q1;//note - que one
   int q4;
   int xy;
   int xt;
   int xt2;
}date_s;

Then your function prototype becomes:

int year_verif(date_s *date)
{
    //...
}

this can be called in main like this:

int main(void)
{
    ...
    date_s date = {0};
    int ret = year_verif(&date);
    ...

A quick comment about your title question, i.e.:

"How to call function from other files like headers in C?"

This is possible, but architecturally really not a good idea, as laid out in this answer to a similar question.

"How to call a function residing in one .c file from a function (eg. main()) residing in another file":

Say your function (no matter what form it has taken.) is defined in a file called date.c

date.c

...
int year_verif(date_s *date)
{
    ...
}

Place the prototype for the function: int year_verif(date_s *date); into a header file named for example date.h

date.h

int year_verif(date_s *date);  

Then, in main.c, add the line: #include "data.h" along with other header files at the top of the main.c file:

main.c

#include <stdio.h>
#include <stdlib.h>
#include "date.h"

Now the function can be called just as shown above.

Answered by ryyker on December 31, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP