Category: C

Is C a portable language?

Is C a portable language?

brown and black suitcase with red and yellow plastic toyI occasionally write answers on Quora.com and many recurring questions are about assembly language. Now I used to write games in 6502 and Z80 assembly language when the only alternative was Basic. But assembly language is very tedious to write, painstaking and it’s slow to write programs in assembly language. Most of your code is moving values between memory and registers or vice versa.

Anything major you write will not easily port onto a different CPU or Operating System. A Mac uses a different CPU to most Windows PCs and a Raspberry Pi uses a similar CPU design to a Mac (M1 or M2) but there are still differences in calling OS routines on Raspberry Pi OS (based on Debian) and Mac OS.

Higher level languages are generally portable. I’ve written C# code that runs on Linux and my game which was 2,200 lines of C needed about an hours work to run on Ubuntu Linux and Raspberry Pi.

But if you have a large complex C application, it may have issues when porting it.  A developer Rex Jaeschke wrote a book on portability and published it in 1989. He’s recently updated it and it’s available on Wikibooks.  It’s called Portability and the C language and is several hundred pages long. If you ever want to port C, it is very worthwhile reading. I’ve added a permanent link to the Link to C utilities page.

 

Is Tiobe Index misinformation?

Is Tiobe Index misinformation?

news, false, conceptEvery month a new Tiobe Index is published purporting to show the most popular programming languages. Except, if you look at other programming language popularity surveys, lists etc, the Tiobe Index increasingly seems to be at odds with everybody else.

Take the programming language C. As you can imagine I have a particular interest in it. I was at first delighted when I saw it at #3 in the Tiobe Index. In fact, in their most recent index it is listed as the 2nd most popular programming language. Well that is just crazy. No matter how much I might want it, there is no way on Earth that it is the 2nd most popular programming language.

It’s not just me saying it. Let’s look at who else says it.

SiteC's RankingComments
Tiobe Index2Updated monthly
GitHub Octoverse Programming languages9Annually- this is October 2022
Reddit Programming languages10Subreddits for programming languages
Red Monk Programming Language Rankings10June 2022. Updated every 6 months
StackOverflow developer survey11Annually
Statista most used programming languages 202211
Hackerrank Developer skills report 202312

There are other programming language lists or surveys but those show what I mean.  No one else has C anywhere that high. Here it’s in positions 9-12 with an average roughly around 10.5 if you exclude Tiobe or 9.3 if you don’t.  I think their methodology is flawed and biased by age. Older languages appear to carry more weight.

Never mind C, what about JavaScript? Everyone else has it in first place (except HackerRank who has it in 4th). Tiobe index has it in 7th. Misinformation is defined as “false information that is spread, regardless of intent to mislead.”. Maybe that’s a bit harsh but that’s how I regard the Tiobe Index.

Nice to see an article favouring C

Nice to see an article favouring C

brown wooden letter c decorToptal.com is a site for recruiting freelances for projects. They have published an article by a developer Daniel Munoz which praises C and shows how it is still quite relevant today. It’s definitely worth a read.

Given the pressure today to move to Rust, Go and even Kotlin, it’s nice to see someone praising C.  Of course for the games I do C is near perfect.

Posted in C
How to debug programs using SDL

How to debug programs using SDL

Terminal fprintf outputThere’s nothing worse than a program halting with a simple “Segmentation fault” and no idea where or why. It happened to me today working on the 2nd eBook (for Raspberry Pi) and I had to figure out where it was going wrong.

In the end it was a really silly bug, I was trying to load masks but had left the masks/ folder out of the filename.

How did I find it? I sprinkled a few fprintf(stderr,”message”); throughout the program changing “message” to something appropriate and launched it from a terminal. I’d thought it was in a function LoadTextures() so added a call before and after but you can see that worked and the segmentation fault happened after LoadTextures().

So I added it before and after LoadMasks() and my second run it happened before it reached the After LoadMasks message.

Note, I used stderr as the output path because, unlike stdout which is buffered and can be cleared, anything sent to stderr appears immediately.  More about this in this offsite article.

The using SDL part of the title is just to show that you can find bugs even in programs that use SDL.

How to fix sound on the Raspberry Pi

How to fix sound on the Raspberry Pi

Raspi-config for Raspberry PiFirst I modded the code that inits sound to this:

	int success=Mix_OpenAudio(44000, AUDIO_F32LSB, 2, 8192);
	if (success==-1 ) {

		LogError2("InitSetup failed to init audio because %s",Mix_GetError());
	}

