Variable declaration in for loop - Intellisense stuck with C89 standard? - c

I have a C project which i open in VisualStudio 2019 with the "Open folder" option. I added a CppProperties.json to get Intellisense running, but now it throws errors if i try to declare a variable in a for loop like this:
for (int i = 0; i < 10; i++)
{
}
error: E0029 expected an expression
error: E0020 identifier "i" is undefined
I searched StackOverflow already and found this thread where someone mentions that the declaration of variables in for loops was added in the C99 standard.
In the VisualStudio documentation it says you should add "-std=C99" to your project settings, but i have no project file (because I opened it with the "Open Folder" option) and therefore no project settings.
Is there a way to tell Intellisense what C standard to use anyways ?
Thanks in advance.

Found it: Just add
"compilerSwitches": "-std=c99",
to your CppProperties.json.

Related

VSCode not importing functions from other files in C language

I am going through a C course and got to the point where we #include "myfile.h"
Good news : VSCode finds "myfile.h" and can pull variables from it, such as int myvar=10;
Bad News : VSCode does not seem to identify function definition in the "myfile.c", so extern in myfunction() is seen but not defined.
This results in the following
int i = myfunction();
Compilation Error: undefined reference to 'myfunction';
How can I get VSCode to recognize and use "myfile.c"?
Answer found in ::
undefined reference error in VScode
Basically, I had to compile all my files at once. This required me going into the tasks.json file and modifying it from ${file} to ${workspaceFolder}\*.c
This is better explained in ::
https://code.visualstudio.com/docs/cpp/config-linux

Visual Studio Open Folder GCC (Cygwin) Intellisense Errors

I'm trying to configure Visual Studio Community 2019 for use as the IDE for work on an open source C project targeting the Nintendo 64.
The makefile seems to define a gcc-derivative (mips64-elf-gcc) as the compiler and has -std=gnu99 as one of the CFLAGS, so I assume that means this is C99.
I don't know much about C development, so I initially followed a tutorial, which directed me to set up Cygwin. For a time I edited the *.h and *.c files in Notepad++ and then used the Cygwin terminal to compile everything (there's probably a thousand better ways, but I'm a C/*nix nub and just wanted to get something off the ground quickly). Notepad++ was ok just to tinker around with things, but I'm getting more serious about this project and doing any real work in Notepad++ is a real bother. I've used Visual Studio for a long time, so I figured I'd try getting things set up there, since I know it has support for C/C++.
I've successfully gotten VS to run my makefile using the "Open Folder" feature and by wiring up the requisite data within the CppProperties.json and tasks.vs.json files. While attempting to get IntelliSense working, however, I've run into a few errors that I can't resolve.
1. 'duplicate parameter name' in stdio.h & string.h
There are several instances of this error. The problem seems to be lines like this one:
FILE * freopen (const char *__restrict, const char *__restrict, FILE *__restrict);
As you can see, there are multiple "parameters" all named __restrict. I'm still just beginning my C journey, so I'm not entirely sure what this is, but my suspicion was that it wasn't a parameter name, but might be #defineed somehwere. Using the Agent Ransack file search utility, I looked for instances of #define __restrict and did find some. I added those paths to the INCLUDE section of my CppProperties.json file, but that didn't help at all.
According to the VS Open Folder tutorial, IntelliSense can sometimes get hung up on preprocessor directives, so I added the recommended cpp.hint file, closed VS, deleted cached data in the .vs folder, and started VS back up, but the errors still persist.
2. "expected a ')'" in stdio.h
I assume this one is also related to that __restrict issue.
FILE * fopen (const char *__restrict _name, const char *__restrict _type);
^
| expected a ')'
It seems to be unhappy about the _name parameter for some reason.
3. 'expected an expression / identifier "i" is undefined' in my for loops
Any place in the code I've been working on where a for loop is defined, I get these errors. Here's an example of the errors:
void v(void)
{
for (int i = 0; i < 25; i++)
{ ^ ^
| | identifier "i" is undefined
|
| expected an expression
}
}
If, however, I change the loop to the following, everything is fine.
void v(void)
{
int i;
for (i = 0; i < 25; i++)
{
}
}
It's not liking the int type declaration in in the init-expression of the for loop. I'm not sure what I could've done in configuration to break a core language feature. I assume something about the previous errors are affecting this.
None of this affects the build process, since all of that is still being done through the makefile using everything I have set up in Cygwin, so I guess it isn't critical to fix these issues, but the whole point of doing this was to have a nice environment to work in and having 61 errors constantly hanging out is probably gonna put a pretty big damper on that situation.
I've tried everything I can think of to resolve these issues and nothing has worked. This is a particularly difficult issue to google, due to the large number of potential contributing factors required as search terms, so that hasn't proved fruitful.
Any help or advice is appreciated.

