2

Trying to get some code working and the modulus doesn't want to do what I want it to do... which means I have it wrong.

I have unsigned chars that I'm trying to seperate out hours/minutes/seconds into so I can display them on the screen in Ascii.

The variable secs is anunsigned int. Everything else is anunsigned char. I want the results in unsigned chars so not to waste memory. Working in an embedded environment.

Anyone care to look at the code snippet and tell me what I've done wrong?

hours   = secs/3600.0;
minutes =(secs/60.0)-(hours*3600);
seconds =secs-(hours*3600)-(minutes*60);

sec_ones    =(unsigned char)((seconds%10));
sec_tens    =(unsigned char)((seconds-sec_ones)%100);
min_ones    =(unsigned char)(minutes%10);
min_tens    =(unsigned char)((minutes-min_ones)%100);
hrs_ones    =(unsigned char)(hours%10);
hrs_tens    =(unsigned char)((hours-hrs_ones)%100);
Mike
  • 47,263
  • 29
  • 113
  • 177
Chef Flambe
  • 885
  • 2
  • 15
  • 35

4 Answers4

2
minutes =(secs/60.0)-(hours*3600);

should be

minutes =(secs/60.0)-(hours*60);

Other than that, it works for small enough input: http://ideone.com/VPKP1

There are some things that I'd change, though. For example, there's no point doing a double division and then assigning the result back to unsigned char, you might as well just do integer division.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
1

You mentioned it is an embedded program.

seconds = secs-(hours*3600)-(minutes*60);

hours is an unsigned char promoted to int in the expression hours*3600.

If your are working with 16-bit int you will have problems with the above line. On two's complement system 16-bit int range go from -32768 to 32767 which mean you have an overflow when hours is >= 10.

ouah
  • 142,963
  • 15
  • 272
  • 331
0

first, you do your computations with double since you are using double constants.

then the modulus calculations will not be done as unsigned char because it is a narrow type. generally it will first be promoted to int and then the calculations will be done.

Usually you are better off by using unsigned int directly for all your variables. char types are only useful when you want to save space.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
0

your program is just bad written, try to do that, it run correctly

unsigned int secs = 5000;
unsigned char sec_ones,sec_tens,min_ones,min_tens,hrs_ones,hrs_tens, hours, minutes, seconds;

hours   = secs/3600.0;
minutes =(secs/60.0)-(hours*60);
seconds =secs-(hours*3600)-(minutes*60);

sec_ones    =(unsigned char)((seconds%10));
sec_tens    =(unsigned char)(seconds/10);
min_ones    =(unsigned char)(minutes%10);
min_tens    =(unsigned char)(minutes/10);
hrs_ones    =(unsigned char)(hours%10);
hrs_tens    =(unsigned char)(hours/100);

printf("%.1u%.1u:%.1u%.1u:%.1u%.1u\n",hrs_tens,hrs_ones,min_tens,min_ones,sec_tens,sec_ones );
elhadi dp ıpɐɥןǝ
  • 4,763
  • 2
  • 30
  • 34
  • Baddly written? Never would have guessed that. Good thing I asked for help. Yes, your responce DID help. I realized a calc error. 3am coding never a good idea. Thanks. – Chef Flambe Jun 01 '12 at 03:15