Why does the program give only the result if it is defined mainly (and how can I change this)?

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 starts, but I get 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(optional). I have already searched, but the only questions related to it are regarding the use mainof a function as a name and incorrect outputs. Maybe I was looking for the wrong way, but I would be glad if someone could answer this question.

+4
source share
4 answers

Since the function returns int, you must assign it inttoo, but you don't need to.

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

, void .

#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;
}
+4

, ...

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

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

, , int something();, something, int. , something();.

+3

, array, declare main. .

, .

.

C

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

:

// 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;
}

. .

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

, , , .

:

 array();

.

:

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;
}
+1

int array(void); main - . , , array, . array main. , :

int main()
{
     int array(void); // you tell compiler that you have a function array.
     array();  // you are executing the code of array();
     return 0;
}
+1

Source: https://habr.com/ru/post/1695078/


All Articles