4

I tried to run this

int array( void )
{
    char text[12] = {  112, 114, 111, 103, 112, 0 };
    int i;
    for(i = 0; text[i]; i = i +1)
        printf("%c", text[i]);
    printf("\n");
    return 0;
}

int main( void )
{
    int array(void);
    return 0;
}

and the program runs but I am getting no result. Now when I use the main function to define the program:

int main( void )
{
    char text[12] = {  112, 114, 111, 112, 0 };
    int i;
    for(i = 0; text[i]; i = i +1)
        printf("%c", text[i]);
    printf("\n");
    return 0;
}

I get the result progr(as wanted). I already searched but the only related questions I find are about using main as a function name and wrong outputs. Maybe I am searching the wrong way but I would be pleased if someone could answer this.

sg7
  • 6,108
  • 2
  • 32
  • 40
Gottaquest
  • 111
  • 6

4 Answers4

3

Since the function returns int, You should've assigned its return value to int too. OR
You can just call it normally with only its name because it always returns a 0 constant.

int i = array(); // i = 0
// or
array();

It's just printing and its value is always 0.
Then I suggest making it void instead because You'll need to call it by name only.

#include <stdio.h>
void array()
{
    char text[12] = {  112, 114, 111, 103, 112, 0 };
    for(int i = 0; text[i]; i++)
        printf("%c", text[i]);
    printf("\n");
}
int main()
{
    array();
    return 0;
}

Note that You can't assign the type void to anything.

Beyondo
  • 2,952
  • 1
  • 17
  • 42
  • 1
    Well, I know, but his function has nothing to do with the return value anyway so it's just printing, I didn't say to worry about the return value but to improve his code, `i = i +1` >> `i++`, also no need to type `void` everywhere since compilers already smart enough to optimize this. – Beyondo Mar 19 '18 at 21:52
2

As stated in the comments on the question, instead of this...

int main(void)
{
  int array(void);
  return 0;
}

You should instead write

int main(void)
{
  array(void);
  return 0;
}

This is because when saying int something(); you are declaring a prototype of a function called something that takes in no parameters and returns an int. Instead, you need to invoke that function simply by typing something();.

notphilphil
  • 385
  • 5
  • 16
  • 3
    I'm sure that you meant he's **declaring**, while **defining** means the actual body of the function between the scopes. – Beyondo Mar 19 '18 at 23:11
2

Your direct problem is that you did not call the function array, you declare it inside the body of main. The declaration itself does not execute the code.

A function declaration tells the compiler about a function's name, return type, and parameters.

A function definition provides the actual body of the function.

Defining a Function

The general form of a function definition in C programming language is as follows

return_type function_name( parameter list ) {
   body of the function
}

In your case you have two function definitions:

// 1.
int array( void )
{
    char text[12] = {  112, 114, 111, 103, 112, 0 };
    int i;
    for(i = 0; text[i]; i = i +1)
        printf("%c", text[i]);
    printf("\n");
    return 0;
}

// 2.
int main(void)
{
    //...
    return 0;
}

Function Declarations

A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately.

int array(void); // function declaration, no parameters, returns int value 

Calling a Function

To call a function, you simply need to pass the required parameters along with the function name, and if the function returns a value, then you can store the returned value.

In your case call it like this:

 array();

since there are no parameters to pass.

To sum up:

int array(void);   // function declaration, no parameters, returns int value 

int array(void)    // definition of the function `array`
{   
    // function body
    char text[12] = {  112, 114, 111, 103, 112, 0 };
    // ...
    return 0;
}   

int main(void)    // definition of the function `main`, 
{
  array();        // function call, calling function `array` with no parameters
  return 0;
}
sg7
  • 6,108
  • 2
  • 32
  • 40
2

int array(void); inside of main is only a declaration. You are telling to the compiler that you have a function elsewhere that is called array that it takes no parameters, and returns nothing. But in no case the array function is being called on main. To call it just change the declaration to an statement like:

int main()
{
     int array(void); // you tell compiler that you have a function array.
     array();  // you are executing the code of array();
     return 0;
}
Luis Colorado
  • 10,974
  • 1
  • 16
  • 31