-3
#include<stdio.h>
int main()
{

 int n;

 scanf("%d",&n);

  char str[500];

 scanf("%[^\n]s",str);

  printf("%d\n",n);

  printf("%s",str);

return 0;
}

Input:

5 7 1 2 3 

Output:

5 5 ->

I want the output to be

5

7 1 2 3

Can anybody help me with my code... Please

Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • possible duplicate of [How can I read a string with spaces in it in C?](http://stackoverflow.com/questions/4025673/how-can-i-read-a-string-with-spaces-in-it-in-c) – Ankur Jan 10 '15 at 17:58

5 Answers5

0

Use fgets(3) to read the line:

 fgets(str, sizeof(str), stdin);

Later parse the line str, perhaps using sscanf(3) (possibly with %n) or strtol(3)

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

Use

fgets(str, sizeof(str), stdin);

to read till end of line.

Then in order to get the integers you can use strtok() to break the line into tokens and convert tokens to intgers using strtol()

Gopi
  • 19,784
  • 4
  • 24
  • 36
0

If you really want to use scanf() and read a number before starting getting the input, you could do this:

#include <stdio.h>

int main(void) {
  int numbers[128];
  int n;

  printf("How many numbers to read?\n");
  scanf("%d", &n);

  int i;
  for(i = 0; i < n; ++i) {
    scanf("%d", &numbers[i]);
  }

  for(i = 0; i < n; ++i)
    printf("%d\n", numbers[i]);

  return 0;
}

Output:

How many numbers to read?
5
1 2 3 4 5
1
2
3
4
5

Use fgets(). Feed the function with stdin.

Here is an example:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void) {
  char line[256];
  char* pch;
  int numbers[128];
  int N = 0; // actual numbers of numbers read

  // to read one line
  fgets(line, 256, stdin);
  line[strlen(line) - 1] = '\0';
  pch = strtok(line, " ");
  while (pch != NULL) {
    numbers[N++] = atoi(pch);
    pch = strtok(NULL, " ");
  }

  int i;
  for(i = 0; i < N; ++i)
    printf("%d\n", numbers[i]);

  return 0;
}

Output:

1 2 3 4 5
1
2
3
4
5
gsamaras
  • 71,951
  • 46
  • 188
  • 305
0

Why OP code fails: With user input like 5 Enter a space b Enter.

The first scanf() consumes "5", leaving "\na b\n" for the next scanf(). That scanf() scans nothing and saves nothing into str as the first char is a '\n'.

scanf("%d",&n);
scanf("%[^\n]s",str);

For reading a line scanf("%[^\n]s",str); has a number of problems.

  1. There is not reason for the s in "%[^\n]s"

  2. "%[^\n]" will not save anything into str if input is "\n".

  3. No protection against reading too much data.

  4. Code should check the return value from scanf().

As suggested by many, use fgets()

  char str[500];
  if (fgets(str, sizeof str, stdin) == NULL) HandleEOForIOerror();

  // to remove '\n'
  size_t len = strlen(str);
  if (len > 0 && str[len-1] == '\n') str[--len] = 0;

  printf("%s",str);

To read the number, recommend the same, use fgets()

#include<stdio.h>

int main(void) {
  int n;
  char str[500];
  if (fgets( str, sizeof str, stdin) return -1;
  if (1 == sscanf(str, "%d",&n)) {
    printf("%d\n",n);
  }
  if (fgets( str, sizeof str, stdin) return -1;
  printf("%s",str);
  return 0;
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

scanf("%d",&n); change to scanf("%d%*c", &n);
or
scanf("%[^\n]s", str); change to scanf(" %[^\n]", str);

BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70