I was trying to play with pointer syntax and I didn't get why I was getting some of the errors I've got. Then when I've tried to compile it with gcc it worked. Is there a way to fix this in NetBeans? It's really annoying to work with false errors...
The code in the picture is:
int (*(*f3(int z))(double ))(float ){
return NULL;
}
This is broken as well yay! :
char (* ( *f())[])(){
return NULL;
}
It seems that cdecl is better in parsing C that this IDE, embarrassing...
This seems to be a bug in Netbeans, there's nothing you can do about it except for reporting it (or fixing it yourself). I think you can't even completely turn off these error indications in Netbeans.
Netbeans has a lot of bugs like this, and other IDEs like Eclipse and Code::Blocks are no better. Although I would think that parsing entered code is the single most important core functionality of an IDE that distinguishes it from an ordinary text editor, they fail surprisingly often when you write some more complicated constructs or use newer language features.
Related
code: http://www.ioccc.org/1988/phillipps.c
How do i run this on Coderunner?
I've encountered some compiling problems that i can't solve after searching on the internet.Can someone help me out?
If I am not being informative enough, i'm sorry, but please tell me how i can do better at asking these tech questions.
compiling errors below(i use coderunner)
The program is written in an old dialect of C and is relying on some features now considered broken. Clang (the compiler in question) is not happy about the third argument of main and I don't think you can convince it to accept that.
You can either install gcc, that compiler will accept the code with just warnings. But I don't think coderunner has gcc integration.
Or you can manually unscrew the objectionable bit of the code.
Replace all instances of the word main with mayn in the code and add this bit of code to the beginning of the file:
main() {
mayn(1,0,0);
}
Now you can enjoy the program under clang/coderunner as well.
So this is my first post. I am preparing for some upcoming college courses on programming, one of them deals with C. I have been teaching myself for a little while and have never encountered this problem before, even though I have used the same code. I am having the issue on any ide that I use, which are MS Visual Studio 2019, and Codeblocks, both 16.01 and 17.12.
The problem is when I try to include an inline function, the compiler brings up an error: identifier "keep_window_open" is undefined, as well as the error: 'keep_window_open':identifier not found.
This never happened until I upgraded Codeblocks from 16.01 to 17.12, so that leads to believe something happened with the compiler or some settings that I don't know about, or how to change.
I have tried going back to Codeblocks 16.01, but still get the same error. All my previous programs, that use the exact same code still compile and run, so I am at a lost and completely frustrated. Enough so to think about changing my major, which I really do not want to do. I enjoy programming. But this problem is killing me.
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
inline void keep_window_open() { char ch; scanf("%s", ch); }
int main()
{
printf ("Lets see if this stupid thing works\n");
keep_Window_open();
return 0;
}
The problem is the line of code:
keep_Window_open();
If I comment it out, the program will compile and run, however I can't see it, because all it does is flash and then close the console window. And I need to use this code to see the output window. As I have said I have used this code in many other programs, most of which are much more complex than this, and they all still compile and run. It is only on new projects that I try to create.
I expect the window to stay open until the user presses and enters a key, like it always has before. I am stuck, and would appreciate some input on how to proceed, other than throw myself in front of a bus.
OK, so I was able to solve the issue in CodeBlocks, but not in Visual Studio. I changed the code to have the inline function below the main function, and now when compiled and run in CodeBlocks (17.12) it works exactly like it should, or how I thought it should.
However when I try the same solution in Visual Studio, after putting the inline function below the main function I now get a new error:
'keep_window_open' redefinition basic types
Since I usually use CodeBlocks for C, Visual Studio for C++ and intellij for java, I am not going to worry about this, as long as it works in CodeBlocks I should be OK. However I am curious as to why it work in one IDE and not the other?
I am new to this programming thing, and there is much to learn, and I know virtually nothing about compilers and linkers, as I am focusing on programming techniques and syntax in several different languages.
I am writing a simple C editor. I have to validate a code to highlight any misspellings, missing semicolons, uses of non-existing functions/variables/methods, assigments in if condition and so on and so forth.
Parsing and validating C is a very complex problem so I have decided to use CDT. However, I have no idea how to do so.
I have only found informations about method org.eclipse.cdt.core.dom.ast.gnu.c.GCCLanguage.getASTTranslationUnit(...) but this is not helping very much, because it allows to find only basic syntax errors. (Am I right?)
I need a function which gets a C code or an object of the class IASTTranslationUnit. It has to return list of all problems (errors and warnings). How can I do that, using the CDT API?
Many categories of errors can be checked by resolving names found in the AST (calling IASTName.resolveBinding()), and seeing whether the resulting binding is an IProblemBinding.
See how CDT's ProblemBindingChecker does this to show many errors in the editor.
Note that this wonn't catch all errors; you can look at CDT's other checkers for ideas about how to catch other categories of errors (some of the checkers also produce warnings).
However, even all of CDT's checkers put together won't diagnose of all the errors and warnings that a compiler would. If that is a requirement, I would suggest using an actual compiler's internals, such as libclang.
I'm want to learn C programming with K&R using XCode, but I can't even get the Hello World to work right - it's giving me errors it shouldn't, I guess because it's being very technical. Can I get XCode to relax on requirements? Would greatly appreciate some advice! Thanks.
Xcode Bah! Just compile from the command line:
gcc myfile.c -o myfile
You don't need Xcode, but if you want to ... Xcode has hello world built in. Make a new project "Command Line Utility", "Standard Tool", give it a name, have a look in "Source", and you'll see the Hello World program:
#include <stdio.h>
int main (int argc, const char * argv[]) {
// insert code here...
printf("Hello, World!\n");
return 0;
}
Click "Build and Run" and it goes, you see:
Running…
Hello, World!
Debugger stopped.
Program exited with status value:0.
I will agree with ergosys above. Xcode is realistically made for Objective-C coding, and even its "Command Line Utility" template is sheer overkill. Anything in K&R will compile flawlessly with
gcc -Wall filename.c
except maybe some of the more advanced stuff. K&R doesn't go into object compiling or linking extensively, since it's only meant to teach you the language. Grasp compilation though, and learning C will be much easier. I much prefer using Makefile's or gcc/g++ than IDE's like Xcode or Eclipse.
I'm gonna assume that XCode is demanding Ansi Standard C, while you are using an early edition of the K&R book, whch still uses the old style.
Unless you are doing this just so you can interprete some ancient C code, don't bother with the old style. Use the Ansi Standard syntax.
Honestly, XCode is way more than you need if using K&R. K&R is about basics, XCode is about making you pull your hair out.
I actually prefer to use the command line instead of an IDE on the Mac.
At least for K&R, using the command line is the way you want to go. Try:
gcc -o outputfile code.c
If you want to get into iPhone/iPod or Cocoa applications, then use Xcode. But more times than most, XCode is overkill and will probably just slow you down.
As I said in my comment, you shouldn't see an error from Xcode with the hello world program from K&R. You might be seeing some warnings, or you have mistyped something. Remember to select the correct language for compiling (you want C, of course).
Can I get XCode to relax on requirements?
In general, you want the compiler to be able to warn you for things that it thinks are not "right". In some cases, the warnings are harmless, and can be turned off, but in most of the cases, the warnings expose issues with your program. I like compiling my code with the maximum warnings enabled for example, because it saves me a lot of time later.
If you are getting errors, you should post (copy-paste) your code here.
Finally, look at the errata page for K&R. It has some bug fixes or corrections, but nothing for hello world.
(It could be that you're using the first edition of the book, in which case you shouldn't use that book to learn C.)
I had a nasty typo that wasted my time and my colleague's time, it was something like this:
for (i = 0; i < blah; i++); // <- I had a semi-colon here, that's the bug!
{
// Some awesome logic here
}
First of all, it's very embarrassing, second thing, I should never repeat this. I'm relatively new to C. In Java, I guess I can use FindBugs to catch errors like these, what tool should I use for C code? Lint?
Yes, PC-Lint is probably the best tool available.
In addition to Lykathea's PC-Lint suggestion, you can also get better (or at least more) diagnostics if you bump up the warning level of the compiler. Something like /W4 or -Wall
Though I'm not sure if your particular problem would have been caught with this (MS VC doesn't seem to flag it even with all warnings enabled). I think that's because it's not an uncommon idiom for for loops to be empty when the work is done as side effects of the loop control expressions.
A few things that have saved me in the past, from the top of my head:
Use if (3 == bla) rather than (bla == 3), because if you misspell and type (3 = bla) the compiler will complain.
Use the all-warnings switch. Your compiler should warn you about empty statements like that.
Use assertions when you can and program defensively. Put good effort into making your program fail early, you will see the weaknesses that way.
Don't try to circumvent any safeguards the compiler or the OS have put in place. They are there for your ease of programming aswell.
Also look at clang static analysis
I would start by learning about splint and gdb. If you need more advanced, build on these two tools. But they are a good start.
GCC has most of the functionality that Lint has had built in via the warning flags.
Any good GUI programming environment ("IDE" - Integrated Development Environment) like Eclipse would generate a warning in a case like that.
A good syntax highlighter will make some cases like this more visible.
I would suggest seeing if you have the ability to enforce MISRA standards. They were written with great thought and many rules that are simple for a compiler to check. For example, A rule I use requires all NOP commands have their own line. This means when you put a ; on the end of a loop statement it will through an error saying that it is not on it's own line.
QA·C by Programming Research is another good static analysis tool for C.
In this (old) version of How to Shoot Yourself In the Foot, and in many other versions around the web, C is always the language that allows for the simplest procedure. When programming in C, you have to remember this and be careful. If you want protection, choose another language.
This saying is attributed to Bjarne Stroustrup (C++) himself. To (mis)quote:
"C makes it easy to shoot yourself in the foot"