Posts

Showing posts from January, 2025

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..

The Sad Messiness of VS Code

There is NO way to hide binary files in the Explorer view. No way!!! Shame on your MSFT! I'm having the setup gcc in tasks.json to output a .o so that files.exclude in the settings.json can do it.. A sad day :(

Video Playlist

https://www.youtube.com/playlist?list=PLlRFEj9H3Oj5NbaFb1b2n8lib01uNPWLa

An Elegant Problem for You - Why HW Takes So Long to Run on WSL if Using VS Code

When VS Code (I wasn't able to set the gcc that I have in the WSL terminal to be "the" compiler, though, following Goog's advice, I did go to command palette and say wsl-remote new window.. This is what I see (I can't find the exact compile command) the VS Code terminal when I "Run without Debugging" & 'c:\Users\anant\.vscode\extensions\ms-vscode.cpptools-1.23.2-win32-x64\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-iet5cxoy.sqy' '--stdout=Microsoft-MIEngine-Out-vq0tqhub.lns' '--stderr=Microsoft-MIEngine-Error-xyos54mn.10t' '--pid=Microsoft-MIEngine-Pid-3ateclpj.3ff' '--dbgExe=C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\gdb.exe' '--interpreter=mi' VS Code compiled output for a prog that prints a simple message, scanf's an int and prints out that minus 1 : 246 bytes --> 55.03k gcc : 16k But, if you run the ./hello.exe, it takes 5 s...