-2

i have the following warning when compiling with "gcc -ansi -Wall -pedantic conc.c" what i"m doing wrong , please assist

Thanks,

conc.c: In function ‘main’: conc.c:8:1: warning: control reaches end of non-void function [-Wreturn-type]

#include <stdio.h>
void contract(char s1[], char s2[]);
int main()
{
    char s1[] = "abcd";
    char s2[] = "zzzz";
    contract(s1,s2);
}

void contract(char s1[], char s2[])
{
    char temp = 'a';
    int i = 0;
    while (s1[i] != '\0')
    {
        temp = s1[i];
        i++;
    }
    if (i != 2)
    {
        s2[0] = s1[0];
        s2[1] = '-';
        s2[2] = temp;
    }
    printf ("\n The first char string is %s \n the shorten one is %s \n",s1,s2);
}

3 Answers3

3

Your main function doesn't explicitly return anything. Just slap a return 0; at the end and you should be OK:

int main()
{
    char s1[] = "abcd";
    char s2[] = "zzzz";
    contract(s1,s2);
    return 0; /* Here! */
}
Mureinik
  • 252,575
  • 45
  • 248
  • 283
3

The -ansi option switches to the ISO C90 standard. Back then, the main function had to return something. Since C99, it's OK to leave out the return statement.

Roland Illig
  • 37,193
  • 10
  • 75
  • 113
3

When using -ansi (=C89) you don't get the default behaviour of main returning 0 if you don't return anything – you defined a non-void return type (int), and now you simply have to return something.

Marcus Müller
  • 27,924
  • 4
  • 40
  • 79