-2

My Visual Studio compiler is showing me error messages even when the code is correct.

Even on a simple code like,

 #include<stdio.h>

int main(){
    int add(firstNumber, secondNumber){
        int result=firstNumber+secondNumber;
        return result;
    }
    int out=add(10,30);
    printf("%d", out);
    return 0;
}

It's showing the massage,

expected a ';', line 4

and

identifier "out" is undefined, line 9

The code runs well. But it's frustrating seeing those red lines and the error messages on my code.

Note: I'm using MinGW as a C path

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Swaflick
  • 17
  • 4
  • 3
    There's no way you have this code compiling. – sweenish Apr 26 '21 at 20:05
  • 3
    Nested functions (like your `add`) are not supported in *Standard* C or C++. Some compilers may support them as extensions. Try moving your `add` function to before, and outside, `main`. – Adrian Mole Apr 26 '21 at 20:05
  • 1
    **C and C++ are *different* programming languages.** I recommend to learn C, it is simpler. Read [*Modern C*](https://modernc.gforge.inria.fr/), [this C reference](https://en.cppreference.com/w/c), and the documentation of your C compiler. I heard that mingw is a variant of the [GCC](http://gcc.gnu.org/) compiler: invoke it with all warnings and debug info: `gcc -Wall -Wextra -g` – Basile Starynkevitch Apr 26 '21 at 20:07
  • 2
    I'll point out that, compiler extensions for nested functions aside, the parameter types are not identified. Is that also a compiler extension? – sweenish Apr 26 '21 at 20:09
  • 1
    And **your code above is *very* incorrect** – Basile Starynkevitch Apr 26 '21 at 20:10
  • @sweenish: yes. Ancient versions of C compilers assumed all variables without types were `int`, and some C and C++ compilers keep that as an extension. This was never part of any C standard though. – Mooing Duck Apr 26 '21 at 20:21
  • @MooingDuck That's atrocious. But good to know. – sweenish Apr 26 '21 at 20:23

2 Answers2

3

Neither C nor C++ Standard allows nested function definitions as in this program

 #include<stdio.h>

int main(){
    int add(firstNumber, secondNumber){
        int result=firstNumber+secondNumber;
        return result;
    }
    int out=add(10,30);
    printf("%d", out);
    return 0;
}

where the function add is defined within the function main.

Moreover the identifiers firstNumber and secondNumber in the parameter list do not have type specifiers.

You have to move the function definition outside main. For example

#include<stdio.h>

int add( int firstNumber, int secondNumber )
{
    int result = firstNumber + secondNumber;
    return result;
}

int main( void ){
    int out=add(10,30);
    printf("%d", out);
    return 0;
}

In C++ you could use a lambda expression instead of the function add. For example

#include <cstdio>

int main()
{
    auto add = []( int firstNumber, int secondNumber )
    {
        return firstNumber + secondNumber;
    };

    int out = add(10,30);

    printf( "%d\n", out );

    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

The problem is the function you defined in main, C doesn't support functions defined inside other functions. Your code should look like this.

#include <stdio.h>
    

int add(firstNumber, secondNumber){
    int result=firstNumber+secondNumber;
    return result;
}

int main(){
    int out=add(10,30);
    printf("%d", out);
    return 0;
}
selbie
  • 100,020
  • 15
  • 103
  • 173
Kookies
  • 75
  • 10