-1

my question is pretty simple: if i have a main.c file:

#include <stdio.h>
#include <string.h>
#include "settings.h"

int main(int argc, char * agrv[])
{
    printf("Homepage");
    char choice[3];
    fgets(choice, sizeof(choice), stdin);
    if (strcmp(choice, "1") == 0)
    {
        openSettings();
    }
}

and a settings.h file:

void openSettings();

with settings.c file:

#include <stdio.h>
#include <string.h>
#include "settings.h"

void openSettings()
{
    printf("Settings");
    char choice[3];
    fgets(choice, sizeof(choice), stdin);
    if (strcmp(choice, "1") == 0)
    {
        printf("something");
    }   
    else if (strcmp(choice, "1") == 0)
    {
        //return to main function (the homepage)
    }
}

how can i return to main function in manìin.c from settings.c?

shish
  • 9
  • 1
  • 3
  • 1
    Did you try `return`? – dbush Oct 10 '22 at 12:41
  • @dbush only "return"? – shish Oct 10 '22 at 12:42
  • 3
    Well, the way you've written if, your `openSettings` function is *always* going to return to `main`. In the "something" case, were you wanting it *not* to return to `main`? – Steve Summit Oct 10 '22 at 12:43
  • 2
    You don't even need `return`. Just let the function run to completion. – Weather Vane Oct 10 '22 at 12:44
  • 1
    Remember that [`fgets`](https://en.cppreference.com/w/c/io/fgets) reads and *adds* the newline in the buffer. You might want to [remove it](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input) before using the string. – Some programmer dude Oct 10 '22 at 12:45
  • Also don't skimp on the buffer size. Don't be afraid to use larger buffers for your input. – Some programmer dude Oct 10 '22 at 12:46
  • yes, because if you mess up by typing say "12" that leaves some of it in the buffer, and there will be a mistake at the next input too. – Weather Vane Oct 10 '22 at 12:49
  • The `//return to main function (the homepage)` can never be executed. The code will always end by reaching the end of the function. It is like `if(1==1) { /* some code */} else if (1==1) { /* some other code returning to main function */ } {/* default */}`, that should illustrate, why "default" is always executed. and "return to main" never. – Yunnosch Oct 10 '22 at 13:22

1 Answers1

0

how can i return to main function in [main.c] from settings.c?

In general, when a function finishes executing, control automatically returns to the caller. You don't have to do anything special to make that happen. (The exceptions are when the function terminates by calling a function that terminates execution of the whole program, such as Exit(), or when the program crashes.) You can make function execution terminate at any point via a return statement, or, if the function does not need to return a value, then it's fine to allow control to reach the closing brace of the function body, at which point it returns.

If the function is declared to return a value then that value should be specified via a return statement. Otherwise, return statements in the function must not specify any return value.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157