Posts

Safely Swallowing Up Spaces or Non-Spaces

Can you explain why this is bad? (to swallow up spaces) if( ' ' == *instring)   while( ' ' == *instring && *(instring++)); The idea? You encounter a space, and now, while you see spaces and not the NULL ('\0') character, you keep advancing the string pointer. Bad. But why? It's not that dangerous - just not very readable. Better to have  while( ) instring++; What is really dangerous? Swallowing up non-spaces using while( ' ' != *instring && *(instring++)); The idea - if the character you see is not a space and not the NULL character, advance the string pointer. But what actually happens? Exactly! The pointer advances EVEN in the case of the NULL ('\0') being encountered at *instring. So, always do while( *instring && ' ' != *instring ) instring++; Much more readable. Yes, in this above snippet, you can be cute and do ' ' != *(instring++);

Underwhelming VS Code Tips Video of Potential Use to You

Image
Thank you Mayur font ligiatures editor formatting multiple cursors rainbow csv live preview (see your HTML/CSS rendered quickly) httpyac (API - do GET/PUT stuff within VS Code for quick debug/dev) bookmarks pdf diagrams code runner

A Critique of Jason Fedin's Course on Udemy

Image
It's a survey of everything. That may be okay. It's a lot of work and difficult to convey the concepts for sure. But, if it were me, with his store of knowledge, I'd approach it as : here is a complex project I accomplished and these are the advanced C concepts that were needed to make it successful. Thoughts?

You Want VS Code to Collapse *ALL* (Comments and Functions)

Then, this is what your keybindings.json needs to contain (highlighted bit): [     {         "key" : "ctrl+alt+/" ,         "command" : "workbench.action.editorLayoutSingle"     },     {         "key": "ctrl+alt+shift+f",         "command": "editor.foldAll",         "when": "editorTextFocus"     } ]

A Tasty Morsel

Given this requirement: The conversion rate you should use is 6.75 CNY for every 1 USD. All numbers should be represented as a string with 2 decimal places. (e.g. "21.00" NOT "21.0" or "21") Why does this code: char *USD_to_CNY (long dollars, char *yuans) {   sprintf(yuans, "%.2f Chinese Yuan", 6.75*dollars); // write to yuans   return yuans; // return it } Given this problem: for 122978293824730344 dollars, expected "830103483316929822.00 Chinese Yuan", but got "830103483316929792.00 Chinese Yuan" The answer : the input uses 54 bits, and your type has insufficient precision. So, use long long with 675 instead of 6.75, and do a divide by 10 at the sprintf. The title - from a book in which I read this joke: "What do a passionate kiss and a spider have in common? Both lead to the undoing of the fly."

A Classic C Programming Mistake - See if You Can Spot it

size_t *sz_out; *sz_out = input[sz_in-1] - input[0]+1; int * output = (int *) malloc( *sz_out ); Nail it?  That's right, you have to be specific in the malloc call. If you give it just a number (in this case, sz_out is a number of bytes - size of output) it'll allocate that many bytes - not what you want.  You always use malloc along with the sizeof operator to get the allocation right. Live and learn.

Poor Documentation - How Could You Do this to Us K&R?

Exercise 4-3. Given the basic framework, it's straightforward to extend the calculator. Add the modulus (%) and unary minus operators. Add an "erase" command which erases the top entry on the stack. Add commands for handling variables. (Twenty-six single-letter variable names is easy.) Really? How about some examples that tell us what to expect for us to parse? Chatting with GPT, I get variable assignment :  <value> <variable> := And for unary minus: <value> u- So, essentially, we have new operators. How are we supposed to know what to expect? What will getop send us - given that we're to expect a single character? That's one thing. Now, given that 'c' is a command that tells you to do something, how are you supposed to deal with a variable named c, which they claim is easy? Actually, looks like it's a hack by Dr. Chuck. The actual textbook has the getop discussed before the exercise, which makes things easier to understand..