print 1 to 10 numbers with array in c

Ezzaral commented: Wow, you're a well of useless questions... and in the wrong forum too! -3

Recommended Answers

All 9 Replies

... and here is how you would do this with Python:

arr = [12, 7, 9, 25, 87]

for item in arr:
    print(item)

Just to compare the syntax, here is the same thing in C:

#include <stdio.h>

int main()
{
    int arr[] = {12, 7, 9, 25, 87};
    int i;
    
    for(i = 0; i < sizeof(arr)/sizeof(int); i++)
    {
        printf("%d\n", arr[i]);
    }

    return 0;
}

There is "no beauty" in even simple C code! Let's all thank the Python development team for giving us a readable syntax. Come to think of it, they all were very fluent in C since the core of Python is writtten in C.

I guess the true beauty of C is somewhat hidden, it can do low level stuff more readable than assembly language.

Its true ! Thanks Python Dev team. I saw one example from Ene where there was no indentation, I whoop it was horrible!
Anyway it is more readable than assembly but wait a minute snee, Assembly is more readable than 10000100101111, I mean a turbulent of machine code language!

There is "no beauty" in even simple C code!

"Beauty is only in the eye of the beholder".
Those that know, find a well written piece of code in C very motivating and easy to work with.
The comparison of languages as it has been described here, only shows the lack of knowledge. for(i = 0; i < sizeof(arr)/sizeof(int); i++) There's no need to evaluate sizeof(arr)/sizeof(int) every single time in the loop, since it never changes.

I don't agree, because the compiler can see that sizeof(arr)/sizeof(int) never changes, so it certainly generates the code with a constant value. Isn't it a lack of knowledge Aia ? Knowledge, beauty, great philosophical problems to think about !

I don't agree, because the compiler can see that sizeof(arr)/sizeof(int) never changes, so it certainly generates the code with a constant value. Isn't it a lack of knowledge Aia ? Knowledge, beauty, great philosophical problems to think about !

The compiler doesn't miraculously change that expression into a constant. It evaluates each time through the loop. An unnecessary waste of CPU cycles when making a single call and keeping the value would have been sufficient.

[As a way of clarification]
The comment about sizeof() was not the reference to lack of knowledge, but the comparison of languages base on syntax, which is why I said, "The comparison of languages as it has been described here".

I tried comparing the assembly code produced when you remplace sizeof(arr)/sizeof(int) by the constant 5. The diff between the 2 programs is

<       cmpl    $4, -8(%ebp)
<       jle     .L3
---
>       movl    -8(%ebp), %eax
>       cmpl    $4, %eax
>       jbe     .L3

So the difference is only one movl instruction.

>So the difference is only one movl instruction. cmpl $4, -8(%ebp) is not the same that cmpl $4, %eax jle .L3 in not the same that jbe .L3 movl -8(%ebp), %eax is in a loop by virtue of "Jump if Below or Equal" Having the opportunity to execute many times. So it is not "only one mov1 instruction" executed only once.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.