That extra call to Mix_GetError() returns a string saying what the problem is and in this case I got

ALSA : Couldn’t find any hardware audio formats.

ALSA is short for Advanced Linux Sound Architecture and Wikipedia is very informative. It certainly put some perspective on it.

 

Also I found this article adds a lot more information.  What fixed it though was doing

sudo raspi-config

Then pressing enter on 1. System Options and then S2 Audio and selecting the headphones option. Simple as that. After that asteroids played the sounds as expected.

There’s a lesson here which is to use the functions provided, in this case calling the Mix_GetError() function to show errors.  I added it into the PlayASound() call to see why it was failing, and it stopped doing it, i.e. it worked with lots of explosions on screen. I suspect it was the sound configuration hadn’t been properly set which is an issue with Raspberry Pis apparently.

Raspberry Pi Sound issues- trying to fix it

Raspberry Pi Sound issues- trying to fix it

Loudpeaker
Image by OpenClipart-Vectors from Pixabay

In working through my Linux/Raspberry Pi eBook(Yes – the second eBook!), I’m up to the chapter where sounds are introduced using the SDL_mixer library. And I’ve hit two sets of problems.

It sometimes refuses to initialize the sound code.  This code below hits the LogError line:

	int success=Mix_OpenAudio(22050, AUDIO_S16LSB, 2, 8192);
	if (success==-1 ) {
		LogError("InitSetup failed to init audio");
	}

The other day it was working but not today. Now I have updated the Pi’s code (sudo apt update etc) but I wouldn’t have expected that to break it. I’ve been looking on the internet and find the whole thing a bit complicated.

I’ve got my Pi running 64-bit Raspberry Pi OS. I’ve changed the output device to headphones which plug into the headphone socket. If I run the VLC media player and tell it to play into the headphones, it will happily play the .wav files I’ve got for the asteroids game.

But if I run speaker-test, a terminal application with this command line

speaker-test -c2 -twav -l7 plughw:1,0

I get

speaker-test 1.2.4

Playback device is default
Stream parameters are 48000Hz, S16_LE, 2 channels
WAV file(s)
Setting of hwparams failed: Invalid argument

By running this command:

aplay -L

I got 71 lines of output but of these these below are the most important

output
hw:CARD=Headphones,DEV=0
    bcm2835 Headphones, bcm2835 Headphones

and the speaker-test command using the device parameter –Dhw:Headphones now worked. I’ve highlighted the bits in the aplay output needed to identify the device.

The new command  is

speaker-test -c2 -twav -l7 -Dhw:Headphones

I can now hear a female voice saying front left then front right a few times in my headphones.

So my Pi’s sound device is working; I just have to figure why SDL_mixer isn’t always. I’ll keep looking.

And the second problem which only occurs when the mixer is working, is when you play a lot of sounds. The PlayASound() function checks the result. On Windows it never had a problem but on Raspberry Pi, when you blow up a lot of asteroids say at one time, it plays a number of explosions then returns an error for each explosion after that. I think there’s only so many channels; that’s an easy fix; just ignore the error and return as if it succeeded.

A very useful Windows Utility in C

A very useful Windows Utility in C

ExplorerPatcher menuIf you use Windows 11 you’ll be aware of one or two issues with it; the file explorer popup menu needs a second click to take you to the Windows 10 menu with Copy, cut paste etc.

So I was pleased to discover explorer patcher which is a utility written in C that lets you get the Windows 10 right-click menu back as well as changing the File explorer right-click popup menu and a lot of other stuff.

It’s on GitHub and the authors are happy for you to look at the source code and submit improvements of your own.

If you want to see how to make changes to Windows etc in C, this is a great example.

Added to the C Code Links page.

PS. I use this and have found that after a Windows update, the right-click menu gets reset to the Windows 11. However just opening the explorer patcher settings and unticking then ticking the option for Disable the Windows 11 context menu on the File Explorer menu fixes it.

How to Configure Visual Studio Code for C/C++ development

How to Configure Visual Studio Code for C/C++ development

VS Code C/C++ extensionWhen I first started with VS Code on Linux, I found the C/C++ configuration somewhat confusing.  I blundered through, wasted a bit of time and got there in the end. After a gap of a couple of years I did the same again recently on Raspberry Pi. It’s clearer in my mind now so I thought I’d explain it here. This works for Ubuntu, Raspberry Pi OS and should for most other Linuxes. (Not that I’ve tried them all…)

