Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

c code story

Name: Anonymous 2011-12-25 0:50

this one time i went to these guys and i wall all "hey guys check out my c code" and they were all "dude your code sucks" and i was all "no dude nothing is wrong with my code" and then the guy was all "you used a break statement in nested while loops" so i said to him i says "dude thats totally legit" and hes all "dude your noob programmer if you compile that in a very specific way then there is a chance that the code wont work" so i told him right to his face that he is a faggot and wrong because my code worked and i didn't care if it wouldn't work compiled that way because maybe i won't compile it that way

and thats my story i hope you enjoyed it

Name: Anonymous 2011-12-25 0:56

>>1
lisp is shit

Name: Anonymous 2011-12-25 2:15

This is one reason I will never be a great C programmer. There's decades of idioms and gotchas you have to know about.

Name: Anonymous 2011-12-25 2:47


#define Hello int main
#define prog (){
#define I int i;
#define just for(i=0;i<5;++i)
#define ate {
#define toast printf("%s", Best Story Ever");
#define The }
#define End }

Hello prog
I just ate toast
The End

Name: Anonymous 2011-12-25 3:19

>>4
Holy fuck I love it

Name: Anonymous 2011-12-25 3:29

>>4,5
Nice syntax error
printf("%s", Best Story Ever");

Name: Anonymous 2011-12-25 13:30

If it's not C, it's shit.

Name: Anonymous 2011-12-25 13:44


#include <stdio.h>
void main(short i) {
  while(i++ < 5)
    puts("Best Story Ever");
}

omg optimized

Name: Anonymous 2011-12-25 16:06

const char msg[] = \
  "Best Story Ever\n"
  "Best Story Ever\n"
  "Best Story Ever\n"
  "Best Story Ever\n"
  "Best Story Ever\n";

int main() {asm ("int $0x80" : : "a" (4), "b" (1), "c" (msg), "d" (sizeof(msg)+1));}


OMG OPTIMIZED

Name: Anonymous 2011-12-25 16:29

>>9
Windows? Ha ha, faggot!

Name: Anonymous 2011-12-25 16:34

>>7
C is crap.

Name: Anonymous 2011-12-25 19:00

>>10
int $0x80 is a Linux system call. I have no idea what you're talking about.

Name: Anonymous 2011-12-25 19:06

Why do people declare main as int and then not return anything?

Name: Anonymous 2011-12-25 19:27

>>11
Lisp is worse shit.

Name: Anonymous 2011-12-25 20:07

>>3
Should've started when you were 12.

Name: EXPERT C PROGRAMMER 2011-12-25 20:31

#include <stdio.h>
#include <stdlib.h>

#define foreach(a, f)                                   \
    do {                                                \
        int i;                                          \
        for (i = 0; i < sizeof (a) / sizeof *(a); i++)  \
            (f)((a)[i]);                                \
    } while (0)

int main(void) {
    foreach(((char *[]){ "Best Story Ever"
                       , "Best Story Ever"
                       , "Best Story Ever"
                       , "Best Story Ever"
                       , "Best Story Ever"
                       }), puts);
    return EXIT_SUCCESS;
}

Name: Anonymous 2011-12-25 20:36

>>15
I wrote my first ANSI C compiler, when I was 12.

Name: Anonymous 2011-12-25 20:50

>>16
Bloated

Name: Anonymous 2011-12-25 20:59

So can any C experts elaborate on break statements in nested while loops because I use them all the time without a problem.

Name: >>16 2011-12-25 21:31

>>19
there is no problem?

Name: Anonymous 2011-12-25 22:16

You can avoid using break statements within loops when you design the loops to halt at a flag variable.

Name: Anonymous 2011-12-25 22:20

>>21
It makes the code look like shit
Compare

while(a) {
  while(b) {
    if(c)
      break;
    }
  }
}


to


continueProcessing = 1;
while(a) {
  while(b) {
    if(c && continueProcessing)
      continueProcessing = 0;
  }
}

Name: Anonymous 2011-12-25 22:21

No.

Name: Anonymous 2011-12-25 22:27

>>22
So write a better design.
while (!endgame)
{
  while (code==CODEBLUE)
  {
    // do stuff
    if (gameaction==PLAYERQUIT)
    {
      gameaction=CODERED;
      endgame = true;      
    }
  }
}

Name: Anonymous 2011-12-25 23:20


int i = 0;

while (1)
{
    i += 2
    if (i == 8)
    {
        break;
    }
}


I don't see anything wrong with this in terms of efficiency.

Name: Anonymous 2011-12-25 23:23

>>25

That's equivalent to:


for (int i = 0; i == 8; i += 2)
{
    continue;
}

Name: Anonymous 2011-12-25 23:39

>>26

But the while loop has to check that 1 == 1 each time it runs through the loop, whereas the for loop doesn't. So the while loop does 13 operations, whereas the for loop only does 8 operations.

