• Is this idiomatic C? (Declare where needed vs. up front)

    From Chris M. Thomasson@3:633/10 to All on Sun May 24 03:49:50 2026
    Anybody up for some C, if so, C the following:

    ________________
    int main(void)
    {
    int hello_0 = 12;

    {
    int hello_1 = 30;

    int hello_2 = hello_0 + hello_1;
    }

    return 0;
    }
    ________________

    Declare where needed vs, all, well, we know, up front?

    [...]

    Declare where needed vs. all up front, as a pun for C... All shit up
    front! eyes forward, init to zero, march.

    Let the flame rise! ;^o

    --- PyGate Linux v1.5.14
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From makendo@3:633/10 to All on Sun May 24 19:49:20 2026
    ? 2026/5/24 18:49, Chris M. Thomasson ??:
    int main(void)
    {
    ÿÿÿ int hello_0 = 12;

    ÿÿÿ {
    ÿÿÿÿÿÿÿ int hello_1 = 30;

    ÿÿÿÿÿÿÿ int hello_2 = hello_0 + hello_1;
    ÿÿÿ }

    ÿÿÿ return 0;
    }

    $ gcc -S -O2 -Wall -march=x86-64-v3 -x c -o - -
    .file "<stdin>"
    <stdin>: In function ?main?:
    <stdin>:8:13: warning: unused variable ?hello_2? [-Wunused-variable]
    .text
    .section .text.startup,"ax",@progbits
    .p2align 4
    .globl main
    .type main, @function
    main:
    .LFB0:
    .cfi_startproc
    xorl %eax, %eax
    ret
    .cfi_endproc
    .LFE0:
    .size main, .-main
    .ident "GCC: (GNU) 16.1.1 20260515 (Red Hat 16.1.1-2)"
    .section .note.GNU-stack,"",@progbits


    --- PyGate Linux v1.5.14
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From David Brown@3:633/10 to All on Sun May 24 14:11:49 2026
    On 24/05/2026 12:49, Chris M. Thomasson wrote:
    Anybody up for some C, if so, C the following:


    It's nice to have an actual C question, though it is more about style
    and preference. But please cut the childish babble - it does not add
    anything useful.

    ________________
    int main(void)
    {
    ÿÿÿ int hello_0 = 12;

    ÿÿÿ {
    ÿÿÿÿÿÿÿ int hello_1 = 30;

    ÿÿÿÿÿÿÿ int hello_2 = hello_0 + hello_1;
    ÿÿÿ }

    ÿÿÿ return 0;
    }
    ________________

    Declare where needed vs, all, well, we know, up front?


    A lot of people like to declare variables only when they are ready to be initialised. And it is quite common to avoid changing variables
    afterwards, unless there is obvious need (like a loop index) or it is
    clearer in code - if you are calculating a new value, it can be helpful
    to introduce a new variable with a new name for the purpose. This can
    make it easier to see in code where things come from - when you are
    looking at later lines in the function and wondering where the variable "hello" is set, if it is only set in one line of the code then you can't accidentally miss a later change.

    Obviously you need to pick decent names for this - suffixing a number is unlikely to be a good strategy for clear code.

    It is also worth declaring these variables as "const". Then everyone - authors, readers and compiler - can clearly see that they are not changing.

    Using new variables rather than re-using old variables does mean that
    you need an optimising compiler for efficient results, as the compiler
    will do the lifetime analysis and re-use registers or stack slots - but there's never much point thinking much about efficiency while using a non-optimising compiler.



    --- PyGate Linux v1.5.14
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Lawrence D?Oliveiro@3:633/10 to Unknown on Mon May 25 04:08:53 2026
    On Sun, 24 May 2026 03:49:50 -0700, Chris M. Thomasson wrote:

    Declare where needed vs, all, well, we know, up front?

    As needed, of course. Also handy being able to declare loop index
    variables directly in the loop header:

    for (PyTypeObject ** e = types;;)
    {
    if (*e == NULL)
    break;
    if (PyType_Ready(*e) < 0)
    break;
    if (PyModule_AddObject(modu, (**e).tp_name, (PyObject *)*e) < 0)
    break;
    ++e;
    } /*for*/
    if (PyErr_Occurred())
    break;

    --- PyGate Linux v1.5.15
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)