2

Sorry if I seem stupid for asking this but I want to reinforce my understanding and clear any misconceptions I have about user define functions.

"Case:" I have two functions (can be either void or int) which I will call them function1 and function2. I have them in a file that will be called by other programs. I have function1 as a void function called in another program and will keep being called until the program ends (function2 will not run when function1 is running).

Function2 must be executed by another program(not the same program for function1) calls for function2 (function1 must not be running in this case). Function2 can be an int or void function.

I know that the standard procedure would to put them in order in my main function since this program will call main only. The main will run in the order of the functions placed (e.g. function1 will run first then function2)

eg

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

void function1(....)
int function2(...)

void function1(intA,intB,struct.....)
{
  ...Conditions, loops, whatever
}


int function2()
{
..........
return(some value)
} 

In this part, I would have the main at the bottom

int main(int argc, char *argv[]) 
{
function1();
int A;

A = function2();
....
}

I thought that I would use if else conditions in main to make "Case:" possible.

Would this sample code run exactly as I mentioned at the top under Case: (with if else conditions)? If not, what am I getting confused? Example code in c would help very much as well with clear explanations.

Please tell me if there is something confusing with my explanation or my question. I will try to make it clear.

4
  • Do you want use swith statement rather if-else to switch to your function? Commented Dec 1, 2013 at 12:39
  • -@Haccks I assume you mean switch statement. That depends since I have both function1 and function2 being called at certain times (And not running at the same time). Switch can work but I use if else condition more often switch. I want if my code follows what I want (Case) Commented Dec 1, 2013 at 12:42
  • I don't understand what you mean with "program". Is this a library? Multithreaded? Or is it a single program? And what problem are you trying to solve with an if-else-statement? Commented Dec 1, 2013 at 12:44
  • -@iveqy what I mean by program is I have a c program, the one I wrote on my OP. The functions will be called by other c files that are running somewhere else. The if else statement ensures that function1 and function2 are not running at the same time. Commented Dec 1, 2013 at 12:47

1 Answer 1

1

If you want to call only one function, use a command line parameter which you get via argv/argc.

int main(int argc, char *argv[])
{
    if (argc != 2)  return -1; // zero or 2+ commandline arguments

    if (0 == strcmp(argv[1], "function1"))
         function1();
    else if (0 == strcmp(argv[1], "function2"))
         function2();
    else { return -1; /* error */ }

    return 0;
}

What happens behind the scenes (before main() is called), a partial list:

  • OS creates new prrocess and executes it's entry point address (not main()).
  • Entry point function receives all command line arguments from the OS in some OS dependent manner.
  • Various C std library are called to initialize the framework.
  • Environment variables are copied to std C library structures.
  • Dependent DLL's are loaded.
  • Globals are initialized.
  • An array of char* is created (argv). Each array entry points to a C string. The first (argv[0]) is always the exe name. If the OS/shell have passed command line arguments, they are copied to argv[1], argv[2], etc.
  • main(int, char**) is called with argv and argc.

All this work is done to abstract the OS from the program. The above flow is heavily OS dependent and doesn't interest most developers. When you're an (very) advanced C programmer or if you take an interest in this flow then you can read on your own.

main() prototype is defined by the C standard. Actually there are several legal prototypes to choose from. The compiler/linker will choose what you implement.

Sign up to request clarification or add additional context in comments.

12 Comments

How does the strcmp function exactly works? All I know is that it compares bytes and returns a negative or positive value.
robably a better question is how does each part in this command line parameter work? Also for the else if for function2, would it matter if it were a void function?
@GhostMember - strcmp subtracts chars from str1 by str2, the first non zero is returned. Positive number is left is higher, negative right is higher and zero for equal.
@GhostMember - argc - number of arguments in argv. argv[0] contains the exe name. argv[1..N] contain commandline arguments if they exist.
@egur- Okay, I get now. However, when I was testing the example, I had an error in my terminal saying that "too few arguments to function call, expected 2, have 1" for the if statement and the else if. Are the errors caused by the lack of default arguments? What should I add to it and why?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.