October 7th, 2012, 11:36 AM
String in C
Hello i want to scan and print some Sentences, is it possible? then how? In delphi i used string but C has just float integer and char
If its possible please use some primitive thins, im programming in C just for 2 weeks, not a big professional hehe...
October 7th, 2012, 01:12 PM
scan sentences with fgets().
print them with, for example, printf().
October 7th, 2012, 01:16 PM
Thanks a lot for help, finally it works. tomorrow i have exam in school, so must work hard hehe
Btw a few second after tried your idea i tried to use scanf for reading string too, but it didnt worked, because i tried like this:
hehe and just from luck i tried it without & better said from mistake, and it worked, crazy that for decimal and hexadecimal i need & and for string not :-)
October 7th, 2012, 02:05 PM
decimal and hexadecimal are "plain" variables; strings are arrays.
Originally Posted by Ricsie
When you use an array in most contexts, it get automatically converted to a pointer to its first element -- which is exactly what scanf() needs.
Also using scanf("%s") does not allow you to enter sentences with spaces.
scanf("%d", &i); /* &i points to i */
scanf("%s", a); /* a "decays" to &a so it points to the string */
You would do better to use scanf("%99[^\n]", a) which allows the user to enter spaces and stops before overflowing the array.
Summing up: fgets() is the better function.
fgets(), by desing, prevents buffer overflow
fgets(), by design, allows spaces
October 7th, 2012, 08:00 PM
crazy that for decimal and hexadecimal i need & and for string not :-)
When the computer reads data from a device it needs a location to store the data. In scanf you provide an address that is the beginning of the memory where the data will be put.
char a_string; /* a_string is an address. */
int an_integer; /* an_integer is the integer data */
scanf("%d",&an_integer); /* the location of that integer */
scanff("%s",a_string); /* the location of those characters */
[/code] are essential for python code and Makefiles!
October 8th, 2012, 02:01 AM
You might like to read the comp.lang.c FAQ, particularly section 6.
Originally Posted by b49P23TIvg
Arrays are not pointers and pointers are not arrays.
Comments on this post
October 8th, 2012, 01:55 PM
Thanks a lot, you all helped me a lot, and the page is great you gave me, im reading through the whole FAQ and its really interesting... :-)