Click here to Skip to main content
16,017,954 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
How do i get rid of an error
expected identifier or ‘(’ before ‘{’ token


This is my code

#include <stdio.h>
int main(void);
{
int x, y, z;
int r = scanf"%d-%d-%d", &x, &y, &z;
printf"%d\n", r;
return 0;
}


I assume its something to do with my second line, but when I try to fix it it just breaks the code even more. Please bear with me, I'm very new to this and basic coding like this eludes me.

What I have tried:

I've tried removing the semi-colon, but it just breaks the code even harder.
Posted
Updated 1-Oct-21 4:14am

Adding a semicolon to a function definition turns it into a forward declaration - which can't have a body.
Remove the semicolon at the end of the "main" line.

scanf is a function: you need to call it by appending open and close round brackets to the name:
C
int x = foo();
Or if you need to supply parameters, they go inside the brackets:
C
int x = foo(666, "a parameter");


scanf requires at least two parameters (a format string, and one or more pointers to locations to store the information) so they go inside the brackets:
C
int r = scanf("%d-%d-%d", &x, &y, &z);


You will need to do the same thing with printf as well.
 
Share this answer
 
v2
Comments
CPallini 1-Oct-21 10:08am    
5.
#include <stdio.h>
int main(void)     // <== don't want semi colon here
{
int x, y, z;
int r = scanf("%d-%d-%d", &x, &y, &z);   // function calls require parens
printf("%d\n", r);
return 0;
}
 
Share this answer
 
Comments
CPallini 1-Oct-21 10:08am    
5.
jeron1 1-Oct-21 10:20am    
Thanks. :)
Member 15373566 1-Oct-21 10:12am    
Thanks for the help! If you don't mind me tacking on another question, what does scanf do in this function? No matter my input the output is always 1
jeron1 1-Oct-21 10:34am    
Have a look here.
https://www.cplusplus.com/reference/cstdio/scanf/
As everybody knows, if you want to learn the C programming language, then you must greet the world.

C "Hello, World!" Program[^].
 
Share this answer
 
Comments
jeron1 1-Oct-21 10:31am    
Indeed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900