IOCCC Program compiling errors - c

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.

Related

Keep getting indentifier undefined error in C using any ide

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.

System calls not working in Atmel AVR Studio (with ASF)

I am not getting answers on the AVR Freaks forum and wonder if someone here could help me.
The answer might lie in this SO question, but I am not sure why it would be necessary.
Basically, I have my fist ever Atmel project (AVR studio 6, UC3 processor). The code compiles and links and I can load it to the Atmel board and step through in the debugger.
However, when I try to step over (or run until a breakpoint on the line after) a (valid) call to sprintf(), malloc() or memcpy() (there may be more, which I have not yet discovered), the IDE never returns to the next line of my code, just seeming to hang, or run forever.
[Note] Compiler optimization is off
Do I need to set some linker options (e.g link static (which I tried & it didn't help)? Or build with some library?
What confuses me is that the code compilers and links - what is being linked when I call these standard functions? If I need something else I would expect a compiler or linker error, but get none - so why won't my code run?
Sorry for such a stupid n00nb question, but it is my first micro-controller project.
I discovered that the CPU on my board is an Engineering Sample and not supported by Atmel Studio without a new io.h file.
I sort of figured that out from this question: http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=106652
Sorry to have troubled you.
what is being linked when I call these standard functions?
The AVR-libc, the implementation of the C standard library ported to the AVR platform.
so why won't my code run?
Compiler errors and runtime errors are not even related. Both of these lines are valid C and they compile, however, on most systems, I'd expect them to dump core:
int x = 1 / 0;
*(int *)0 = 41;
So it might be either:
a bug in the standard library (very unlikely), or
a bug in the online debugger (very unlikely), or
maybe you just expect something that is not supposed to happen?
Instead of trying to step over, what happens if you set a breakpoint at next line after the line you want to step over?
Also, does the operation change if you turn off compiler optimization?

How to disable warnings when compiling C code?

I am working on 32-bit Fedora 14 system. I'm compiling my source code using gcc.
Does anybody know how to disable warnings while compiling c code?
EDIT: Yes i know. Best thing is to fix those Warnings to avoid any undefined/unknown behavior. But currently here, i have written huge code first time and there are lots of error & warning in first compilation. Here i just want to concentrate on errors first and then i will see warnings.
try to add -w option when compiling
http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
Every body tells use -Wall switch with gcc, but you want to disable it. It is not advised, Use debugger to find it.
Linus Torvalds:
"But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
The best is to find the problem. It will prevent you in future looking for errors, which would not have occured, if you fixed the actual one.
But, if you're sure there is no bug or you have assured the problem is caught by your code, place this somewhere in the file (where 177 the number of your warning is):
#pragma diag_suppress 177 // supress #177-D function was declared but never referenced
let say you are getting warning -Wfoo-bar try to add compilation flag -Wno-foo-bar

Using XCode to learn C with K&R, but getting too many errors

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.)

Any Tools to Catch Silly Mistakes in C Code?

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"

Resources