We’ll start with you having already installed Clang (or GCC) and VS Code, and the C/C++ extension for VS Code (shown above). So make sure those are all done.

Start by defining a Folder for VS Code. VS code doesn’t use projects but it manages everything relative to the currently defined Folder.  It’ll ask you to open a folder initially. That’s where your source code etc will go. I created a folder called examples under my home folder and used that.

To compile anything C/C++ you also need two json files. These files are

  • tasks.json
  • c_cpp_properties.json

They are held in a hidden folder called .vscode in your current folder. Press F1 and you’ll see a popup menu. Type in C/C++ and you’ll see all the C/C++ items. Select C/C++: Edit Configurations (JSON). It’s highlighted below.

Vs Copde C++ menu

Now if you click that, and look in the folder examples you’ll see nothing but if you know how to view hidden files using the files utility (as shown below). Then you’ll see the folder .vscode.  On Raspberry Pi the file explorer always shows hidden files.

Show hidden files in files utility Now look in that folder and you’ll see c_cpp_properties.json.

Next we want tasks.json. On the Terminal menu, click the bottom item which is Configure Default Build Task

It’ll ask you to select the clang  build active task so click that and voila you now have tasks.json open in the editor.

Vs Code Configure Default Build Task

Build Active File

 

 

 

 

 

 

 

 

 

 

Now I’ve created the standard hello world file in the file hw.c.

#include <stdio.h>

int main() {
  printf("Hello world\n");
  return 0;
}

So just do Terminal/Run Build Task and it will have clang compile the currently opened file. If you get terminal failed to launch (exit code: -1) then it’s likely that your hello world source file was not the currently opened file in the editor. You can see which file is open because its tab is brightest.

Note that hw.c is brighter than tasks.json on the left. On the right, the open file is tasks.json and its tab is brighter.

Open file in VS Code

Tasks.json is open

So what are the json files for?

The c_cpp_properties.json lets you specify include file paths.  For instance if you have the SDL files installed, the include files are in /usr/include/SDL2

SDL2 include files

Note you can install SDL on linux by following these instructions.

The tasks.json file lets you specify which files are to be included and also linked.

Here I’ve just shown the args section from a tasks.json used to build SDL2 games.

	"args": [
		"-g",
		"${file}",
		"${workspaceFolder}/hr_time.c",
		"-o",
		"${fileDirname}/${fileBasenameNoExtension}",
		"-lSDL2",
		"-lSDL2_image",
		"-lSDL2_mixer",
		"-lm"
	],

The -g option includes files. The ${file} is the current opened file and {workspaceFolder{} specifies the current folder where the file hr_time.c (used for timing). The -l is for linking files and links SDL2, SDL2_image and SDL2_mixer.  The last -lm links math(s) code; technically the -l{name} flag tells the linker to link against lib{name}. So -lm links against libm, the c math library.

I know a fair bit of C but

I know a fair bit of C but

The letter C in fancy script
Image by Gordon Johnson from Pixabay

There are a lot of subtleties that you only pick up with experience.  I was pleased to find a blog entry by a bloke Tom M on “Everything I wish I knew when learning C” which is well worth a read.

I’m not going to copy anything from it except for this little snippet below one below. For the rest, you’ll have to read his blog post.

char signedness

All other integer types default to signed, but bare char can be signed or unsigned, depending on the platform.

As said char is an integer type but unless you are doing stuff like ++ or — when its value can overflow according to being signed or unsigned, it’s not really a problem. And unless you are writing code that runs on wildly differing platforms, you can probably safely assume that a char is the same as a byte and has 8 bits. On those other platforms, I’d suggest you read the answers to this Stackoverflow question.

Update to the Windows eBook

Update to the Windows eBook

Visual Studio configurationA reader asked me how to setup SDL2 for Windows given recent changes in SDL2. Specifically the files and libsdl projects have been moved from the libsdl.org website to GitHub. You can easily find SDL2 Image, Mixer, TTF etc.

However it can still be quite daunting setting up Visual Studio for SDL2. You have to download the specific files, then configure the project properties to specify the include paths for header files and then the lib files, both the path to them and identify the ones you want to use.

As I’m on my new PC, I bit the bullet and went through the process of setting it up. It took just over an hour to configure it.  I’ve put it into a PDF that’s a couple of pages long.

So the game works but only after I disabled the sound code; it was failing in the call to Mix_OpenAudio(). I think recent work on the SDL Mixer needs some work on my part. I need to sit down and look at the SDL Mixer page and figure out what’s failing. Once that’s done, I’ll update the files.