A problem with the for loop is that the compiler will optimise the loop away, because it does nothing.

Name: Anonymous 2011-12-25 23:53

>>27
But the while loop has to check that 1 == 1 each time it runs through the loop
No it doesn't it's a constant expression.

A problem with the for loop is that the compiler will optimise the loop away, because it does nothing.
It alters the value of i.

Name: noko 2011-12-25 23:56

#include <stdio.h>

int main() {
  int i = 0;

  while( i < 5 ) {
    printf("%s\n", "Best story ever!");
    ++i;
  }

  return 0;
}

Name: Anonymous 2011-12-25 23:57

>>13
compiler will make it return 0 if not specified.

Name: Anonymous 2011-12-26 0:03

>>30
any behavior which varies between compilers is discouraged.

Name: Anonymous 2011-12-26 0:11

>>28

>It alters the value of i.

It's part of the loop counter though. The compiler is smart enough (or if you actually need the loop to delay, then it's stupid enough) to actually optimise it away.

Name: Anonymous 2011-12-26 0:12

>>31
no fake, but when you only use one compiler and not adding to some open source project let alone a private one they don't care.

>>27
1==1
no it doesn't, that would have to be the stupidest compiler ever to even assemble to that.


[ @ ~/host/prog/story ] $ cat st.c
#include <stdio.h>

void forloop(void)
{
    int i;
    for(i=0;i==8;++i)
        continue;
}

void whileloop(void)
{
    int i = 0;
    while(1){
        i+=2;
        if(i == 8)
            break;
    }
}


int main()
{
    forloop();
    whileloop();
}
[ Mon Dec 26 12:08:40 ]
[ @ ~/host/prog/story ] $ cat st.s
    .file    "st.c"
    .text
.globl forloop
    .type    forloop, @function
forloop:
.LFB0:
    .cfi_startproc
    pushq    %rbp
    .cfi_def_cfa_offset 16
    movq    %rsp, %rbp
    .cfi_offset 6, -16
    .cfi_def_cfa_register 6
    //int i = 0;
    movl    $0, -4(%rbp)
    jmp    .L2
.L3:
    //++i
    addl    $1, -4(%rbp)
.L2:
    //i==8
    cmpl    $8, -4(%rbp)
    je    .L3//continue
    leave
    ret
    .cfi_endproc
.LFE0:
    .size    forloop, .-forloop
.globl whileloop
    .type    whileloop, @function
whileloop:
.LFB1:
    .cfi_startproc
    pushq    %rbp
    .cfi_def_cfa_offset 16
    movq    %rsp, %rbp
    .cfi_offset 6, -16
    .cfi_def_cfa_register 6
    //int i = 0;
    movl    $0, -4(%rbp)
    jmp    .L7
.L9: //while(1)
    nop
.L7:
    //i+=2;
    addl    $2, -4(%rbp)
    //i==8?
    cmpl    $8, -4(%rbp)
    jne    .L9//i!=8
    nop
    leave
    ret
    .cfi_endproc
.LFE1:
    .size    whileloop, .-whileloop
.globl main
    .type    main, @function
main:
.LFB2:
    .cfi_startproc
    pushq    %rbp
    .cfi_def_cfa_offset 16
    movq    %rsp, %rbp
    .cfi_offset 6, -16
    .cfi_def_cfa_register 6
    call    forloop
    call    whileloop
    leave
    ret
    .cfi_endproc
.LFE2:
    .size    main, .-main
    .ident    "GCC: (Debian 4.4.5-8) 4.4.5"
    .section    .note.GNU-stack,"",@progbits

Name: Anonymous 2011-12-26 0:21

>>33
also to add some application to the loops:

[ @ ~/host/prog/story ] $ cat st.c
#include <stdio.h>

void forloop(void)
{
    int i;
    for(i=0;i!=8;i+=2)
        puts(">>33 is a faggot");
}

void whileloop(void)
{
    int i = 0;
    while(1){
        puts(">>33 is a faggot");
        i+=2;
        if(i == 8)
            break;
    }
}


int main()
{
    forloop();
    whileloop();
}



gcc -S st.c

[ Mon Dec 26 12:19:33 ]
[ @ ~/host/prog/story ] $ cat st-O0.s
    .file    "st.c"
    .section    .rodata
.LC0:
    .string    ">>33 is a faggot"
    .text
.globl forloop
    .type    forloop, @function
forloop:
.LFB0:
    .cfi_startproc
    pushq    %rbp
    .cfi_def_cfa_offset 16
    movq    %rsp, %rbp
    .cfi_offset 6, -16
    .cfi_def_cfa_register 6
    subq    $16, %rsp
    movl    $0, -4(%rbp)
    jmp    .L2
.L3:
    movl    $.LC0, %edi
    call    puts
    addl    $1, -4(%rbp)
