Learn C the Hard Way – Lucky I already thought of that

Because I was remapping the keyboard, I left my terminal in root, and ended up creating an exercise as root. So later, I couldn’t amend it. Options were chown, chmod, or just leaving it there and not caring. Fortunately, I already had exercise expansions and extra credit as different files, so it didn’t affect me. However, pro-tip here; don’t do that.

Learn C the Hard Way – packing away my ego

Ex 5. I’m pretty good at copying typing I can see, but unfortunately I’m also pretty good at remembering what I’ve seen in previous exercises, so I nearly made the argvs into ints. Brain managed to pull up at the last minute, though. However, that’s one to be very careful of in future.

Line 4, /this/ version of the main function needs that combo of ints and chars. I took a moment realising that that was not a universal truism for main().

Line 5, starting the body of the function, threw me for a loop. I’m learning this because I’m messing with an Arduino for my current project, Sparky. That means I’ve been basically copying and pasting and trying to make things work. So I’ve seen an if() that had brace pairs and an if() that didn’t. For a moment I was trying to over-ride what Zed says. Obviously here, the {} are only required if there’s more than one line…

So I’m going to believe him. That’s a very hard thing for me. And yeah, a bit later on we’re told that statements end in ; – except logic.

What’s going on in there is that we demand an int be returned, and then we return a 0 at the end, and in between we do all sorts of interesting stuff. I believe we could type

void main(int whatever)

if we wanted to input ‘whatever’ and get nothing out. We’d just do stuff inside the main function. That’s how the Arduino sets things up. It has void loop() and that loop just goes round and round and round, doing stuff but never actually outputting.

I think the only thing I’m unsure of is the char *argv[]. The [] means it’s an array. (I know that because I’m working out failure messages for Sparky, and that’s how they are filed.) But char* x is different to char *x and the * is worrying me. Pointer. It’s a pointer of some sort. But is *char a pointer to a character… well, colour me ignorant. I’m guessing, not looking this up. It’ll end up explained.

Learn C the Hard Way – Arch is a bastard for this and I’m fine with that

Ex 4.

Valgrind is available in the /extra repository in Arch. We’re going to make it ourselves, though. Fair enough. I’ve made a few packages as far as I recall, but not in a while. When I was initially setting up my machine, inkscape wasn’t so good and I was using inkscape-bzr and now I don’t need that any more.

$ tar -xf <FILE>

is enough to unpack most things. ‘eXtract File’. The tool will work out what sort of file it’s working on and just does it.

$ ./configure

– error. Valgrind works on kernels 2.4 and 2.6 – Arch is a rolling release. I’m on kernel 4.something. Soooo. Package manager it is. At some point I’ll need to work out what about the package has been changed to make it work in Arch. The README.aarch has instructions on cross-compiling and paths and hosts which have a shape that makes sense but I’m not looking at the detail. I’ve used pacman and I have valgrind on the system. I stared at the Makefiles that my attempt generated, though, in the hope of absorbing their strength by drooling. On with the lesson.

The output of valgrind is already similar to Zed’s, but at some point I may diverge – Arch is like that. For now, though, I’m getting error tracking.

By fixing the uninitialised variable in ex 4, I fix all the actual errors. This is odd, as valgrind doesn’t pick up the %d without a related integer, in the first printf function. I can’t see why that might be in the manual. However, I know what the program should say, so I know what to fix. An uninitialised variable is an error that could lead to unexpected problems, and valgrind picks it up. A variable that’s missing could lead to, well, also unexpected problems. But valgrind doesn’t pick it up. Odd. Maybe an Arch thing but I don’t think so. It was not there in Zed’s print-out either.

The compiler tells me what the other problem is, so I’ll deal with that. Variables are now initialised and don’t go unused, and all format sequences are filled.

I did some messing about with making downloaded packages, but I’ve got to look into Arch’s installation process to really understand that. pacman is a powerful tool, and like all powerful tools, it hates me right from the off. I know I can make a package and then have pacman install it from my local file, but for now I’m not using that. Onward, upward, and avoid the Pevensey children on the way.

Adventures in Keyboard Mapping

I have a laptop which was very kindly donated by a friend. It’s better than any of my other computers, which isn’t hard. However, it’s a US layout.

I’m currently using a VM to learn C, as I like Arch, but I’m doing that inside a windows interface, for which I’ve had to remap my keyboard. So, inside that VM I have Xwindows and consoles, and they all need mapping.

Well… I can copy and paste #~/?| and all those things. I don’t need them. As long as I have £. And ¬.¬ of course.

I don’t like emoticons.

Fuck.

Learn C the Hard Way – placeholders, thingies, format strings, and my brain holding too much

Ex 3:

Easy enough. I had to look up exactly what a format string was, but that’s fine. Zed flagged it up for looking. This is quite a lot like Python’s %s and %d functionality. Just different syntax.

So, trying to break the printfile, I found that

printf("%c", height);