Error C4576 in VS2015 enterprise

I have the error C4576 in Visual studio 2015 when I tried to compile the file: transcoding.c.
The source code of this file is here: transcoding.c
error C4576: a parenthesized type followed by an initializer list is a non-standard explicit type conversion syntax
The error arise at line 127 in this instruction:
enc_ctx->time_base = (AVRational) { 1, enc_ctx->sample_rate };
I used the source of ffmpeg in my project
https://www.ffmpeg.org/download.html
I searched around for a solution but I'm not able to correct the error
If someone have found something similar, please provide an idea
Despite what some other answers incorrectly claim, VS2015 compiler provides comprehensive support for C99 features, including the compound literal feature you are trying to use in that problematic line.
One possible explanation for the error message is that it the source file, despite being named as .c file, is being compiled as C++ file. The project settings might explicitly request C++ compiler for this file. In C++ this code is invalid.
Check your compilation settings to see if it by any chance includes a /TP ("compile as C++") switch.
Old question, but...
The solution is pretty simple:
AVRational tb;
tb.num = 1;
tb.den = enc_ctx->sample_rate;
enc_ctx->time_base = tb;
or
enc_ctx->time_base.num = 1;
enc_ctx->time_base.den = enc_ctx->sample_rate;
Remove the parenthesis around the type in the macro definition.
That should work.
enc_ctx->time_base = AVRational { 1, enc_ctx->sample_rate };
Looks like a question where the C and C++ tags make sense. You're trying to compile C99 code with a C++ compiler. That doesn't work.

VC++: fprintf error in release mode

I'm working in visual studio 2010 and I have code written in C.
If i run in 'Debug' mode, the code will run without any error or warning.
But if I run the same in 'Release' mode, errors and warning will appear.
Error List:
warning C4013:'fprintf' undefined; assuming extern returning int
error C2065: 'stdout': undeclared identifier
What is the reason? please help
warning C4013:'fprintf' undefined; assuming extern returning int
It looks like you didn't include stdio.h.
Seems like a difference in configuration between Release and Debug.
Check the values for "Whole Program Optimization", they may differ between the two build configurations.
I had the same problem in reverse: In Release mode everything was fine, but in Debug mode some functions like 'ext' (FFTW library) were reported as "undefined; assuming extern returning int".
The failing build configuration (Release in my case) had under project properties Configuration Properties > General the option "Whole Program Optimization" set to No Whole Program Optimization.
The successful build configuration had this set to Use Link Time Code Generation. When I set that option in my failing target as well, everything worked fine.

Visual Studio: Create a Hello World app in C?

How can I create a basic C app in Visual Studio, be it 2010 Ultimate or 2008 Professional? I have searched through the project templates, and can find plenty for C++, but none for C.
(I'm hoping that the compiler and debugger will be built in.)
New project/Win32 Console Application/Empty project.
Add a file called "hello.c" (important that it's .c)
Type out a basic hello-world:
#include <stdio.h>
int main()
{
printf("Hello world\n");
return 0;
}
Compile, execute... PROFIT!
Visual Studio doesn't have a separate compiler for C, it uses the C++ compiler for C code. You can tell it to restrict itself to legal C syntax by using a compiler switch or by renaming the .cpp file to .c
Edit: I'm still using 2005 on this machine, so it might not be in the same place, but try this
Right click on the main.cpp in the solution explorer pane (on the right).
Choose Properties (bottom of the menu)
Open up the C/C++ group of properties
choose Advanced page
Change "Compile As" property to "Compile as C Code (/TC)"
In addition to changing the file extension, you may need to change the compiler settings:
Settintgs of Application->C/C++->addition->compiler such...
You must take /TC for basic C and /TP for C++.
https://msdn.microsoft.com/ru-ru/library/032xwy55.aspx
Good Luck.

Resources