2

When I try to compile the following C code on Visual Studio 2017 with default C/C++ settings:

#include <stdio.h>
#include <stdlib.h>


/* function declaration */
/*Line 6*/ int max(int num1, int num2, int num3); 

int main() {

/* local variable definition */
int a = 100;
int b = 200;
int c = 400;
int ret;

/* calling a function to get max value */
ret = max(a, b, c);
printf("Max value is : %d\n", ret);
return 0;
}

/* function returning the max between two numbers */
/*Line 25*/ int max(int num1, int num2, int num3) {

/* local variable declaration */
int result;

if (num1 > num2)
result = num1;
else
result = num3;
return result;
}

I get the error(s):

Expected an Identifier: Line(s) 6,25

Expected a ";": Line(s) 25

Intellisense highlights those lines and wont let me run the code. Yet in Codeblocks (Using the default GNU GCC compiler, from mingW) this EXACT code compiles just fine. What is causing this?

Multiple sources have told me that its not due to Codeblocks using GCC compiler and Visual Studio using "cl" compiler by default.

The same sources have told me that it is also not due to the possibility of each IDE compiling the code using different C standards.

I have named the the file extension as ".c" and I get these errors

If I try to compile the code as c++(or as a ".c++" file it works, but that's not what I want. I want C.

I would prefer to use Visual Studio over Codeblocks due to its sleek look and menu layout. I also prefer the Visual Studio debugger.

What steps can I take to successfully compile this simple code on Visual Studio 2017?

Chris
  • 107
  • 1
  • 11
  • For C program it ought to be `int main(void)` at least. – alk Jun 24 '18 at 08:00
  • @Chris - Not related to your problem, but you might want to add a test case for `num2` being the largest number. Like `max(1, 2, 1);`. – Bo Persson Jun 24 '18 at 10:36
  • @alk, Why? "If no parameters are given, then the function does not take any and should be defined with *an empty set of parenthesis* or with the keyword void." https://www-s.acm.illinois.edu/webmonkeys/book/c_guide/1.3.html – purec Jun 24 '18 at 10:52
  • @purce: In C "*an empty set of parenthesis*" does **not** define a empty parameter list. This is just an essential difference between C and C++. The doc you link unfortunately seems to be inaccurate (to not say wrong) here. – alk Jun 24 '18 at 11:22
  • How can you pass anything to main()? – purec Jun 24 '18 at 11:50
  • @purec: By just doing it. You might like to gxxgle for K&R function definitions. – alk Jun 28 '18 at 16:50
  • @alk, 1. Doing what? 2. Thank you, dear sir. Search for "main(" this dark manual of programming and tell me what you see? – purec Jun 29 '18 at 08:12
  • @purec: Do pass in variables. See also: https://stackoverflow.com/a/1631781/694576 – alk Jun 29 '18 at 10:20
  • @purce: From the link in my last comment: "*`int foo();` it actually declares a function `foo` that takes 'an unspecified number of parameters of unknown type'. You can call it as `foo(2, 3);` and as `j = foo(p, -3, "hello world");`*" – alk Jun 29 '18 at 10:22
  • Dude, how can you pass anything to main()?(show me) K&R 2nd edition has plenty of foo(void) but not a single main(void). Stop confusing people about nothing at all. This is C... Don't need no... – purec Jun 30 '18 at 11:17

1 Answers1

14

Microsoft has a notoriously defined max macro. If the macro definition is pulled into your source for whatever reason, token substitution will wreak havoc. The result of that I'd wager is what you are seeing. Mostly because by your own admission, it happens only in Visual Studio.

The solution (and test for it) is fairly simple. One of these should "fix" your code:

  1. Rename max to something else, like my_max.
  2. Before including any headers, add #define NOMINMAX to suppress the definition of the macros in any included MS headers.

Beyond that you are gonna have to tinker with your project settings and see what may be improperly set. This is not a macro that should be automatically added in a simple console project.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458