Comp Sci C language "too many arguments for format" error

Click For Summary
The user is encountering a "too many arguments for format" error when trying to read multiple integers in one `scanf` call using the format string `"%d"`. The correct approach requires matching the number of format specifiers in `scanf` with the number of variables being read, such as using `scanf("%d%d%d%d%d", &a, &b, &c, &d, &e)`. The discussion highlights the need for the format string to correspond to each variable to avoid errors. Additionally, the user seeks a more efficient way to input multiple numbers without repetitive prompts. Properly formatting the `scanf` statement will resolve the issue and allow for all numbers to be read in a single input.
iquicck
Messages
3
Reaction score
0
Homework Statement
C language,Linked List
Relevant Equations
Linked List
I have a problem there,I want to Users decide a,b,c,d,e numbers.
if I write
C:
printf("enter numbers:");

scanf("%d",&a);

printf("enter numbers:");

scanf("%d",&b);...
if ı do like that ,it has not any problem but its not effective,Program always asking number a,b,c etc.
My wish is program directly get all number in one time.
I tried this code;
C:
printf("enter numbers:");

scanf("%d",&a,&b,&c,&d,&e);
But Program gave " too many arguments for format" error and giving wrong solution. How can ı fix this code?

Orjinal code:
C:
#include <stdio.h>

#include <stdlib.h>
// A linked list node

struct Node

{

int data;

struct Node *next;

};void push(struct Node** head_ref, int new_data)

{
    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));    new_node->data = new_data;    new_node->next = (*head_ref);    (*head_ref) = new_node;

}
void insertAfter(struct Node* prev_node, int new_data)

{
    if (prev_node == NULL)

    {

    printf("the given previous node cannot be NULL");

    return;

    }    struct Node* new_node =(struct Node*) malloc(sizeof(struct Node));    new_node->data = new_data;    new_node->next = prev_node->next;    prev_node->next = new_node;

}void append(struct Node** head_ref, int new_data)

{
    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
    struct Node *last = *head_ref;    new_node->data = new_data;    new_node->next = NULL;    if (*head_ref == NULL)

    {

    *head_ref = new_node;

    return;

    }    while (last->next != NULL)

        last = last->next;
    last->next = new_node;

    return;

}void printList(struct Node *node)

{

while (node != NULL)

{

    printf(" %d ", node->data);

    node = node->next;

}

}int main()

{
int a,b,c,d,e;struct Node* head = NULL;
printf("enter numbers:");

scanf("%d",&a,&b,&c,&d,&e); // The Problem is Here!

append(&head,a);

push(&head, b);push(&head, c);
;

append(&head, d);insertAfter(head->next, e);
printf("\n Created Linked List: ");

printList(head);
return 0;

}
 
Last edited:
Physics news on Phys.org
The format specifier has to have one specification for each argument, e.g.,
C:
scanf ("%d%d", &a, &b);
 
  • Like
Likes iquicck

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 12 ·
Replies
12
Views
9K