would look up height (an int that gave numeric value) and find it was 72. So it would then look up the character 72 in ASCII, and return an upper case H. That’s the DEC version, by default.

Unicode gives me a WTF character. I assume I need a different library to deal with that.

A missing age variable gives me a ludicrous age, but doesn’t crash the program. I can embed the width of the returned number or string in the %symbol by using %+4i – width of 4 digits, including a sign that will always be shown or %10.3f – that would give a minimum width of 10 digits including a decimal point, and 3 figures after the point. A – will left justify shit. I’ve been told that C is really concise, and that’s a good example of using not very many characters to type stuff. It’s not exactly confusing form with function – the format characters sit over the stringy bits. They just sit over it inside the same bit of typing.

r is a return to the start of the line without a new line. I can overwrite the earlier part of a string with it.

So, I’ve got various ways of breaking printf(), but those are mostly bad typing. I can also accidentally cover over a result, or leave big spaces in a result.

The last bit of the Extra Credit wouldn’t be clear if I hadn’t done a bit of research already. The ‘list’ of the makefile isn’t, I think, a thing Zed’s defined. In Ex 2 I cleaned up a bit when I took out ex1 as a dependency for all:. Now, I’m going to make it so ‘clean’ and ‘all’ have the lists I want inside them. For the moment I’m typing out all the names, but I’m aware there’s a way I can substitute in everything that’s in a folder, and stuff like that. I don’t mind having had to look up what the list and the dependencies were – Zed’s trying to make new programmers thing for themselves. I am concerned I’ll be trying to look up too much, which is a problem I always have. Knowing when to stop is a skill you don’t pick up until you know what’s important, and you don’t know what’s important until you’ve practiced for a while. So, every time my brain seems too full, I’ve probably gone too far.

www.le.ac.uk/users/rjm1/cotter/page_31.htm has a good list of width/precision stuff, but the examples are odd.

printf("%s,Hello")

– I’ve eMailed the owner there to ask how the heck* that works. It breaks my program by printing out the last parts of my previous lines. It compiles (badly) and then it betrays me. Le whut?

* I used different words.

Go to L – milling experience

A cog with a slit and an L-shaped piece that fits the slotWhile waiting for client feedback, I came across a couple of people (F. and J.) who were picking apart a broken mechanism, but couldn’t find an easy way to fix it. It had some 3D printed parts, and the ABS had twisted apart, and was jammed inside because it had a flared end. So, I milled down some plate steel while J. drilled out the broken plastic and worked out how to keep a spring out of the way, then we fitted the steel key. In place of the flared end we put in a notch and wired the steel into position. The top needed a star shaped meshing mechanism, so I cut that out of plywood on the laser cutter.

The steel cut pretty easily, but I had to be careful with the vice, as it was thin and I got to the point where I couldn’t safely hold it, and we were very lucky that was a perfect fit for the mechanism. I could take off a 1mm step with the 16mm end mill, and I did that repeatedly, with lube, and it works just fine.

We need more molyslip.

Learn C the Hard Way – Making and making and GNUmaking and throwing things across the room

Ex 2 of Learn C the Hard Way introduces how the makefile works. I’ve tried and failed to use this before, when trying to make an Arch package, so it’ll be good to work out how to do that. I’m not looking forward to failure, of course. This blog is basically a lie.

emacs Makefile

and I created the file as I was told to. My ex1 still has

#include<stdio.h>

in it, so it didn’t throw up any warnings, but when I commented that line out, it did. So my computer’s now in much the same state as what Zed was using to write, and I’m not getting my compiler screaming where his wasn’t. I did have to read down his page to a note saying that he had an unfixed ex1, but I was pretty sure that’s what had happened. I’d put that note up at the top.

If I have two tabs instead of none, I don’t have any errors thrown. This is unlike Python’s unexpected indent. I wonder why it needs an indent at all, but I’m going to go with BECAUSE and ignore that for the moment.

I had to go looking for more information for extra credit. According to
www.cs.colby.edu/maxwell/courses/tutorials/maketutor
‘make’ on its own will execute the first command in the file. So, my first attempt at putting all: ex1 into the file was after clean: and it didn’t work. My second attempt had line breaks, which was a guess at how to separate functions. I then went without separating functions at all. -o is a flag that should take one file name, and then the next bit of info should be separate by default. And whaddyaknow, this works. I have, readers may have noticed, a way of making things difficult for myself, mostly by assuming they will be more complicated than they actually are.

I’ve decided I want to add -d to the makefile flags, if that does what I think it will. I like debugging info.
-o seems to mean we’ve got an old file, which is… odd. So it must be that the internal makefile syntax and the syntax we use to call it are different. It sort of makes sense.

man make

has a typo in it.

$ man cc

– no manual entry for cc. Yerwhat? I assume cc is the c compiler…

$ which cc

– /usr/bin/cc . So I have it. I just don’t have a man page.

$ man -k '^cc$'

– nothing appropriate. This isn’t about an adult filter, either. This man page isn’t in my distro. So, over to

