Hello. I get errors trying to compile C source written and working in DevC++. When I paste it in Visual Studio 6.0 it shows strange errors and won't compile. Can you help me with this?
Hello. I get errors trying to compile C source written and working in DevC++. When I paste it in Visual Studio 6.0 it shows strange errors and won't compile. Can you help me with this?
What errors do you get?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
It would help us if you give us the exact error messages, and perhaps a short bit of code to show the problem. Yes, there are differences between for exampel Visual Studio 6.0 and gcc that is used behind the scenes in Dev-C++. Sometimes that's simply that one has extensions that the other one doesn't support, for example. Also VC 6 may have "issues" when it comes to standard compliance - or so I've heard.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Here are examples.
The code:
gives errorCode:for(k = 0; k <= num_people; k++)
although I have declared the identifier as int k; before the for loop.Code:test.c(190) : error C2065: 'k' : undeclared identifier
with errorCode:strcpy(name[j], temp);
and errorCode:test.c(183) : warning C4024: 'strcpy' : different types for formal and actual parameter 2
The codeCode:test.c(178) : error C2106: '=' : left operand must be l-valuehas errorCode:int i,j,k,count;but there's no error in fact.Code:test.c(167) : error C2143: syntax error : missing ';' before 'type'
There are some more errors and warnings.
Well, one way to find the problem is to write the smallest and simplest program that compiles on Dev-C++'s compiler yet fails to compile on MSVC6. Post that here with the error messages, and we'll see what's wrong.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
laserlight, do you think it might be something with the encoding of the file?
I don't think it's the encoding. However, I suspect that if you post the code around this point:
we will be able to advice you further.Code:test.c(167) : error C2143: syntax error : missing ';' before 'type'
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Code:if(num_people == 0){ printf("No people records."); return 0; } int i,j,k,count; // this is test.c (167) error C2143: syntax error : missing ';' before 'type' float average_age = 0; int sum_ages = 0; char temp[30]; int temp_ages[30]; for (i=0; i<num_people; i++){ for (j=i+1; j<num_people; j++){ if (strcmp(name[i], name[j]) > 0){ strcpy(temp, name[i]); temp_ages[i] = age[i]; strcpy(name[i], name[j]); age[i] = age[j]; strcpy(name[j], temp); age[j] = temp_ages[i]; } }
In standard C (as opposed to extensions in gcc) you have to declare all variables at the begining of the block. You have code first, then variable declarations. - you could work around it with something like:
If you compile in gcc using the "-ansi" switch to tell the compiler that you only want code that is compliant with ISO C90 standard, rather than "gcc compliant" which is the default.Code:if(num_people == 0){ printf("No people records."); return 0; } { int i,j,k,count; // this is test.c (167) error C2143: syntax error : missing ';' before 'type' float average_age = 0; int sum_ages = 0; char temp[30]; int temp_ages[30]; for (i=0; i<num_people; i++){ for (j=i+1; j<num_people; j++){ if (strcmp(name[i], name[j]) > 0){ strcpy(temp, name[i]); temp_ages[i] = age[i]; strcpy(name[i], name[j]); age[i] = age[j]; strcpy(name[j], temp); age[j] = temp_ages[i]; } } }
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp, is there any way to tell the MSVC 6.0 compiler to compile with "-ansi" - as I understood that's causing the errors.
Surrounding with braces, as you said, works - the error in line 167 is gone, so is it possible to change compile parameters in MSVC 6.0 so it'd behave like DevC++'s compiler?
No, the problem is that you are using extensions beyond "ansi" that are only available in gcc. You could change it to compile as C++ which allows these sort of constructions, but chances are that other things that are different between C++ and C will trip you up - and that can get very tricky to solve if you don't understand those differences.
If you then use -ansi on your gcc compile, it will let you know when you've "broken" things.
My actual suggestion is that you move your variable declarations to the beginning of the function. That should work in ALL compilers [that have been produced in the last 10 years or so] with no problems.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
The problem is that it is gcc that has the extension, not MSVC6. So the solution is to change your code to comply with the standard, upon which it will work on both compilers.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Ok, now I understand. But what about errors like
How could I understand what's causing these errors/warnings?Code:test.c(147) : error C2143: syntax error : missing ';' before 'type' test.c(116) : warning C4047: 'function' : 'struct _iobuf *' differs in levels of indirection from 'int ' test.c(150) : warning C4024: 'fprintf' : different types for formal and actual parameter 1 test.c(74) : warning C4047: 'function' : 'const char *' differs in levels of indirection from 'int ' test.c(129) : error C2275: 'FILE' : illegal use of this type as an expression
Sounds like more of the same, but without seeing the code that those errors refer to, it's very hard to say. Errors don't mean much without the actual code...
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
This is not properly indented code.
I don't suggest you indent so much as you did with your if. And all ending } should be on the same level as the block element - such as the if or while or whatever it is you're using.
And you're ALSO mixing spaces and tabs. Not very good. Usually messes up code outside your editor. Use only spaces or only tabs - it's for the best. Recommend tabs.
Your if is also not on the correct level. It should be on the same level as all the other code inside the function. No code except function should appear without any preceding spaces or tabs.