To "mark the end of the string somehow" was not about the NUL char, but to be possible to scan a string with spaces. For "Javaid 10 37.2", the "%s %d %f" will get the 3 parts and assign them to the char buffer, integer and float... If you do "Javaid Malik 10 37.2" with "%s %d %f", "Javaid" will be copied to n array and sscanf() will try to covert "Malik" do int...
If you try, with the last string, using "%[^"] %d %f", the entire string will be copied to the array, and the other 2 argumentos will be undefined. So... If you use ""Javaid Malik" 10 37.2" and ""%[^"]" %d %f" you will get what you want...
Running your code as it is you will get an segmentation fault:
Code:
$ cc -o test test.c
test.c:3:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
main()
^~~~
test.c: In function ‘main’:
test.c:13:22: warning: format ‘%d’ expects argument of type ‘int *’, but argument 4 has type ‘int’ [-Wformat=]
sscanf ( str, "%s %d %f", n, a, c );
~^
test.c:13:25: warning: format ‘%f’ expects argument of type ‘float *’, but argument 5 has type ‘double’ [-Wformat=]
sscanf ( str, "%s %d %f", n, a, c );
~^
$ ./test
Javaid 37 3.17
Javaid 37 3.2
Segmentation fault (core dumped)
And the warnings are self explanatory... Changing the string you will get:
Code:
$ sed -i 's/Javaid/Javaid Malik' test.c
$ cc -o test test.c
test.c:3:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
main()
^~~~
test.c: In function ‘main’:
test.c:13:22: warning: format ‘%d’ expects argument of type ‘int *’, but argument 4 has type ‘int’ [-Wformat=]
sscanf ( str, "%s %d %f", n, a, c );
~^
test.c:13:25: warning: format ‘%f’ expects argument of type ‘float *’, but argument 5 has type ‘double’ [-Wformat=]
sscanf ( str, "%s %d %f", n, a, c );
~^
$ ./test
Javaid Malik 37 3.17
Javaid Malik 37 3.2
Javaid 32758 15610336269040904490120578005030928384.00
Ok... This seems to be an unexpected result, but remember sscanf() is failing when trying to convert "Malik" to int...
What you see here is the garbage from local variables b and c. Let's see on gdb, shall we?
Code:
$ cc -g -o test test.c
$ gdb test
Reading symbols from test...done.
(gdb) l
1 #include<stdio.h>
2
3 main()
4 {
5 char str[50];
6 char n[40], name[] = "Javaid Malik";
7 int a, age = 37;
8 float c, cgpa = 3.17;
9
10 printf ( "%s %d %0.2f\n", name, age, cgpa );
(gdb) b 10
Breakpoint 1 at 0x837: file test.c, line 10.
(gdb) r
Starting program: /home/frederico/MyStuff/tmp/test
Breakpoint 1, main () at test.c:10
10 printf ( "%s %d %0.2f\n", name, age, cgpa );
(gdb) p a
$1 = 32767
(gdb) p c
$2 = -1.03806422e+34
(gdb) b 14
Breakpoint 2 at 0x5555555548bd: file test.c, line 14.
(gdb) c
Continuing.
Javaid Malik 37 3.17
Javaid Malik 37 3.2
Breakpoint 2, main () at test.c:14
14 printf ( "%s %d %0.2f\n", n, a, c );
(gdb) p a
$3 = 32767
(gdb) p c
$4 = -1.03806422e+34
(gdb) c
Continuing.
Javaid 32767 -10380642212464256323223514653917184.00
[Inferior 1 (process 1273) exited normally]
Now you known from where that 32758 comes from (the values are a little bit different here because of debugging code)... The other value is garbage as well...
Did YOU get my point?