C language "too many arguments for format" error

Click For Summary
SUMMARY

The discussion centers around a common issue in C programming related to the "too many arguments for format" error when using the scanf function. The user attempts to read multiple integers in a single scanf call with the format string "%d" for five variables (a, b, c, d, e), which leads to the error. The correct approach is to provide a format specifier for each variable, such as using "scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);" to avoid this error. Additionally, the user is implementing a linked list structure to store these integers.

PREREQUISITES
  • C programming language fundamentals
  • Understanding of scanf and printf functions
  • Basic knowledge of linked lists in C
  • Memory management in C using malloc
NEXT STEPS
  • Learn proper usage of scanf with multiple variables in C
  • Explore advanced linked list operations in C
  • Study error handling techniques in C programming
  • Investigate memory management best practices in C
USEFUL FOR

C programmers, students learning data structures, and developers looking to improve their input handling and memory management skills in C.

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   Reactions: iquicck

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
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
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 12 ·
Replies
12
Views
10K