0

When i run my code in online GCC compiler i get no errors and program gets compiled but when i try to run it in VSCODE it gives me error that function declaration is not allowed here. Code.

#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
int main()
{
  int n, x, y, i, j;
  printf("How many numbers do you want to enter? ");
  scanf("%d",&n);
  int *a=(int*)malloc(n*sizeof(int));
  printf("Enter the numbers: ");
  for(int i=0; i<n; i++)
        scanf("%d",&a[i]);
  printf("Enter the value of the two distance x and y: ");
  scanf("%d %d",&x,&y);

int minDist(int arr[], int n, int x, int y)
{
    int i, j;
    int min_dist = INT_MAX;
    for (i = 0; i < n; i++) {
        for (j = i + 1; j < n; j++) {
            if ((x == arr[i] && y == arr[j]
                 || y == arr[i] && x == arr[j])
                && min_dist > abs(i - j)) {
                min_dist = abs(i - j);
            }
        }
    }
    if (min_dist > n) {
        return -1;
    }
    return min_dist;
}
  printf("\nThe Minimum Distance between %d and %d is %d.\n",x, y,minDist(a,n,x,y));
  free(a);
  return 0;
}
  • Move `int minDist(int arr[], int n, int x, int y) {...}` OUTSIDE of `main()`. C does not allow nested functions. (`main()` is a function, so you can't declare another function inside it) Double check the gcc online compile -- I seriously doubt it compiled. – David C. Rankin Aug 14 '21 at 04:43
  • I don't think you're allowed to define functions inside each other in C. You should define `minDist` outside of `main` and then declare it at the above of main. have you tried this? –  Aug 14 '21 at 04:45
  • For future question it would be useful to specify which compiler you are using. VS Code is not a compiler but an editor that can be used together with different compilers. – Gerhardh Aug 14 '21 at 08:34
  • @DavidC.Rankin: C does allow nested functions; C 2018 4 6 grants permission: “A conforming implementation may have extensions (including additional library functions), provided they do not alter the behavior of any strictly conforming program.” And trying an online GCC compiler as you suggested confirms [it does compile](https://godbolt.org/z/4538n93G5). [Nested functions are a documented GCC extension.](https://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html) – Eric Postpischil Aug 14 '21 at 11:09
  • @EricPostpischil Thank you. That explains why it compiled -- after starting with C the year before the '89 standard was promulgated I'm often surprised by what is in the latest draft -- especially when it is contrary to what existed through C11. (the last compiler supported by all but bleeding-edge distros) I like the local jump aspect of the GCC extension(the old near-jump) – David C. Rankin Aug 14 '21 at 21:46

5 Answers5

4

You're declaring minDist inside main. Nested functions are a GNU extension. Other compilers might not allow them.

luther
  • 5,195
  • 1
  • 14
  • 24
2

Take it and move it before the main function.

Here is your code

#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
int minDist(int arr[], int n, int x, int y);

int minDist(int arr[], int n, int x, int y)
{
    int i, j;
    int min_dist = INT_MAX;
    for (i = 0; i < n; i++) {
        for (j = i + 1; j < n; j++) {
            if ((x == arr[i] && y == arr[j]
                 || y == arr[i] && x == arr[j])
                && min_dist > abs(i - j)) {
                min_dist = abs(i - j);
            }
        }
    }
    if (min_dist > n) {
        return -1;
    }
    return min_dist;
}
int main()
{
  int n, x, y, i, j;
  printf("How many numbers do you want to enter? ");
  scanf("%d",&n);
  int *a=(int*)malloc(n*sizeof(int));
  printf("Enter the numbers: ");
  for(int i=0; i<n; i++)
        scanf("%d",&a[i]);
  printf("Enter the value of the two distance x and y: ");
  scanf("%d %d",&x,&y);
  printf("\nThe Minimum Distance between %d and %d is %d.\n",x, y,minDist(a,n,x,y));
  free(a);
  return 0;
}
Nishant Bangera
  • 176
  • 1
  • 1
  • 8
  • Telling somebody what to change their code to does not answer questions about what was wrong with the code or why it worked in GCC. – Eric Postpischil Aug 14 '21 at 12:43
1

Following C standards, your program is not wrong.

You've used nested functions and that's supported in C. The reason to why your program worked just fine when compiled using gcc and not in VS Code is that

Nested functions are supported as an extension in GNU C, but are not supported by GNU C++

according to this(which was also pointed out by @luther in his answer).

VS Code's C/C++ extension uses the g++ command to compile C and C++ programs (yes, you can use both gcc and g++ to compile .c and .cpp files). g++ will compile .c and .cpp files, but will treat them as C++ files. So, now it's clear why your program does not work when compiled and run in VS Code.

Check this to look at VS Code's C/C++ extension and this to know the difference between gcc and g++.

If you really want to have your program compile and run in VS Code then you can choose to do one of these 2:

  1. You can place the the minDist() before the main() function in your code like this:
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>

int minDist(int arr[], int n, int x, int y)
{
    int i, j;
    int min_dist = INT_MAX;
    for (i = 0; i < n; i++) {
        for (j = i + 1; j < n; j++) {
            if ((x == arr[i] && y == arr[j]
                 || y == arr[i] && x == arr[j])
                && min_dist > abs(i - j)) {
                min_dist = abs(i - j);
            }
        }
    }
    if (min_dist > n) {
        return -1;
    }
    return min_dist;
}

int main()
{
  int n, x, y, i, j;
  printf("How many numbers do you want to enter? ");
  scanf("%d",&n);
  int *a=(int*)malloc(n*sizeof(int));
  printf("Enter the numbers: ");
  for(int i=0; i<n; i++)
        scanf("%d",&a[i]);
  printf("Enter the value of the two distance x and y: ");
  scanf("%d %d",&x,&y);
  printf("\nThe Minimum Distance between %d and %d is %d.\n",x, y,minDist(a,n,x,y));
  free(a);
  return 0;
}
  1. You may declare the function before the main() function and have the function's body after the main() function. Like this:
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>

int minDist(int arr[], int n, int x, int y); // declaration of the minDist() function


int main()
{
  int n, x, y, i, j;
  printf("How many numbers do you want to enter? ");
  scanf("%d",&n);
  int *a=(int*)malloc(n*sizeof(int));
  printf("Enter the numbers: ");
  for(int i=0; i<n; i++)
        scanf("%d",&a[i]);
  printf("Enter the value of the two distance x and y: ");
  scanf("%d %d",&x,&y);
  printf("\nThe Minimum Distance between %d and %d is %d.\n",x, y,minDist(a,n,x,y));
  free(a);
  return 0;
}

int minDist(int arr[], int n, int x, int y)
{
    int i, j;
    int min_dist = INT_MAX;
    for (i = 0; i < n; i++) {
        for (j = i + 1; j < n; j++) {
            if ((x == arr[i] && y == arr[j]
                 || y == arr[i] && x == arr[j])
                && min_dist > abs(i - j)) {
                min_dist = abs(i - j);
            }
        }
    }
    if (min_dist > n) {
        return -1;
    }
    return min_dist;
}

Edit: Many thanks to Erik Postpischil for pointing out that I have not completely answered the question.

  • This answer does not explain why the code worked in GCC or specifically why it is “wrong.” – Eric Postpischil Aug 14 '21 at 12:44
  • *Following C standards, your program is not wrong.* I'd disagree with that. Nested functions are ***not*** [standard C](https://port70.net/~nsz/c/c11/n1570.html) See [**Nested function in C**](https://stackoverflow.com/questions/2608158/nested-function-in-c) – Andrew Henle Aug 14 '21 at 14:44
  • @AndrewHenle: The answer is correct that OP’s program is not wrong, although it is not entirely correct to say they are “supported” in C. The C standard generally does not classify programs as “wrong.” It characterizes programs as *strictly conforming* and *conforming*, and, by omission, programs which are neither. OP’s program is, as defined by the C standard, a conforming program (at least in regard to the nested function; I have not checked the rest of it). The C standard **explicitly invites** extensions (C 2018 4 6), so programs which use them are not “wrong” because of it. – Eric Postpischil Aug 14 '21 at 15:53
  • 1
    @AndrewHenle: The portability of C is often mistaken; C was never intended to be a language that was portable like Java was, in the sense that programs could be written once and run on every C platform. It was intended to be portable in that the core C language could be ported to many different platforms, each of which could customized or extend the language as desired for that platform and its users’s purposes. Extensions in C are a feature of the design, not wrong. – Eric Postpischil Aug 14 '21 at 15:54
  • @EricPostpischil Why did you bring up portability? I was merely addressing the fact that nested functions do not "[follow] C standards". – Andrew Henle Aug 14 '21 at 18:49
  • @AndrewHenle: Because thinking that nested functions do not follow C standards (they do) is part of thinking that following the C standard means using only strictly conforming code, rather than conforming code. Nested functions are conforming code, according to the C standard. – Eric Postpischil Aug 14 '21 at 19:13
0

c program is compiled in top to bottom so before using function in main you have to specify that there is a function that exist. for this write function prototype in up or write entire function body before main().

#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
int minDist(int arr[], int n, int x, int y);

int main()
{
  int n, x, y, i, j;
  printf("How many numbers do you want to enter? ");
  scanf("%d",&n);
  int *a=(int*)malloc(n*sizeof(int));
  printf("Enter the numbers: ");
  for(int i=0; i<n; i++)
        scanf("%d",&a[i]);
  printf("Enter the value of the two distance x and y: ");
  scanf("%d %d",&x,&y);
  printf("\nThe Minimum Distance between %d and %d is  %d.\n",x,y,minDist(a,n,x,y));
  free(a);
  return 0;
}

int minDist(int arr[], int n, int x, int y)
{
    int i, j;
    int min_dist = INT_MAX;
    for (i = 0; i < n; i++) {
        for (j = i + 1; j < n; j++) {
            if ((x == arr[i] && y == arr[j]
                 || y == arr[i] && x == arr[j])
                && min_dist > abs(i - j)) {
                min_dist = abs(i - j);
            }
        }
    }
    if (min_dist > n) {
        return -1;
    }
    return min_dist;
}
-1

Why are you declaring the function inside the main function?

Take it and move it before the main function.

  • Even if i take the function out i still get error.implicit declaration of function 'minDist' is invalid in C99 [-Werror,-Wimplicit-function-declaration] printf("\nThe Minimum Distance between %d and %d is %d.\n",x, y,minDist(a,n,x,y)); ^ 1 error generated. – Neetesh Khanal Aug 14 '21 at 04:53
  • Have you moved it before the main function (where you are using it) or moved it behind the main function(so you are using it before you declared it)? – OneTrickDragon Aug 14 '21 at 04:59
  • 1
    @NeeteshKhanal In C, you have to declare functions (and variables) before you can use them. Note that a definition is also a declaration, although (for globals, at least) it is often a good idea to have a declaration as well (less important for statics). With GCC, I find `-Werror=missing-declarations -Werror=redundant-decls` to help enforce good style, although it may take a bit of effort to convert projects to survive that (mostly: add `static`, if you were already using headers appropriately) – o11c Aug 14 '21 at 05:01
  • The compiler does so from top to bottom and you have to declare functions you want to use before you are using them otherwise the function is not known. Nishant Bangera should have refactored the code such that it should work. – OneTrickDragon Aug 14 '21 at 05:01
  • 1
    At first i was placing is behind the main. Later i placed it above main. It worked. Thank you. – Neetesh Khanal Aug 14 '21 at 05:01
  • Good, you are welcome. I think you should accept an answer to close this Qand A – OneTrickDragon Aug 14 '21 at 05:13
  • in c it is allowed to declare a function in main. It will not give any error. –  Aug 14 '21 at 13:22