This month’s Exercise was inspired by a program I recall from years ago called The Drunk and the Lamppost. It’s not the classic joke, but an examination of random movements and probability.
Continue reading
Author Archives: dgookin
Declaring a String Literal as a Pointer
The best way to declare a string literal in your code is to use array notation, like this:
char string[] = "I am some sort of interesting string.\n";
Continue reading
The Wandering King
The chessboard is empty. In the center is the king. He can move only one square at a time, but in any direction — or he can choose not to move at all. How many turns would it take him, moving randomly, to exit the chessboard?
Continue reading
Does the File Exist?
Many programs rely upon external files to store data. When the file is missing, or not where it should be, the fopen() function returns a NULL pointer, which your code can act upon. Or, you can pre-check for the file’s existence by using the access() function.
Continue reading
Ignoring Signals
Have you ever had your C code run amok and had to press Ctrl+C to cancel?
Yeah, I’ve never had that happen to me either.
Continue reading
I’m Outta Here
A specific command to terminate a program was popular with programming languages long ago. For example, in BASIC you had the END command, which exited a program. For unusual circumstances, you could use the STOP command. It was the abnormal termination command, which stopped the program and also generated a “Break in line” message (in GW Basic). I’m sure other early languages also had commands to bail out of a program, and modern languages are the same.
Continue reading
Fizz Buzz – Solution
The nifty thing about this month’s Exercise is that so many solutions are available. In fact, I found the challenge engaging because I kept trying to think of other ways to solve the puzzle.
Continue reading
Negative Time
With the dawn of the new year, and my reflection upon the Clock program I blogged about recently, I’ve been thinking about time. Specifically, the time_t data type used with various C library time functions. Can this value contain negative time?
Continue reading
Fizz Buzz
The FizzBuzz problem is quite easy to explain, but not as easy to code because you can go in so many directions. It works like this:
Continue reading
Tick-Tock Goes the Clock, Part III
My clock program is almost complete! It generates the current time as output and it updates the current time every minute. The only step remaining is to synchronize the program’s output with the system time. I want to ensure that when the system reads :00 seconds that the program ticks over to the next minute.
Continue reading