2

I am a beginner in C and im currently learning how to make functions in C, below is my code where i want a user to input a name and an occupation at random, then the users random input will be replaced in a sentence. The problem is that when i run this particular program there is no output at all, I am using CodeBlocks and its a console application but when i build and compile and run it, there is nothing on the screen as you can see below, as well as this there is no error messages at all in the "Build log" section where you can normally see errors, the program runs but outputs nothing.

When the program is run all i see on the console is

"Process returned 0 (0x0) execution time : 0.016 s Press ENTER to continue."

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

char FunOne(char Wun[50], char Two[50]);

int main()
{
    char FunOne(char Wun[50], char Two[50]);
    return 0;
}

char FunOne(char Wun[50], char Two[50])
{
    printf("please enter a name: ");
    fgets(Wun, 50, stdin);
    printf("please enter a occupation: ");
    fgets(Two, 50, stdin);
    printf("there was once a man named %s", Wun);
    printf("he lived in the UK and worked as a %s", Two);

    return 0;
}
anastaciu
  • 23,467
  • 7
  • 28
  • 53

1 Answers1

2

The problem is in your main() function, you should do something like this:

Running sample

char FunOne(char Wun[50], char Two[50]); //declaration

int main()
{
    char Wun[50], Two[50]; //declaration of variables (char arrays in this case)
    FunOne(Wun, Two);  //usage
    return 0;
}


char FunOne(char Wun[50], char Two[50]) //definition
{
    //...
}
anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • any idea why? i thought in order to call the function you needed too type the function name, and parameters in the brackets? –  Mar 29 '20 at 01:14
  • in C `char FunOne(char Wun[50], char Two[50]);` is the declaration of the function, in order to use it you then pass the correct parameters to it like `Wun` and `Two`, which also need to be declared and, if needed, initialized. If the return type is not `void` you can assign it to a compatible variable with `=` operator, I leave you a link which has a somewhat simple discription of how functions work in C https://www.tutorialspoint.com/cprogramming/c_functions.htm – anastaciu Mar 29 '20 at 01:32
  • you said I need to pass the correct parameters to use it, hasnt this been done when I specified "char Wun[50], char Two[50]" within "char FunOne(char Wun[50], Two[50]);". –  Mar 29 '20 at 11:11
  • @Qasim, type specifiers such as `char` are only used on declaration and/or definition, when actually using the function they must be removed, Also, `Wun[50]` is a specific element of the array, (and in this case it's out of bounds), if you want to pass the whole array you need to pass `Wun` only. I edited my answer to define these concepts. – anastaciu Mar 29 '20 at 13:54
  • ah ok nice, any books or resources you reccomend to get on a level like you with C because you seem very familiar with C etc. –  Mar 29 '20 at 14:40
  • @Qasim, look here https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list – anastaciu Mar 29 '20 at 14:54