C programming help.

Post » Tue Aug 09, 2011 8:16 pm

Most compilers have an flag to enable all errors (-Wall on gcc if I remember rightly). It is worth running this flag.

I tend to do this out of habit: though it's sometimes annoyingly pedantic, it's a great way of finding potential logic errors of the type that can sometimes take days to track down otherwise...

As for declaring main() as void, that's something that I seem to have dredged from my memory that could've happened at any time in the last 25 years! I think I had a moment's uncertainty as I tend to mainly use BSD Unix for compilations these days and I'm not sure if that tends to rigidly follow the standards elsewhere or not (or vice-versa, depending).

And as regards implicit returns, at least you don't see something I discovered with a compiler (whose creator shall remain nameless) quite a few years ago that would fall through the bottom of a procedure and start executing the next one unless an explicit return statement was present. That was... disconcerting. And very broken.
User avatar
GRAEME
 
Posts: 3363
Joined: Sat May 19, 2007 2:48 am

Post » Tue Aug 09, 2011 10:52 pm

That's like a second language and I admire anyone that can understand it because when I look at that sort of thing all I think is

:wacko: :wacko: :wacko: :wacko: :gun: :brokencomputer: :ahhh: :ahhh: :ahhh: :ahhh:
User avatar
Elizabeth Falvey
 
Posts: 3347
Joined: Fri Oct 26, 2007 1:37 am

Post » Tue Aug 09, 2011 11:04 pm

Hello, again. It seems I ran into another small problem.

My task this time is to create a counting program that counts from 1 to 100 in increments of 5.

So far I have this...

#include #include main(){for( int k = 1; k<=100; k+=5 )}



I get an error about C99 mode or something like that. After doing some research I learn that I am suppose to insert C -std=c99 somewhere in my code? I add it in but I just get more errors and it refuses to compile. Can someone tell me what is wrong here?
User avatar
Iain Lamb
 
Posts: 3453
Joined: Sat May 19, 2007 4:47 am

Post » Tue Aug 09, 2011 12:44 pm

Your loop does not really do anything and it's missing the brackets {}, but somehow I'm not sure is it that. Missing semicolon, shrugs? And your main function is missing either int or void return type. Then if you return an int, main would need to end in return 0;. I can't remember what ctype.h includes.
User avatar
Amanda Leis
 
Posts: 3518
Joined: Sun Dec 24, 2006 1:57 am

Post » Tue Aug 09, 2011 7:30 pm

Questioning someones decision of programming language is usually frowned upon, but I must ask, why C?

Also, you should add a printf (is it printf?) statement within the for-loop, displaying the variables current value at every loop.
User avatar
Andrew Tarango
 
Posts: 3454
Joined: Wed Oct 17, 2007 10:07 am

Post » Tue Aug 09, 2011 11:41 pm

Believe me, I don't want to do this crap but it is a required course for web design...
User avatar
Lloyd Muldowney
 
Posts: 3497
Joined: Wed May 23, 2007 2:08 pm

Post » Tue Aug 09, 2011 12:02 pm

lol, I feel for you :brokencomputer:
User avatar
Allison C
 
Posts: 3369
Joined: Mon Dec 18, 2006 11:02 am

Post » Tue Aug 09, 2011 7:46 pm

You need to declare k separately, not within the for loop.

Edit: and not forgetting that it needs to do something, such as "printf("k=%d\n", k);", or at least be terminated with a semicolon.

Questioning someones decision of programming language is usually frowned upon, but I must ask, why C?

Why not? :unsure:
User avatar
Lil'.KiiDD
 
Posts: 3566
Joined: Mon Nov 26, 2007 11:41 am

Post » Tue Aug 09, 2011 1:15 pm

You need to declare k separately, not within the for loop.

Ohh, right, that too, but it would work as is in C++, I think.
User avatar
Amanda Leis
 
Posts: 3518
Joined: Sun Dec 24, 2006 1:57 am

Post » Tue Aug 09, 2011 12:44 pm

Why not? :unsure:



Well, unless you intend to make some highly-optimized or resource-hogging software, it really aint neccessary.
User avatar
Emma Parkinson
 
Posts: 3401
Joined: Wed Jul 26, 2006 5:53 pm

Post » Tue Aug 09, 2011 8:05 pm

I am still getting this error about "loop initial declaration used outside c99 mode".
User avatar
Nany Smith
 
Posts: 3419
Joined: Sat Mar 17, 2007 5:36 pm

Post » Tue Aug 09, 2011 9:48 pm

Well, unless you intend to make some highly-optimized or resource-hogging software, it really aint neccessary.

I'm not entirely sure what to say in response to that. Optimised code is always desirable, and there are worse ways to teach it than with C; given that it forms the basis for many other contemporary programming languages, it's not a bad place to start.

Having used it for the past 25 years I'm aware of its limitations and like any other tool it's not necessarily ideal for every situation, but it doesn't make a lot of sense to write it off so quickly.
User avatar
Lil Miss
 
Posts: 3373
Joined: Thu Nov 23, 2006 12:57 pm

Previous

Return to Othor Games