Thread: String in C

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    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...

    Thanks
    Rick
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    33
    scan sentences with fgets().
    print them with, for example, printf().

    Happy Coding.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0
    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:
    scanf("%s", &a);

    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 :-)
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    33
    Originally Posted by Ricsie
    crazy that for decimal and hexadecimal i need & and for string not :-)
    decimal and hexadecimal are "plain" variables; strings are arrays.
    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.

    Code:
    int i;
    char a[100];
    scanf("%d", &i); /* &i points to i */
    scanf("%s", a); /* a "decays" to &a[0] so it points to the string */
    Also using scanf("%s") does not allow you to enter sentences with spaces.
    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
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480

    sane


    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.
    Code:
    char a_string[88];   /* 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]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    33
    Originally Posted by b49P23TIvg
    Code:
    char a_string[88];   /* a_string is an address. */
    You might like to read the comp.lang.c FAQ, particularly section 6.

    Arrays are not pointers and pointers are not arrays.

    Comments on this post

    • b49P23TIvg agrees : Thank you.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0
    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... :-)

IMN logo majestic logo threadwatch logo seochat tools logo