How can I read N uncertain numbers separated by spaces on the same line by scanf in C

Updated on workplace 2024-05-20
10 answers
  1. Anonymous users2024-02-11

    void f(int n,int **p)if (n<=0)

    return;

    int *a=(int*)malloc(n*sizeof(int));

    for(int i=0;i

    #include

    void f(int n,int **p)if (n<=0)

    return;

    int *a=(int*)malloc(n*sizeof(int));

    for(int i=0;iscanf("%d",&a[i]);

    p=a;int main()

    int n =4;

    int *parr=0;

    f(n,&parr);

    for(int i=0;iprintf("%d",parr[i]);

    system("pause");

    return 0;

    Note: In the main function, n is a variable, e.g. if you write scanf("%d",&n);Then substitute it into the function f(n,&parr); It can also be passed, mainly because it makes this method feasible by using the method of dynamic allocation of memory.

  2. Anonymous users2024-02-10

    Upstairs has made it clear that if you don't need to call the form of a function, you can just use a for loop as follows;

    #include

    #define n 5

    main()

    int a[n];

    int i;

    for(i=0;iscanf("%d",&a[i]);

    for(i=0;iprintf("%d",a[i]);

  3. Anonymous users2024-02-09

    To give you a few examples, you may understand this function, since these two characters are generally used to control the input, so only the case of whitespace carriage return is discussed.

    1.If the input data is of a non-char type:

    int a, b;

    scanf("%d%d",&a,&b);

    In this case, the input method can be 1 space 2

    Or 1 space space. 2

    Or 1 enter 2

    Or 1 car return. 2

    At this point, it can be concluded that scanf receives not only ignore spaces but also carriage returns.

    int a, b;

    scanf("%d,%d",&a,&b);

    In this case, the input method can be 1 space 2

    Or 1 space space. 2

    Or 1 enter 2

    Or 1 car return. 2 (just make sure that 1 is followed by a comma, and any strange input is correct).

    At this point, it can be concluded that scanf reception ignores not only spaces but also carriage returns, but in the case of commas, it must follow a certain format, otherwise an error will occur.

    2.Let's look at the case where the input data is of type char:

    char a, b;

    scanf("%c%c",&a,&b);or scanf("%c,%c",&a,&b);

    At this point, the input can only be formatted and no characters are ignored.

    That's all there is to it, maybe you think about it more, I haven't mentioned it, if you have any questions, you can put it forward and discuss it together.

  4. Anonymous users2024-02-08

    Copy-paste a whole bunch of crap, scanf("%c", &c), spaces are accepted, and spaces are ignored at other times.

  5. Anonymous users2024-02-07

    When the scanf() function receives the input data, it ends the input of a data in the following situations: (instead of ending the scanf function, the scanf function only ends when there is data in each data field and presses enter).

    When encountering a space, "Enter", "Skip Grid" key.

    End of the width.

    In case of illegal import.

  6. Anonymous users2024-02-06

    The input method of scanf depends entirely on the writing method of **.

    Let's talk about the input principle of scanf first:

    1. The concept of scanf input stream, when executed to scanf function, the input stream will be checked, and if it is empty, the user will be asked to input. If it is not empty, it is read from the input stream based on the formatting parameters of the scanf.

    2. %d is used to input an integer type, which will skip the beginning of the blank character (the space tab enter is counted as a blank character), scan until the first number starts, and then start to scan backwards, stop when it encounters a blank character or non-number, and then assign the previous number to the corresponding variable. If the first one is not a number, the scanf returns a failure.

    In the case of %3d %2d, the maximum width of the character will be scanned. And if the width is not scanned, it will stop if it touches blank characters or non-numbers.

    3. %c is used to enter a character, and will not skip the first blank character. Start with the first character of the input stream.

    If there is a width modification such as %3c, the width of the character will be scanned, but only the first character will be assigned to the corresponding variable.

    4. %s is used to input a string, which will skip the beginning of the whitespace character (the space tab enter is counted as a whitespace character), and then start from the first non-whitespace character, and then start to scan backwards, stop when it touches the whitespace character, and then assign the previous string to the corresponding variable. %s can't accept incoming spaces because it stops when it hits whitespace.

    5. %f is used to input a single-precision floating-point number type, that is, float type, and the others are the same as %d

    6. %lf is used to input a double-precision floating-point number type, that is, a double type, and the others are the same as %d

    For your question above.

    scanf(%3d%2d%3c,&a&b&ch)

    Enter 123 spaces and 46 spaces abc

    The output is a=123, b=46, c=space.

  7. Anonymous users2024-02-05

    The format control %3c should be a pointer to an array of characters, and the address of a character variable, and the result is unpredictable - if I don't care about the consequences of modifying the memory area adjacent to ch, I don't say it.

    Hereinafter**. #include

    int main()

    Enter 123 46 abc

    Output a=123 b=46 c=' ab'

    It can be seen that the teacher is obviously wrong.

  8. Anonymous users2024-02-04

    When there is a width modification, the data does not need to be divided, otherwise it will be divided.

  9. Anonymous users2024-02-03

    Width modification is a limitation on the width of the output data.,It has nothing to do with the input data.,The input data must be divided.。

  10. Anonymous users2024-02-02

    %3d represents an integer that occupies a position of 3 spaces, and is less than a space to the left.

Related questions
9 answers2024-05-20

I think it works, I did it once when I first started working, and I don't remember it very well, but you can try it with the seek function, locate it first, read it later, and I remember when I was doing this, I read the book The C Programmer, which is about 234 pages.

3 answers2024-05-20

It is read out as bytes and is converted to a number.

#include >>>More

20 answers2024-05-20

The drop-down box contains multiple items, and the text that can read one of the items in the drop-down box (n represents the index). >>>More

11 answers2024-05-20

It's textbox, right? textbook?

double somedouble; >>>More

16 answers2024-05-20

Write in the mouseover event and mouseout event, and clear the word in the text box when there is focus.