https://www.ma.utexas.edu/cgi-bin/man-cgi?cc+1

to get my fix. And aaaah. ‘gcc’ exists.

$ man gcc

– Result! And wow, that’s complicated. I mean, that’s a whole set of stuff that I could do, with huge options lists I can’t (yet) hope to comprehend. But here I am failing in public, so let’s look at the last bit of the Extra Credit. I don’t know what ‘better’ means in this case, for a future case of what a Makefile does.

Online research – http://mrbook.org/blog/tutorials/make/ is helpful. It’s coming from the angle of people who can code and want to automate it, but it’s still explaining things simply. all: is a default. Anything after the colon is dependencies. I think this should be expanded and explained more. I’m not getting it from Zed’s explanation, and a couple of sentences there would have made the first part of the Extra Credit much easier to understand. I think I’m learning too far ahead of things, though. I went back and stopped ex1 from being a dependency of all: and that’s where I’m leaving it. I’ve cruised through a few examples, but there’s too much to hold in my head to stay here. I need to move on and see how it works.

Learn C the Hard Way – I do everything the hard way

For those joining me in my world record yak shaving attempt, today I started poking at Learn C The Hard Way by Zed Shaw. It’s currently in its beta form, as he’s writing it in public so people can look and learn and proof-read, and I have a reason to learn enough C to get by. Sparky, the Spark Eroder, needs an output method to help me understand what’s going on in his insides. I can’t afford an oscilloscope, so a £3 microprocessor is going to do the job.

I say ‘a’ microprocessor, but when I bricked the first, I ordered two more. If I’d bricked an oscilloscope, two more would have come in beyond budget.

So, Zed has written up the two cases of Debian and RPM based Fedora, for the installation of what I’ll need. I work on Arch. There are reasons. They are not and can never be adequate reasons, but I’m sticking to them. So, the first thing I did was Exercise 1, to see what would break. If I could make Ex 1 work, then I’d be set up right. If not, I’d have error messages.

I had error messages!

ex1.c: In function 'main':
ex1.c:3: warning: implicit declaration of function 'puts'

Well, he explains this in the Ex 1 page – I’ve got flags set that will show errors. This one is a warning, and if I were on Debian or Fedora or in fact anything sane, it (probably) wouldn’t be a problem. But I’m not, and it is. So now I have to work out WTF the flags have to be. He can show me how to turn the flags /on/ and break things. I’m looking to turn the flags /off/ and mend things, or at least suppress them so I live in bliss and ignorance.

Turning flags off in the future is probably a bad idea, and I’m going top be keeping them on in general, so if I run into this again I can flag it up, but I want to know how it’s going wrong. It also meant I didn’t type the version with the flags turned on.

$ man make

It seems that there’s a –warn-undefined-variables flag, but I can’t work out how to turn it off. There’s also a –touch flag, which is interesting. It’ll just poke (and possible create, I wasn’t doing more than skimming) files so they have updated timings for when I want to make a latest version of a set of files. That’s probably not useful for me yet, but the notion that

make

is so powerful is written all over the manual file. I can define different files as the ones to use, regardless of which are most recent, and I can deliberately override dependencies. So that tells me that make handles dependencies and stuff like that.

On with the show. I can make, and use, ‘ex1.c’. It prints ‘Hello World.’. It’s a capital letter on the second word because that’s how I roll. Oddly, I’ve found that the tighter my copying of code gets, and the more I’m concentrating, the more likely I am to typo in the parts where there’s no penalty for error.

Extra Credit:

I didn’t edit the original exercise file. I made a second file so I’d be typing the same things again, so I’ve got ex1a as well as ex1. I’m pretty sure that’s better, as I can compare programs if I have to.

I worked out what stdio has to stand for. It’s not short for Studio. It means ‘standard in-out’. The ‘.h’ means it’s a header file. I looked up the second bit. The first bit I knew through failing repeatedly at Linux everything, and I’m OK with that.

I managed to get a segmentation fault by using ‘ instead of ” as line breaks. I also need a work-around for my American keyboard which doesn’t play nicely with my locale settings. I’m going to have to give up the £ to get a # and a working pipe that doesn’t need five key presses and a swift prayer.

man 3 puts

– The ‘3’ means it’s in part 3 of the linux user manual, which deals with C library functions. So,

#include

means you’re explicitly declaring the existence of puts() in the library you include, instead of implicitly calling it and relying on the compiler to make up for your error.

It’s all downhill from here

This is a blog dedicated to the making that Diana does, often in the company of others, often to the exclusion of eating, sleeping, or personal hygiene. It’s also dedicated to those who help her, and those who glaze over and rock quietly when she talks to them.

You helped!

It’s a space in which learning will often be accompanied by failure and the word ‘fuck’ whispered quietly but vehemently as the magic smoke escapes from something expensive, or with a tiny grinding sound, a drill bit breaks in a piece of steel.

Fuck.