12
b3b3
7y

So in my class the teacher wanted us to write a little program (not going into further details) and therefore we needed to convert chars from a string to integers.
My classmates are starting like:
if (c = '0')
return 0;
else if (.....

And so on. I was just like duuuuuh wtf are u doing? I was actually screaming when I had a look at my neighbours screen.

I personally am like "return c - 48;"

Comments
  • 2
    return c - '0';

    It's more comprehensible, but still lacks some checks for correct decimal values and it implies decimal number system
  • 0
    @ddephor that one is nice too :) easier to understand I guess. We didn't need any checks in that case but I actually did one in my code
  • 2
    I really learned about ASCII when we had to do our first project in Assembly... We hat to create a Ceasars Cipher - encryptor in M68K Assembly. That shit is awesome
  • 1
    @wholl0p i actually just had a look at x64 asm. Sadly I cannot learn it by converting small c programs into asm because there's a big ton of shit going on so I need to learn it by tuts :(
  • 1
    @b3b3 that's true.. also converting or 'disassembling' doesn't give you the true instructions but only a kind of 'guess'... But anyways I think it is highly interesting!
  • 0
    @wholl0p a simple 5 lines of "Hello World" gave me 180 lines of asm, mostly some external stuff for the linker of whatever. I've been searching for 20h and it didn't find anything. So I guess my text is being printed by some shit I cannot find. But I'll continue learning :)
  • 3
    @b3b3 okay that can definitely be done in less that 180 lines :-0
  • 4
  • 2
    @wholl0p lol I just ran gcc again to convert my c code into asm in elementaryOS instead of centOS and it outputs asm I'm way more familiar with. Both using the same version of gcc. Wtf
  • 1
    @wholl0p kek ich wohn ziemlich nah bei dir 😜
  • 1
    @b3b3 Tatsächlich, gar nicht so weit entfernt :D

    Ja ich denke dass es eben daran liegt, dass du aus C-Code Assembly Code machst, und ich den Code direkt in Assembly geschrieben hab...
  • 0
    @b3b3 Compiler Explorer is exactly what you want: it compiled C++ to asm, and shows you the net compiled code minus the crud, plus shows which line compiled to which assembly commands.

    https://godbolt.org
  • 1
    @configurator Thanks! I'll have a closer look at it tomorrow. Its 4am and I spent the last 20 hours playing around with my fresh arch install
  • 0
    @wholl0p wo ist der Unterschied? C(++) wird doch assemled, gelinked und weiß der Himmel noch. Warum kommt dann beim Übersetzen von zB printf() nicht int 21h raus?
  • 1
    @b3b3 gut, bei meiner Version fehlen auch weitere Spezifikationen usw. Aber 180 Zeilen sind es (gottseidank) nicht :D
Add Comment