Problems with running C code in Visual Studio Express 2012 [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I installed VS Express 2012 to learn C language in my University. I create new empty project then add new item to Source Files folder and change Source.cpp to Source.c
"Hello World" runs without problems but when I write simple "for" loop like this:
#include <stdio.h>
#include <stdlib.h>
int main() {
for (int i = 0; i <= 6; i++)
{
printf("the i value is: %d\n", i);
}
getchar();
return (0);
}
It writes me lots of errors:
------ Build started: Project: cTest, Configuration: Debug Win32 ------
Source.c
e:\ctest\source.c(7): error C2143: syntax error : missing ';' before 'type'
e:\ctest\source.c(7): error C2143: syntax error : missing ')' before 'type'
e:\ctest\source.c(7): error C2065: 'i' : undeclared identifier
e:\ctest\source.c(7): warning C4552: '<=' : operator has no effect; expected operator with side-effect
e:\ctest\source.c(7): error C2059: syntax error : ')'
e:\ctest\source.c(8): error C2143: syntax error : missing ';' before '{'
e:\ctest\source.c(9): error C2065: 'i' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Please help me solve this problem.

Shown in IdeOne,
error: ‘for’ loop initial declarations are only allowed in C99 mode
note: use option -std=c99 or -std=gnu99 to compile your code
I think, this clears your doubt. Declare i at the beginning of the function.
The correct code would be like this at WorkingExample.
From C99 Wiki, this is a new feature that was not available earlier.
Intermingled declarations and code: variable declaration is no longer
restricted to file scope or the start of a compound statement (block),
similar to C++
About VC++ compiler, direct from Mr. Herb Stutter's mouth,
Implements Standard C90 exactly (using /TC or naming files as
something.c)

This happens because your code does not obey to the current C standard under which you compile the code. Declaring int i for loop is allowed in C99 standard. Compile with -std=c99.

Related

Compiler error in c function for c166 16 bit microprocessor

Currently, I am trying to resolve several issues in a REALLY old c166 16bit microprocessor code. I have basic knowledge with c but not with microprocessors or c166, therefore I have zero clue what the compiler is trying to tell me. Maybe some of you guys know what is going on here / aka help me to resolve this compiler error:
Code
void ChecksummenBerechnenFar(word wAnzahlByte, byte far* bP_AnfangSpeicher)
{
word wChecksum;
word wCount;
for(wChecksum=0xFFFF, wCount=0; wCount < (wAnzahlByte-2); wCount+=1 ) {
wChecksum -= *bP_AnfangSpeicher++;
}
*bP_AnfangSpeicher = (byte) (wChecksum & 0x00FF);
bP_AnfangSpeicher++;
*bP_AnfangSpeicher = (byte) (wChecksum >>8);
}
Error
compiling CHECKS.C...
\CHECKS.C(8): error C25: syntax error near 'wAnzahlByte'
\CHECKS.C(9): warning C35: 'ChecksummenBerechnenFar': uses old-style declarator
\CHECKS.C(10): error C25: syntax error near 'wChecksum'
\CHECKS.C(10): warning C34: 'wChecksum': missing declaration specifiers
\CHECKS.C(11): warning C34: 'wCount': missing declaration specifiers
\CHECKS.C(11): error C42: 'wCount': not in formal parameter list
\CHECKS.C(12): error C25: syntax error near 'for'
\CHECKS.C(12): error C25: syntax error near '='
\CHECKS.C(12): error C25: syntax error near '<'
\CHECKS.C(12): error C25: syntax error near '-'
\CHECKS.C(12): error C25: syntax error near '+='
\CHECKS.C(13): error C25: syntax error near '-='
\CHECKS.C(13): error C25: syntax error near '++'
\CHECKS.C(16): error C67: 'byte': undefined identifier
\CHECKS.C(17): error C25: syntax error near '++'
\CHECKS.C(18): error C53: redefinition of 'bP_AnfangSpeicher'
\CHECKS.C(18): error C67: 'byte': undefined identifier
\CHECKS.C(20): error C25: syntax error near '}'
\CHECKS.C(20): error C7: compilation aborted
You seem to be missing a header file inclusion. Some data type might have been renamed using typedef somewhere.
If you are on a Linux host, I recommend installing "ctags" to browse through your code to find definitions and declarations.
For now you could do a grep in your source code directory
"find -name *.h | grep -rn typedef | grep word"
This will give you a file name and line number. open the file location and go to the line number check if that's the definition you are looking for.
If yes then include that file.
Commenters have mentioned that word and byte are not standard C and that’s true. But you aren’t seeing “undeclared identifier” errors.
I think that somehow the symbols word and byte have been #defined as empty strings. Maybe the #defines try to choose appropriate definitions based on the architecture you’re compiling for, and since you’re on a new architecture that it doesn’t recognize, they get converted into blank strings. You might be expected to pass in some kind of value via the -D switch that identifies the target architecture, especially if it’s different from the one you’re compiling on.
With the types replaced by empty strings, the compiler thinks you’re using the ancient K&R function definition syntax, and that’s why you’re seeing these errors.
Try compiling with -E to dump the preprocessor output and see what you get.

Build Apache APR 1.7 with Visual Studio (2017), IF_NAMESIZE undefined

Trying to build apr 1.7 on Windows with Visual Studio, tried 2017 and 2013.
When building the project (Debug|x86) or compiling e.g. dso.c if get the following error message:
1>dso.c
1>c:\dev\log4cxx\apr\include\arch\win32\apr_arch_misc.h(503): error C2143: syntax error: missing ')' before '*'
1>c:\dev\log4cxx\apr\include\arch\win32\apr_arch_misc.h(503): error C2143: syntax error: missing '{' before '*'
1>c:\dev\log4cxx\apr\include\arch\win32\apr_arch_misc.h(503): error C2059: syntax error: ')'
1>c:\dev\log4cxx\apr\include\arch\win32\apr_arch_misc.h(503): error C2061: syntax error: identifier 'apr_winapi_pfn_if_indextoname'
1>c:\dev\log4cxx\apr\include\arch\win32\apr_arch_misc.h(503): error C2059: syntax error: ';'
1>c:\dev\log4cxx\apr\include\arch\win32\apr_arch_misc.h(503): error C2513: ' ': no variable declared before '='
1>c:\dev\log4cxx\apr\include\arch\win32\apr_arch_misc.h(503): error C2065: 'apr_winapi_pfn_if_indextoname': undeclared identifier
1>c:\dev\log4cxx\apr\include\arch\win32\apr_arch_misc.h(503): warning C4047: '=': 'int' differs in levels of indirection from 'int *(__cdecl *)(NET_IFINDEX,PCHAR)'
1>c:\dev\log4cxx\apr\include\arch\win32\apr_arch_misc.h(503): error C2146: syntax error: missing ';' before identifier 'apr_load_dll_func'
1>c:\dev\log4cxx\apr\include\arch\win32\apr_arch_misc.h(503): error C2100: illegal indirection
1>c:\dev\log4cxx\apr\include\arch\win32\apr_arch_misc.h(503): error C2064: term does not evaluate to a function taking 1207 arguments
1>c:\dev\log4cxx\apr\include\arch\win32\apr_arch_misc.h(503): warning C4033: 'apr_winapi_if_indextoname' must return a value
I have succesfully build version 1.65 (after apply the hint from http://letcoderock.blogspot.com/2017/09/build-log4cxx-trunk-on-windows-by.html or Apache Cross Compilation Error ./gen_test_char: cannot execute binary file)
Comparing apr_arch_misc.h of the two version, i have noticed DLL_IPHLPAPI was new to the enum apr_dlltoken_e and Intelisense was complaining about
APR_DECLARE_LATE_DLL_FUNC(DLL_IPHLPAPI, PCHAR, NETIOAPI_API_, if_indextoname, 0, (
NET_IFINDEX InterfaceIndex,
PCHAR InterfaceName),
(InterfaceIndex, InterfaceName));
with "expecting a ')'".
If have changed NETIOAPI_API_ to WINAPI. Do not know if this is correct but the file could now be compiled (after running the above trick with gen_test_char.exe).
But after that I ran into the next Problem:
1>sockaddr.c
1>network_io\unix\sockaddr.c(144): error C2065: 'IF_NAMESIZE': undeclared identifier
1>network_io\unix\sockaddr.c(144): error C2057: expected constant expression
1>network_io\unix\sockaddr.c(144): error C2466: cannot allocate an array of constant size 0
1>network_io\unix\sockaddr.c(144): error C2133: 'scbuf': unknown size
1>network_io\unix\sockaddr.c(1274): error C2065: 'IF_NAMESIZE': undeclared identifier
I have succesfully build apr with gcc in linux/cygwin. I have looked into the the preprocessor substitutions for IF_NAMESIZE and here it is replaced by 44. But I can not find the definiton or where this is set.
Does anyone have an idea how to fix this? And is the above change to WINAPI correct?
Just use cmake to build the Visual Studio Solution:
With
cmake -G "Visual Studio 15 2017" -A x64
the .sln ist properly set up and building the solution works with no errors. Even gen_test_char.exe is build.
Maybe the apr project should make the cmake building instructions a little bit more prominent on their site.
As written in README file:
Note you must manually modify the include\apr.hw file before you
build to change default options
So i just removed line #define _WIN32_WINNT 0x0501 from apr-1.7.0\include\apr.hw
and run nmake -f Makefile.win. Worked fine for me. VS2017 tools, Windows 10.
In case someone else also runs into this problem: I was using cmake + nmake to build APR, but it still didn't work for me.
The reason was that I had old compilation flags to support Windows XP:
cmake -DMIN_WINDOWS_VER=0x0501 ...
Investigating Iphlpapi.h, it seems that netioapi.h and other networking headers used by the current version of APR are supported only starting from Vista onward:
#if (NTDDI_VERSION >= NTDDI_VISTA)
#include <netioapi.h>
#endif // (NTDDI_VERSION >= NTDDI_VISTA)
So in order for the compilation to work, just remove that cmake option or specify a value higher than 0x0600 (the equivalent for Vista).

TensorFlow in C: Why compilation error in hello_tf.c ? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I was trying to install TensorFlow for C on Ubuntu. I followed all instructions in the page : https://www.tensorflow.org/install/install_c
Please see the commands and results below. Please let me know why I am getting compilation error:
hadoopuser#sarkar-D900C:/home/sarkar/tensorflow$ gcc hello_tf.c
hello_tf.c: In function ‘main’:
hello_tf.c:5:3: error: stray ‘\342’ in program
printf(“Tensor flow C library version %s\n”,TF_Version());
^
hello_tf.c:5:3: error: stray ‘\200’ in program
hello_tf.c:5:3: error: stray ‘\234’ in program
hello_tf.c:5:13: error:‘Tensor’ undeclared (first use in thisfunction)
printf(“Tensor flow C library version %s\n”,TF_Version());
^
hello_tf.c:5:13: note: each undeclared identifier is reported only
once for each function it appears in
hello_tf.c:5:20: error: expected ‘)’ before ‘flow’
printf(“Tensor flow C library version %s\n”,TF_Version());
^
hello_tf.c:5:20: error: stray ‘\’ in program
hello_tf.c:5:20: error: stray ‘\342’ in program
Thanks,
S Sarkar
After some research I found this: gcc stray errors
The reason for the errors are the quotation marks. A possible fix would be to replace them.
Best regards.
Just change all the “ with ".

Visual C++ 2010/2008 does not accept `__int64` in C mode

I have reduced the problem to these lines:
typedef __int64 int64;
inline int64 qatoll(const char *nptr) { return _atoi64(nptr); }
When compiling in C mode with VC++2008 or 2010 I get errors:
t.c(2) : error C2054: expected '(' to follow 'inline'
t.c(2) : error C2085: 'qatoll' : not in formal parameter list
t.c(2) : error C2143: syntax error : missing ';' before '{'
In C++ mode it does accept the function definition and only complains about _atoi64.
What is the problem and how can I fix it?
Visual C++ 15 compiles it without errors, so is this related to C99 support?
It is not the __int64 which is the problem, but the inline syntax which should be _inline.
I just tried similar code on an older machine running MSVC 2008 to verify this.

Why gcc splits error message along several lines? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Sometimes gcc displays a single error/warning message in several lines.
Not a big deal, but it looks a little strange (and ugly) to me.
server_inc.c: In function ‘prepareForConn’:
server_inc.c:101: error: ‘filename’ undeclared (first use in this function)
server_inc.c:101: error: (Each undeclared identifier is reported only once
server_inc.c:101: error: for each function it appears in.)
Specially because the location (server_inc.c:101: error:) is prepended, so it looks as if there were many errors.
Is there some way to change this? I'm using gcc 4.4.7 on Linux.
Starting with GCC 4.5, the second message appears on a single line, and is prepended by "note" rather than "error":
$ cat test.c
int main() { x=3; }
$ gcc-4.5.4 -c test.c
test.c: In function ‘main’:
test.c:1:14: error: ‘x’ undeclared (first use in this function)
test.c:1:14: note: each undeclared identifier is reported only once for each function it appears in
With GCC 4.4, I can reproduce the behaviour you're seeing, and that does look like a bug without any way to work around it. If possible, please upgrade your compiler.
Probably because default terminal size has a historical width of 80 columns, see this question.
Given this, GCC has a behavior which by default could follow this rule, taken from here:
-fmessage-length=n
Try to format error messages so that they fit on lines of about n characters. The default is 72 characters for g++ and 0 for the rest of the front ends supported by GCC. If n is zero, then no line-wrapping is done; each error message appears on a single line.
You could specify -fmessage-length=0 to remove line wrapping.

Resources