Q:

how to read random number of int in array

i want to read space separated integer into an array and when i press enter it should stop                      reading at any point of time, how to implement loop for this program please help me to solve this.
i have tried below code but it does't work.and also how to read back again.
#include<stdio.h>
int main()
{
    int arr[30];
    int j=0;
    while(1)
    {
        int d;
        scanf("%d",&d);
            arr[j++]=d;
        if(d=='\n')break;
    }
   return 0;
}

thanks in advance

A:

Your problem is that scanf will automatically skip over all whitespace (spaces, tabs, newlines) as it looks for the next item. You can make it distinguish between newlines and other whitespace by specifically asking to read in a newline:
int main() {
    int arr[30]; // results array
    int cnt = 0; // number of results
    while (1) {
        // in the scanf format string below
        // %1[\n] asks for a 1-character string containing a newline
        char tmp[2]; // buffer for the newline
        int res = scanf("%d%1[\n]", &arr[cnt], tmp);
        if (res == 0) {
            // did not even get the integer
            // handle input error here
            break;
        }
        if (res == 1) {
            // got the integer, but no newline
            // go on to read the next integer
            ++cnt;
        }
        if (res == 2) {
            // got both the integer and newline
            // all done, drop out
            ++cnt;
            break;
        }
    }
    printf("got %d integers\n", cnt);
    return 0;
}

The problem with this approach is that it only recognizes a newline following an integer and will silently skip a line that contains only whitespace (and start reading integers from the next line). If that is not acceptable, then I think the easiest solution is to read the whole line into a buffer and parse the integers from that buffer:
int main() {
    int arr[30]; // results array
    int cnt = 0; // number of results
    char buf[1000]; // buffer for the whole line
    if (fgets(buf, sizeof(buf), stdin) == NULL) {
        // handle input error here
    } else {
        int pos = 0; // current position in buffer
        // in the scanf format string below
        // %n asks for number of characters used by sscanf
        int num;
        while (sscanf(buf + pos, "%d%n", &arr[cnt], &num) == 1) {
            pos += num; // advance position in buffer
            cnt += 1; // advance position in results
        }
        // check here that all of the buffer has been used
        // that is, that there was nothing else but integers on the line
    }
    printf("got %d integers\n", cnt);
    return 0;
}

Also note that both of the above solutions will overwrite the results array when there are more than 30 integers on the line. The second solution will also leave some of the input line unread if it is longer than what fits into the buffer. Depending on where your input is coming from, both of these may be problems that need to be fixed before the code is actually used.

