0

firstly i am a noob in c, and i have seen this issue various times in c, and it happens most of the time

here is the code

#include <stdio.h>

int main ()
{
    int n,a=5, A[n], i;

    printf("value of n is\n");
    scanf("%d\n", &n);

    for(i=0; i<n; i++)
    {
        printf("value of A[%d] is %d\n", i, a++);
    }

    return 0;
}

so when it comes to output, something strange happens, the program asks for two input values

here see this

/tmp/bi8gJ4fc18.o
value of n is
10
7
value of A[0] is 5
value of A[1] is 6
value of A[2] is 7
value of A[3] is 8
value of A[4] is 9
value of A[5] is 10
value of A[6] is 11
value of A[7] is 12
value of A[8] is 13
value of A[9] is 14

so why does scanf require two inputs or when using scanf two inputs are to be provided, and it only considers the 1st input, what is happening here

also one more question, here in the stack-over-flow why I have to press enter two times in the body so that the next line actually goes to the next line in the output here

also, one more plz comment on this and have mercy on me, what is the proper way to search a question in sttack-over-flow, I mean i am never able to find the answer to my questions but when i ask a question others easily find some years old answer to it

Bodo
  • 9,287
  • 1
  • 13
  • 29
Nikhil
  • 63
  • 6
  • 2
    Does this answer your question? [Using "\n" in scanf() in C](https://stackoverflow.com/questions/15443483/using-n-in-scanf-in-c) (I have seen a similar question recently, and I searched for `scanf "\n" to find the duplicate.) – Bodo Nov 19 '20 at 16:54
  • 2
    You have potentially worse problems that the one with `scanf`. What is the value of `n` when you define the array `A`? Think about the order in which you do things! – Some programmer dude Nov 19 '20 at 16:58
  • Please note you never actually store anything in the array A. You just count up from 5 n amount of times. – jcnoe Nov 19 '20 at 16:59
  • Also, the "value of A[i] is" not `a++` but `A[i]` (although uninitialised). – Weather Vane Nov 19 '20 at 17:00
  • thanks, guys for pointing my wrong thinking – Nikhil Nov 19 '20 at 17:14

0 Answers0