-4

I am wondering why Vscode is throwing an error at the line:

char filename[] = scanf("%s", filename);

The error says:

Error

rioV8
  • 24,506
  • 3
  • 32
  • 49
  • 3
    `scanf` returns the number of characters read, that is an `int` not a `char*` – rioV8 Mar 24 '23 at 18:16
  • 3
    You seem to have skipped some very important early parts of your beginners learning material. – Some programmer dude Mar 24 '23 at 18:18
  • 2
    @rioV8 `scanf` returns the number of *conversions* successfully performed. – Oka Mar 24 '23 at 18:28
  • @rioV8 Why are you telling *me* this? You have misrepresented what [`scanf`](https://en.cppreference.com/w/c/io/fscanf) returns, I am simply correcting you. – Oka Mar 24 '23 at 18:36
  • @Oka It was more a remark to the OP: Why is the OP asking us what he can find himself in the C reference – rioV8 Mar 24 '23 at 18:39
  • 1
    *"I am wondering why Vscode is throwing an error at the line:"* - Because that line is not even remotely correct. You cannot put gibberish in, and expect it to work. That code is not based on any valid C syntax. – abelenky Mar 24 '23 at 18:41
  • @Hemalurgical_Potato For starters: *First*, you need to declare your array `char filename[50];`, *then* you can try to read in to it using `scanf("%49s", filename);`. – Steve Summit Mar 24 '23 at 18:41
  • 1
    @abelenky You are too harsh. It's almost perfectly valid C syntax. It's wildly far from what we (that is, we who already know C) know is the way to do it, but it's kinda hard to explain what's *actually* wrong with it! – Steve Summit Mar 24 '23 at 18:42
  • @Hemalurgical_Potato Arrays and strings are special in C. If you were dealing with, say, simple numbers, you could write `float sqrt2 = sqrt(2.0);` on one line and it would work just fine. But you can't initialize an array (that is, something like `char filename[]`) using a function call, because functions never return arrays. – Steve Summit Mar 24 '23 at 18:45
  • Since this is your first time calling `scanf`, you might want to read [these guidelines](https://stackoverflow.com/questions/72178518#72178652) for using `scanf` safely. (It is a *very* strange and troublesome function.) – Steve Summit Mar 24 '23 at 18:47
  • @abelenky "You cannot put gibberish in, and expect it to work." Please remember that there are newbies too, they might not know about the functions and stuff in C. Being rude isn't a solution. – Ar Rakin Mar 25 '23 at 17:20

1 Answers1

2

For starters to use the function scanf you need to include header <stdio.h> and the compiler reminds you about this

#include <stdio.h>

The return type of the function scanf is int. The function is declared like

int scanf(const char * restrict format, ...);

and the function returns

3 The scanf function returns the value of the macro EOF if an input failure occurs before the first conversion (if any) has completed. Otherwise, the scanf function returns the number of input items assigned, which can be fewer than provided for, or even zero, in the event of an early matching failure.

So this declaration

char filename[] = scanf("%s", filename);

does not make sense.

You need to declare a character array with a fixed number of elements and after that to call the function scanf as for example

#include <stdio.h>

//...

char filename[20];
scanf("%19s", filename);
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335