Home > Net >  Implicit declaration of function loop1 and loop2
Implicit declaration of function loop1 and loop2

Time:02-05

Need help with my code, the error I get on loop1 and loop2 is "data definition has no type or storage class" and "Implicit declaration of function loop1 and loop2", and also can any one show me a while loop version of this code.

#include <stdio.h>

int a[5] = { 5, 10, 15, 20, 25 };
int x, temp, size = 5;

int main()
{
    printf("BEFORE: \n\n");
    for (x = 0; x = 4; x  )
    {
        printf("Element [%d] is %d\n"), x, a[x];
    }
    loop1();
    printf("n==============\nAFTER:\n\n");
    loop2();
}

loop1();
{
    for (x = 0; x <= 2; x  )
    {
        temp = a[x];
        a[x];
        a[(size - 1) - x] = temp;
   }
}

loop2();
{
    for (x = 0; x <= 4; x  )
    {
        printf("Element [%d] is %d\n"), x, a[x];
    }
}

CodePudding user response:

I did not try to debug your code. This is the correct definition of the functions syntactically. Based on what you need them to do, you should pass also parameters in. But, the program does not run logically correctly, you should find the problem and tell us the expected output in order to help you.

#include <stdio.h>

int a[5] = { 5, 10, 15, 20, 25 };
int x, temp, size = 5;


void loop1()
{
    for (int x = 0; x <= 2; x  )
    {
        temp = a[x];
        a[x];
        a[(size - 1) - x] = temp;
   }
}

void loop2()
{
    for (int x = 0; x <= 4; x  )
    {
        printf("Element [%d] is %d\n"), x, a[x];
    }
}

int main()
{
    printf("BEFORE: \n\n");
    for (x = 0; x = 4; x  )
    {
        printf("Element [%d] is %d\n"), x, a[x];
    }
    loop1();
    printf("n==============\nAFTER:\n\n");
    loop2();
}

CodePudding user response:

There are many bugs in your program. To mention a few: Functions called before being declared. Arguments placed outside functions calls. Statement that doesn't do anything. Assignment instead of condition. Etc...

Try like:

#include <stdio.h>

int a[5] = { 5, 10, 15, 20, 25 };
int x, temp, size = 5;

void loop1()
{
    for (x = 0; x <= 2; x  )
    {
        temp = a[x];
        //a[x];  ???? why...
        a[(size - 1) - x] = temp;
   }
}

void loop2()
{
    for (x = 0; x <= 4; x  )
    {
        printf("Element [%d] is %d\n", x, a[x]);
    }
}

int main(void)
{
    printf("BEFORE: \n\n");
    for (x = 0; x != 4; x  )
    {
        printf("Element [%d] is %d\n", x, a[x]);
    }
    loop1();
    printf("n==============\nAFTER:\n\n");
    loop2();
}

BTW:

You need to turn up your compilers warning level.

For instance: gcc -Wall -Wextra -Werror ...

  •  Tags:  
  • Related