View previous topic :: View next topic |
Author |
Message |
defragment Just Arrived
Joined: 29 Feb 2004 Posts: 0
|
Posted: Thu Mar 11, 2004 2:05 am Post subject: YACP (Yet another code post) |
|
|
I just thought I would post another source I had written so I could get some feedback about the style, naming conventions and anything else that you can think of. My goal is to write efficent, easy-to-read code. Well enough with the introduction, here is the code.
Code: |
/* This is a very basic example, it will show you how to take the amount of seconds the user inputs
and convert that into Hours, Minutes and Seconds. Obviously this source is aimed at a beginner to
the C programming language, and would most likely not help a seasoned programmer. */
/* Author: Python
Site: [Taken out so I do not 'spam']
Language: C */
#include <stdio.h> /* Basic input/output library */
#define SECONDS_PER_HOUR 3600 /* This is a Symbolic Constant holding the value 3600 */
#define SECONDS_PER_MINUTE 60 /* This is a Symbolic Constant holding the value 60 */
/* We use unsigned integers, because unsigned will make then contain positive numbers only and
an integer is a good variable type for this kind of operation, and it takes up less RAM than
a long. */
unsigned int hours, minutes, seconds, rem_minutes, rem_seconds;
int main() /* Our main function */
{
/* Here we will tell the user how to use this application, and then let them enter the amount
of seconds they wish to convert, I think this is pretty straight forward */
printf("Useage: Simply enter the seconds, for example 60. Stay below 65000.\n\n");
printf("Enter the amount of seconds:");
scanf("%d", &seconds); /* Note: Scanf allows you to take keyboard input and place it into a variable. */
/* Now we convert seconds into time */
hours = seconds / SECONDS_PER_HOUR;
minutes = seconds / SECONDS_PER_MINUTE;
rem_minutes = minutes % SECONDS_PER_MINUTE;
rem_seconds = seconds % SECONDS_PER_MINUTE;
/* End of the calculation(s) */
/* Now we just print our results on the screen, and close out the function */
printf("Hour(s): %d Minute(s): %d Second(s): %d\n\n", hours, rem_minutes, rem_seconds);
return 0;
}
|
|
|
Back to top |
|
|
alt.don SF Boss
Joined: 04 Mar 2003 Posts: 16777079
|
Posted: Thu Mar 11, 2004 2:19 am Post subject: |
|
|
A C expert I am not, but it looks good to me.
|
|
Back to top |
|
|
UziMonkey SF Reviewer
Joined: 19 Dec 2003 Posts: 5
|
Posted: Thu Mar 11, 2004 2:47 am Post subject: |
|
|
A few things. There is no reason to stay below 65000. the limit on a 32-bit integer is much higher (UINT_MAX will hold this, make sure to include limits.h). It would be easier to get rid of the seconds/minutes remaining and use floating point values. You can use ceil to round up to the nearest integer. Also, there is no reason to use all those globals, put them in main. It's not like you need more than one variable anyway..
|
|
Back to top |
|
|
Stormhawk Trusted SF Member
Joined: 26 Aug 2003 Posts: 31 Location: Warwickshire, England, UK
|
Posted: Thu Mar 11, 2004 12:21 pm Post subject: |
|
|
One further point - comments are supposed to explain why you're doing something, not just re-state what the code tells you anyway. People reading the code are likely to know *what* it does, the comments should be there to explain *why* it does it, within the context of the program's objective(s).
Example:
Code: |
scanf("%d", &seconds); /* Note: Scanf allows you to take keyboard input and place it into a variable. */
|
This comment is unnecessary to anyone who knows even basic C.
The comment about printing the result, at the end, is arguably unnecessary also.
Oh, and "useage" should be spelled "usage"
|
|
Back to top |
|
|
|