0

i am new to c and i am trying to sort an array values in a ascending order and i noticed a difference in my program when i do :

int t[5] = {5 , 4 , 3 , 2 , 1 };

and when i do :

int t[5];

t[0] = 5;
t[1] = 4;
t[2] = 3;
t[3] = 2;
t[4] = 1;

what is the difference ?

EDIT: THE DIFFERENCE :

first program :

int t[5] = {5,4,3,2,1} ;

int i,aux;

for(i=0;i<5;i++)
    if(t[i] > t[i+1])
        aux=t[i];
        t[i]=t[i+1];
        t[i+1]=aux;

for(i=0;i<5;i++)
    printf("Val : %d\n" ,t[i] );

the output of the first program :

Val : 5
Val : 4
Val : 3
Val : 2
Val : 1

the second program :

int t[5] ;
t[0] = 5;
t[1] = 4;
t[2] = 3;
t[3] = 4;
t[4] = 5;
int i,aux;

for(i=0;i<5;i++)
    if(t[i] > t[i+1])
        aux=t[i];
        t[i]=t[i+1];
        t[i+1]=aux;

for(i=0;i<5;i++)
    printf("Val : %d\n" ,t[i] );

the second program output :

Val : 5
Val : 4
Val : 3
Val : 4
Val : 5
8
  • 1
    and i noticed a difference in my program what would that be? Commented Jun 22, 2020 at 9:52
  • 1
    Oh, I see, they will indeed be different, 'cuz you used different set of values. Commented Jun 22, 2020 at 9:52
  • When you initialize the array, at no point in time there is garbage anywhere (for multi-thread programs, ...). int a[5] = {3, 2}; /*no garbage, even for a[4]*/ versus int a[5]; /*garbage*/ a[0]; a[0] = 3; /*garbage*/ a[1];. For a toy snippet, there really is not much of a difference, though when possible, it's a good programming technique to initialize. Commented Jun 22, 2020 at 9:54
  • @pmg but i did int t[5] in both Commented Jun 22, 2020 at 9:57
  • 3
    @OussemaNehdi Your programs have undefined behavior. Your array t has 5 elements. The valid indices are 0 through 4, inclusive. But what happens when i is 4 in your loop? You are comparing t[4] with t[5], even though t[5] lies beyond the end of the array. Even worse, you're changing t[5]. It's a severe bug in your code. It's not surprising that you get different results. The value past the end of t may be different in the two versions, and if you change it, anything can happen. Fix the bugs. Then the two versions should behave the same. Commented Jun 22, 2020 at 10:16

2 Answers 2

2
for(i=0;i<5;i++)
    if(t[i] > t[i+1])
        aux=t[i];
        t[i]=t[i+1];
        t[i+1]=aux;

First: indentation does not reflect code structure. The for and if only affect 1 other statement

Your program is equivalent to

for (i = 0; i < 5; i++) {
    if (t[i] > t[i+1]) {
        aux = t[i];
    }
}
t[i] = t[i + 1];
t[i + 1] = aux;

Second: you are trying to access t[5] which does not exist, causing Undefined Behaviour.

Sign up to request clarification or add additional context in comments.

Comments

-1

The first is called initialization and is performed at compile time (or the compiler generates the instructions to initialize the array).

The second is called assignment and is always performed at run-time.

Note: initializing a static array is performed completely at compile time. Initializing an automatic array (on the stack) is performed every time the function is entered, and the compiler has generated instructions for this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.