Tag: C99

In praise of Visual Studio

In praise of Visual Studio

Visual Studio debuggingLooking at some of the posts on the C subreddit about recommendations for a text editor to help learn C, I see many people recommending editors like Sublime Text, Notepad (on Windows).

I started programming in an era when IDEs were few and far between and an IDE is a wonderful time saving device. It is vastly superior to editing in a text editor then launching a compiler either by command or a batch file.

With an IDE you get immediate feedback on where the errors are, sometimes with a mini-map that lets you navigate directly to the error. It’s quicker to refactor code (though that’s a function of an editor) and usually one key press away from running or debugging.

Debugging in particular is very good in Visual Studio on Windows or Visual Studio Code on Windows/Linux/Mac. It’s visual, you can see multiple lines of code, add breakpoints and step through it with one key press.

I find line debuggers like GDB to be very frustrating by comparison.  Mousing over a variable and seeing its value in a debugger is a big time saver. Also seeing the stack calls and being able to jump to calls in the stack is handy.

Now if you are used to Linux, working in a terminal and compiling code through batch files then fine, but I believe its a bit of a hurdle for new programmers. An IDE makes it easy to switch from editing, compiling and debugging without leaving the program. It is all round more productive and easier to get into for a beginner.

So it’s worth at least trying out the free version (Community Edition) of Visual Studio if you are on Windows.  The days when Microsoft hated Linux (They called it a cancer!) are long past.  And no I don’t get paid for advocating Visual Studio; I just like it. Considering that it’s completely free, it’s remarkable value.

Do you declare variables at the top of functions

Do you declare variables at the top of functions

Matrix code
Image by Pete Linforth from Pixabay

This is for C99 and later. Prior to this, in function you had to declare all the variables that would be used in that function before any code. Since C99, you can declare them nearer to where they are used.

This function is from the Asteroids game. The declaration of the two variables hx,hy could have been moved to just before the do-loop. They can’t be declared inside the do-loop because they are used after it.

// doHyperJump() find empty space on screen fo PlayerShip
void CheckJump() {
	int hx, hy;

	if (jumpFlag && SDL_GetTicks() - jumpTimer > 3000) { 
		jumpTimer = SDL_GetTicks();
		jumpFlag = 0;
		do {
			hx = 65 + Random(SCREENWIDTH - 130);
			hy = 65 + Random(SCREENHEIGHT - 130);
			if (IsEmptySpace(hx, hy)) break;
		} while (1);
		Player.x = (float)hx;
		Player.y = (float)hy;
		Player.vx = 0;
		Player.vy = 0;

This is one of those cases where it doesn’t really matter. But generally I will declare them close to where they are first used and I almost always declare the for-loop variable in the for-loop. Only if it’s needed afterwards do I declare it after, but that is a slightly iffy use unless you want to check the value it had when it exited the loop.

Remember the scope of the variable i.e. the part of the function in which the variable can be accessed starts just after it is declared. By keeping declarations near the code, you reduce the scope and this can help robustness.

I’m considering switching to C11

I’m considering switching to C11

Programming image
Image by Gerd Altmann from Pixabay

All C code I write in the books is currently to the C99 standard. All the compilers involved (Visual C++ on Windows and Clang on Ubuntu) support C99 but C11 support seems restricted to GCC and Clang.

Microsoft has traditionally supported C++ but their C support seems a bit grudging; realistically they don’t prioritise it which I can understand.

Given though that I’m not going to republish my first e-book for a while (I’d like to add a WebAssembly chapter or two first), I’m going to investigate whether it’s worth switching to C11 for the 2nd book. From what I’ve read all it needs is a flag to tll it to compile to C11 standards. This is for Clang.


But the other question is what will I gain by doing this and I can’t actually see there’s that much benefit.. I don’t need Unicode, I don’t think alignment will really make much difference. You can read about the C11 changes on WikiChip.

So I’ve made the decision. I’ll stick with C99 for now. But for an alternative view, I recommend Danny Kalev’s 2012 article on C11.