-1
#include <stdio.h>

int main(void) {
    int n,i;
    int str[n];

    scanf("%d\n",&n);
    for(i=0;i<=n;i++)
    {
        scanf("%d",&str[i]);
        printf("%d th %d\n",i,n);
    }
    return 0;
}

Input:

10

8 9 2 1 4 10 7 6 8 7

Output:

0 th 10

1 th 10

2 th 10

3 th 10

4 th 10

5 th 10

6 th 10

7 th 6

Why is 6 in the output?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 1
    C is an imperative programming language, not declarative. It means roughly that your program is executed line by line. You cannot declare an array that will update its size automatically when `n` changes, you can only declare an array with a size that is in `n` **at the point of declaration**. – StoryTeller - Unslander Monica Feb 22 '17 at 09:11
  • Question is (i) well-written, (ii) contains short compilable code, (iii) documents actual output, (iv) documents expected output. And two established users have submitted good answers, which adds more credibility to the question. So why the downvote? – Bathsheba Feb 22 '17 at 09:12
  • 1
    @Bathsheba maybe because it is a question that shows that he didn't get a "basic concept" of C and therefore this is a bad question and he is a bad person ;) – Kami Kaze Feb 22 '17 at 09:16
  • 1
    @Bathsheba - I honestly think the downvotes are due to the horrible formatting in the first version of the post. I fixed it, and I hope people will re-evaluate their votes now, but you know... Vote and run is popular on SO. – StoryTeller - Unslander Monica Feb 22 '17 at 09:19

3 Answers3

4

This is really strange code with undefined behavior. What do you expect this:

int n;  // No value!
int str[n];

To do? You get an array whose length is unknown since n has no value at the point of the str declaration.

If you expected the compiler to "time-travel" back to the str[n] line magically when n is given a value by scanf(), then ... that's not how Co works, and you should really read up on the language a bit more. And compile with all warnings you can get from your environment.

As an extra detail, even if it were fixed so that n had a value, the for loop overruns the array and gives you undefined behavior again.

For an array of size m, the loop header should read

for (size_t i = 0; i < m; ++i)

Since indexing is 0-based, you cannot index at m, that's outside the array.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • Interestingly you can engineer this "time travel" property using actors in C++. Nice answer by the way. – Bathsheba Feb 22 '17 at 09:19
2

In your code

int n,i;
int str[n];

you're using n while it has indeterminate value, unitialized. It invokes undefined behavior.

To elaborate, n being an automatic local variable, unless initialized explicitly, contains indeterminate value.

Solution: You need to define int str[n]; after you has taken the value from user (and sanitized).

After that, there's once more issue, your loop runs off-by-one for the array. C uses 0-based array indexing, so, for an array of size n, the valid indexes will be 0 to n-1. You loop construct should be

for(i=0; i<n; i++)
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

The value for the size of the string str[] is not yet defined. You need to have initialized this value before declaring a string/array.

If you're using gcc to compile, try including the -Wall flag in your compile line to catch errors like this.

moosefoot
  • 133
  • 3
  • 11