.L2:
    cmpl    $8, -4(%rbp)
    jne    .L3
    leave
    ret
    .cfi_endproc
.LFE0:
    .size    forloop, .-forloop
.globl whileloop
    .type    whileloop, @function
whileloop:
.LFB1:
    .cfi_startproc
    pushq    %rbp
    .cfi_def_cfa_offset 16
    movq    %rsp, %rbp
    .cfi_offset 6, -16
    .cfi_def_cfa_register 6
    subq    $16, %rsp
    movl    $0, -4(%rbp)
.L7:
    addl    $2, -4(%rbp)
    cmpl    $8, -4(%rbp)
    je    .L10
.L6:
    movl    $.LC0, %edi
    call    puts
    jmp    .L7
.L10:
    nop
.L9:
    leave
    ret
    .cfi_endproc
.LFE1:
    .size    whileloop, .-whileloop
.globl main
    .type    main, @function
main:
.LFB2:
    .cfi_startproc
    pushq    %rbp
    .cfi_def_cfa_offset 16
    movq    %rsp, %rbp
    .cfi_offset 6, -16
    .cfi_def_cfa_register 6
    call    forloop
    call    whileloop
    leave
    ret
    .cfi_endproc
.LFE2:
    .size    main, .-main
    .ident    "GCC: (Debian 4.4.5-8) 4.4.5"
    .section    .note.GNU-stack,"",@progbits



gcc -O3 -S st.c

[ Mon Dec 26 12:19:44 ]
[ @ ~/host/prog/story ] $ cat st.s
    .file    "st.c"
    .section    .rodata.str1.1,"aMS",@progbits,1
.LC0:
    .string    ">>33 is a faggot"
    .text
    .p2align 4,,15
.globl whileloop
    .type    whileloop, @function
whileloop:
.LFB12:
    .cfi_startproc
    subq    $8, %rsp
    .cfi_def_cfa_offset 16
    movl    $.LC0, %edi
    call    puts
    movl    $.LC0, %edi
    call    puts
    movl    $.LC0, %edi
    call    puts
    movl    $.LC0, %edi
    addq    $8, %rsp
    jmp    puts
    .cfi_endproc
.LFE12:
    .size    whileloop, .-whileloop
    .p2align 4,,15
.globl forloop
    .type    forloop, @function
forloop:
.LFB11:
    .cfi_startproc
    subq    $8, %rsp
    .cfi_def_cfa_offset 16
    movl    $.LC0, %edi
    call    puts
    movl    $.LC0, %edi
    call    puts
    movl    $.LC0, %edi
    call    puts
    movl    $.LC0, %edi
    addq    $8, %rsp
    jmp    puts
    .cfi_endproc
.LFE11:
    .size    forloop, .-forloop
    .p2align 4,,15
.globl main
    .type    main, @function
main:
.LFB13:
    .cfi_startproc
    subq    $8, %rsp
    .cfi_def_cfa_offset 16
    movl    $.LC0, %edi
    call    puts
    movl    $.LC0, %edi
    call    puts
    movl    $.LC0, %edi
    call    puts
    movl    $.LC0, %edi
    call    puts
    movl    $.LC0, %edi
    call    puts
    movl    $.LC0, %edi
    call    puts
    movl    $.LC0, %edi
    call    puts
    movl    $.LC0, %edi
    addq    $8, %rsp
    jmp    puts
    .cfi_endproc
.LFE13:
    .size    main, .-main
    .ident    "GCC: (Debian 4.4.5-8) 4.4.5"
    .section    .note.GNU-stack,"",@progbits



As you can see with O3 the compiler is smart enough to optimize both loops to not even do a loop or any jumps and just does 4 prints and then in the main method it just does 8 prints instead of even using call on those two methods.

Name: Anonymous 2011-12-26 0:21

>>33

The for loop header should be:

[code]
for(i=0;i==8;i+=2)
[code]

You made it increment by 1.

Name: Anonymous 2011-12-26 0:23

>>35

Herp.


for(i=0;i==8;i+=2)

Name: Anonymous 2011-12-26 0:29

>>36
>>35
i noticed but didn't care to fix it.
it wouldn't make a big difference anyway.

addl    $1, -4(%rbp)
//would turn into:
addl    $2, -4(%rbp)

Name: Anonymous 2011-12-26 0:29

>>34

This isn't helpful though if you want to make the loop wait so you can flash an LED on and off on a microcontroller.

Name: Anonymous 2011-12-26 0:34

>>38
I don't see what you're getting at. The compiler will optimize both loops regardless of what you do inside if they are structured like that.

Name: Anonymous 2011-12-26 0:37

>>39

We did some microprocessor control stuff. By setting the clock multiplier to a certain value, you create a paced loop where the pacer counts to a certain number, which creates a time delay based on what the multiplier sets the clock to. If the compiler optimises the loop, wouldn't it modify the time delay to, say, make an LED flash on and off every 500ms?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List