1

Consider:

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

int main() {
    int n;
    char name[100];
    int number;
    printf("Enter the value of n\n");
    scanf("%d",&n);
    printf("Enter %d values\n", n);
    for(int i=0; i<n; i++)
    {
        scanf("%[^\n]s", &name);
    }
}

Whenever I am entering the value of n, it just prints (Enter n values) and exits the program. The for loop never runs. It ran successfully for the first time, but after that it just exits the program.

There were some answers that said it will not print anything. I don’t want it to print just to take input n times. It is not doing that.

My aim is to take n as input and then take strings of names (like harry, robin, etc.) n number of times as input.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Akshat kant
  • 125
  • 6
  • 2
    Note: [`gets`](https://en.cppreference.com/w/c/io/gets) "_The `gets()` function does not perform bounds checking, therefore this function is extremely vulnerable to buffer-overflow attacks. It cannot be used safely (unless the program runs in an environment which restricts what can appear on stdin). For this reason, the function has been deprecated in the third corrigendum to the C99 standard and removed altogether in the C11 standard. `fgets()` and `gets_s()` are the recommended replacements. **Never use `gets()`**._" – Ted Lyngmo Jul 26 '21 at 09:35
  • Without checking, I bet that you will find the answer here http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html otherwise read the docs (https://en.cppreference.com/w/c/io/fscanf), check the return value and output the scanned/read value (those are different things) for verfication. If even that fails the info you got that way will help you to ask according to [ask]. – Yunnosch Jul 26 '21 at 09:41
  • You are not printing anything... And in the code you commented out, you use `i>n` which should be `i – Support Ukraine Jul 26 '21 at 09:46
  • Are you trying to enter `n` strings or `n` chars? I suppose the program is not complete, what do you want to do with `name` after the input has been done? – Jabberwocky Jul 26 '21 at 09:54
  • I am trying to take n as input and then want to take strings of names (like harry,robin etc) n number of times as input – Akshat kant Jul 26 '21 at 09:57
  • 1
    The `'s'` doesn't belong at the end of `"%[^\n]s"`. The `"%[..]"` is the complete specifier. You need to ***validate*** all input, e.g. `if (scanf("%d",&n) != 1) { /* handle error */ }` – David C. Rankin Jul 26 '21 at 10:02
  • [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/q/1694036/995714) – phuclv Jul 26 '21 at 10:03
  • @phuclv there is no `gets` in his code – Jabberwocky Jul 26 '21 at 10:03
  • @Jabberwocky the code posted is shifting sand. Akshat please don't change the code to update from comments, this isn't a "live" code editing site. – Weather Vane Jul 26 '21 at 10:12

2 Answers2

2

Your code is a little incomplete. And there are a few errors here: scanf ("%[^\n]s", &name)

Do this and everything will be fine:

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

int main(void)
{
    int n;
    char name[100];
    int number;
    printf("Enter the value of n\n");
    scanf(" %d", &n);
    printf("Enter %d values\n", n);
    for(int i=0; i<n; i++)
    {
        scanf(" %99[^\n]", name);
        printf("%s\n", name);
    }
    return 0;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Генс
  • 93
  • 5
1

scanf is particularly unsuited for user input.

You probably want this:

int main() {
  int n;
  char name[100];
  int number;
  printf("Enter the value of n\n");
  scanf("%d", &n);

  printf("Enter %d values\n", n);
  for (int i = 0; i < n; i++)
  {
                                 // the space at the beginning of "%[^\n]"
                                 // gets rid of the \n which stays in the input buffer
    scanf(" %[^\n]", name);      // also there sis no 's' at the end of the "%[^\n]" specifier 
    printf("name = %s\n", name); // for testing purposes
  }
}

But this doesn't actually make much sense because the program is asking for n names, but at each run of the for loop the previous name will be overwritten with the new name.

Also be aware that scanf("%[^\n]", name); is problematic because if the user types more than 99 characters you'll get a buffer overflow.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115