Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How is my programming?
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
DtY




PostPosted: Sun Apr 26, 2009 10:03 pm   Post subject: How is my programming?

This weekend I decided I'd learn C, I played with c++ a bit (and mos6502 asm), but other than that this is my first experience with a low level language.
I'm just wondering how the code looks, like if it should be cleaner (likely), if there's a better way to do something, etc.
Also, I'd like to know if it compiles and runs for everyone, I've only tested it on my own computer using gcc on fedora core 10, Linux.



TicTacToe.gz
 Description:
Linux binary

Download
 Filename:  TicTacToe.gz
 Filesize:  3.44 KB
 Downloaded:  197 Time(s)


Tic-Tac-Toe.zip
 Description:
Tic-Tac-Toe source

Download
 Filename:  Tic-Tac-Toe.zip
 Filesize:  2.22 KB
 Downloaded:  190 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Mon Apr 27, 2009 12:26 am   Post subject: RE:How is my programming?

Short list.


  • No conditional include guards on game.h
  • The functions defined in game.c asude from game() are not declard in game.h.
DtY




PostPosted: Mon Apr 27, 2009 6:04 am   Post subject: RE:How is my programming?

What are guards? And don't I only need to define the functions that need to be public in the header, don't I?
wtd




PostPosted: Mon Apr 27, 2009 1:08 pm   Post subject: RE:How is my programming?

See http://wiki.compsci.ca/index.php?title=Whirlwind_Tour_of_C#Let.27s_compile_something.2C_for_crying_out_loud.21
DtY




PostPosted: Mon Apr 27, 2009 6:40 pm   Post subject: Re: How is my programming?

Okay, I added the include guard, and made all the functions other than game() static, so I don't need to add these to the header do I? (Since they can't be accessed from outside that file)


Tic-Tac-Toe.zip
 Description:
updated source

Download
 Filename:  Tic-Tac-Toe.zip
 Filesize:  2.29 KB
 Downloaded:  203 Time(s)

wtd




PostPosted: Tue Apr 28, 2009 2:13 am   Post subject: RE:How is my programming?

It may be useful to have those helper functions in your header, if only to ensure that you have an interface for them that can be checked when game.c is compiled.
DtY




PostPosted: Tue Apr 28, 2009 3:43 pm   Post subject: RE:How is my programming?

Okay, should they still remain static though?

[edit] Here is game.h with the definitions:
c:
#ifndef __GAME_H
#define __GAME_H

static char getRepr(int i);
static void drawBoard(int board[3][3]);
static void getMove(int board[3][3]);
static void doMove(int board[3][3]);
static int checkWinner(int board[3][3]);
int game(void);

#endif


Also, I had an idea, instead of putting all the function definitions at the top (without the block), if I just #included game.h, since the definitions are there, and if I changed one of the function outlines, I would only need to change it in two places, rather than three. I tried it, and it compiled fine on my compiler, do you know if it will on most compilers?
wtd




PostPosted: Tue Apr 28, 2009 4:13 pm   Post subject: RE:How is my programming?

I'm not sure I follow you.

Normally you have your header with function declarations and your implementation file containing definitions of those functions.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Tue Apr 28, 2009 4:18 pm   Post subject: RE:How is my programming?

Also, in your makefile, you should create CC and LN (compiler and linker) aliases. Allows you to easily change compilers and linkers with only one edit.

Oh, and you need a "clean" rule in your makefile. Smile
DtY




PostPosted: Tue Apr 28, 2009 5:09 pm   Post subject: RE:How is my programming?

Okay, here's my updated makefile:
code:
CC = gcc -c
LN = gcc -o

TicTacToe: TicTacToe.o game.o
        ${LN} TicTacToe TicTacToe.o game.o

TicTacToe.o: TicTacToe.c game.h
        ${CC} TicTacToe.c
game.o: game.c game.h
        ${CC} game.c

clean:
        touch TicTacToe.o game.o TicTacToe
        rm TicTacToe.o game.o TicTacToe

And on the subject of makefiles, any idea why make requires tabs, and doesn't recognize whitespace as whitespace like all other *nix tools?

[edit] Missed your first post, I mean like at the beginning of the files, you have the declaration of the function, like static char getRepr(int i); (I think it's called prototyping), so instead of putting that at the top of the file, I can just #include it's header file.
OneOffDriveByPoster




PostPosted: Tue Apr 28, 2009 6:23 pm   Post subject: Re: RE:How is my programming?

If you have static functions, do not put them into a header file (at least the one that is supposed to be for the interface).
Use a separate header file that can only be included into the correct file (#error inside #if with appropriate check).

The CC variable should not have the "-c" part. Make it plain "gcc". The LN variable is a confusing name for the linker; maybe use "LD". Again, the "-o" part probably does not belong with the command variable.

Your use of touch and then rm can be replaced with "rm -f". FYI, GNU make has $(RM).
DtY




PostPosted: Tue Apr 28, 2009 9:04 pm   Post subject: RE:How is my programming?

Okay, changed all that. What is $(RM)? Is that just a shortcut to rm -f or something?
OneOffDriveByPoster




PostPosted: Tue Apr 28, 2009 10:09 pm   Post subject: Re: RE:How is my programming?

DtY @ Tue Apr 28, 2009 9:04 pm wrote:
Is that just a shortcut to rm -f or something?
The default setting is that it is "rm -f", yes.
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 13 Posts ]
Jump to:   


Style:  
Search: