What does this line of code do?

C and PASCAL (or any other high-level languages) in here please

Moderators: exxos, simonsunnyboy, Mug UK, Zorro 2, Moderator Team

User avatar
wyerd
Atari maniac
Atari maniac
Posts: 87
Joined: Fri May 31, 2013 7:31 pm
Location: Upstate NY. USA.

What does this line of code do?

Postby wyerd » Sat Sep 30, 2017 3:24 pm

I’m reading the C programming guide C-Manship Complete and have come across a line that I don’t understand what it’s doing:-

Code: Select all

year = year % 100;


This is part of the date and time example in chapter 23. I understand that it’s getting the date from the ST, converting the bits and adding 80 to get the year, but what does the above code do?

Thanks!

User avatar
TheNameOfTheGame
Atari God
Atari God
Posts: 1058
Joined: Mon Jul 23, 2012 8:57 pm
Location: Almost Heaven, West Virginia

Re: What does this line of code do?

Postby TheNameOfTheGame » Sat Sep 30, 2017 3:28 pm

Gets the modulo (remainder of division) of the year. For example 1999 would return 99.

If you want the century part, would be year = year / 100; example 1999 would return 19.

User avatar
wyerd
Atari maniac
Atari maniac
Posts: 87
Joined: Fri May 31, 2013 7:31 pm
Location: Upstate NY. USA.

Re: What does this line of code do?

Postby wyerd » Sat Sep 30, 2017 3:41 pm

Ok, but at the end of the previous line :-

Code: Select all

year = ((date >> 9) & 0x007f) + 80;


If the the date returned for example is 19 (years from 1980) add the 80 = 99 already.

User avatar
TheNameOfTheGame
Atari God
Atari God
Posts: 1058
Joined: Mon Jul 23, 2012 8:57 pm
Location: Almost Heaven, West Virginia

Re: What does this line of code do?

Postby TheNameOfTheGame » Sat Sep 30, 2017 3:57 pm

Yes, it looks like that. Maybe they made an error. But I guess not.
Last edited by TheNameOfTheGame on Sat Sep 30, 2017 4:06 pm, edited 1 time in total.

czietz
Hardware Guru
Hardware Guru
Posts: 480
Joined: Tue May 24, 2016 6:47 pm

Re: What does this line of code do?

Postby czietz » Sat Sep 30, 2017 4:02 pm

Well, consider what happens if ((date >> 9) & 0x007f) is for example 21 (years from 1980, i.e. 2001). Then it becomes obvious why the modulo 100 operation is indeed required.

User avatar
TheNameOfTheGame
Atari God
Atari God
Posts: 1058
Joined: Mon Jul 23, 2012 8:57 pm
Location: Almost Heaven, West Virginia

Re: What does this line of code do?

Postby TheNameOfTheGame » Sat Sep 30, 2017 4:08 pm

Oh ok, makes sense now. 21+80 = 101 so the modulo would be 1.

User avatar
wyerd
Atari maniac
Atari maniac
Posts: 87
Joined: Fri May 31, 2013 7:31 pm
Location: Upstate NY. USA.

Re: What does this line of code do?

Postby wyerd » Sat Sep 30, 2017 4:21 pm

Thanks. Yes, I understand now. So, looking at the part of the code that sets the clock to the new date won’t work after the year 1999 as it’s subtracting 80:-

Code: Select all

strncpy ( s, &string[4] );
 h= atoi ( s ) - 80;
 h = h << 9;
 date = date | h;


Let me see if I can figure it out myself :D

czietz
Hardware Guru
Hardware Guru
Posts: 480
Joined: Tue May 24, 2016 6:47 pm

Re: What does this line of code do?

Postby czietz » Sat Sep 30, 2017 4:26 pm

That code won't work anyway, because strncpy takes three arguments, not two: https://www.tutorialspoint.com/c_standa ... trncpy.htm

Maybe this book isn't the best way to learn C?

User avatar
wyerd
Atari maniac
Atari maniac
Posts: 87
Joined: Fri May 31, 2013 7:31 pm
Location: Upstate NY. USA.

Re: What does this line of code do?

Postby wyerd » Sat Sep 30, 2017 4:39 pm

You’re correct. It’s missing the value for n.

Code: Select all

strncpy ( s, &string[4], 2  );


I’ll stick with it. I find debugging programs helps the learning process!!

czietz
Hardware Guru
Hardware Guru
Posts: 480
Joined: Tue May 24, 2016 6:47 pm

Re: What does this line of code do?

Postby czietz » Sat Sep 30, 2017 4:49 pm

OK, I looked at the sample from chapter 23 of the C-Manship Complete book. It has a much more subtle but potentially much more disastrous bug! It does things like...

Code: Select all

char s[3]; /* Temporary string storage. */
[...]
/* Extract "hours" portion and convert to integer. */
strncpy(s, string, 2);
h = atoi(s);


Note that strncpy here copies two characters and no null terminator. This code relies on the last byte in s, i.e. s[2] not being a number, so that atoi will stop its conversion. Since the memory used by s is however never initialized, if by chance s[2] is a number, the result will be completely wrong.

This is an error that is incredibly hard to spot, especially for a new C programmer. I'm afraid, I still don't think this book constitutes a good example to learn from.

User avatar
wyerd
Atari maniac
Atari maniac
Posts: 87
Joined: Fri May 31, 2013 7:31 pm
Location: Upstate NY. USA.

Re: What does this line of code do?

Postby wyerd » Sat Sep 30, 2017 6:05 pm

What is the correct method of converting a string to an integer? Can you recommend any good C guides? Thanks.

czietz
Hardware Guru
Hardware Guru
Posts: 480
Joined: Tue May 24, 2016 6:47 pm

Re: What does this line of code do?

Postby czietz » Sat Sep 30, 2017 6:16 pm

atoi is not the problem here, strncpy is. Although in this particular case (parsing of the time and date from a string) one might use sscanf instead of all these manual string extractions and conversions.

As for good guides: If you want to learn programming [1], the C language, its standard library or basic algorithms, there surely are good modern books. I can't recommend any, though, because it's been a long time since I had a look at what's available. Others might be able to post some suggestions.

If you want to learn Atari/GEM programming though, there won't be any modern books, obviously. There is Peter Lane's GEM programming guide [2], but it expects that you already know how to program in C. Also it doesn't go into the same amount of detail as the C-Manship book. So, sorry, I can't help with recommendations.


[1] Some people say -- and with good arguments, imho -- that one should not teach C anymore to beginners but modern C++ instead: https://www.youtube.com/watch?v=YnWhqhNdYyk. C++ however might not be the best choice on the Atari.
[2] http://peterlane.info/gemguide/html/index.html

User avatar
wyerd
Atari maniac
Atari maniac
Posts: 87
Joined: Fri May 31, 2013 7:31 pm
Location: Upstate NY. USA.

Re: What does this line of code do?

Postby wyerd » Sat Sep 30, 2017 7:05 pm

Yes, the sscanf function does look a much neater approach.

I’ve programmed in other languages before, but I want to develop an accessory for my STe and C appeared to tick the box.

Thanks for the suggestions. I’ll take a look at them.


Social Media

     

Return to “C / PASCAL etc.”

Who is online

Users browsing this forum: No registered users and 1 guest