Hey guys! I’m new to C, so i would like help in one problem. I have to separate a string and put him into diferent variables. Imagine, Sol 3 5 would result in something like:
var1=Sol
var2=3
var3=5
I tried to use the scanf, but it stoped in the first space :/ .
Thanks in advance!
Cheers!
EDIT: Isn’t my homework, i’m just practicing, but i really want to now how I can do this 🙂 . The code I have now is this:
int main () {
char var1[10],var2[10],var3[10],func;
fgets(func, 20, stdin);
fscanf(func,"%s %d %d", var1,var2,var3);
printf("%s %d %d", var1,var2,var3);
return 0;
}
After the edit and code posted
Your problem is that you are lying to the compiler. Don’t do that. It doesn’t like it 🙂
You ask the compiler to read a string and 2 integers … but then tell it to put the results in a char arrays (correct only for the first conversion)
Try declaring your variables as
Oh!
funcis declared as plain char. You probably want something else.Once you declare your variables like that, you need to change the
scanfcall an pass the address of the ints, rather than their (uninitialized) values.