Character " (double quote) in C

Solved
mano9 Posted messages 32 Status Member -  
fiddy Posted messages 441 Registration date   Status Contributor Last intervention   -
Bonjour,
I am working on a project where I need to assign the double quote ( " ) to a string variable (it's the only way to achieve what I want). However, for the past two days, I've tried everything in vain. Actually, here it is:

char machaine[5];
machaine = "SC CREATE " ";

I always get errors, so I don't know how to use " as a character. Please help me, my project is due tomorrow and this is the only thing I need to unblock to finish it.

Best regards
Configuration: Windows XP Internet Explorer 7.0

4 answers

  1. fiddy Posted messages 441 Registration date   Status Contributor Last intervention   1 847
     
    Hello,
    Three errors in your code.
    1/ The size of your string is too small to contain "SC CREATE". (At least 10 characters minimum, you can add more to be safe).
    2/ machaine = "SC CREATE " "; It is forbidden to do that. You must use the strncpy function to perform the string copy. For example:
     strncpy(machaine,"SC CREATE",sizeof machaine); machaine[sizeof machaine - 1]='\0'; //to place the final \0. 

    3/ No quotes in the string. Either that or you need to escape it with a backslash. For example: "SC CREATE\" "

    Best regards
    --

    Google is your friend
    1
    1. KéKeCest ??
       
      Hi fiddy,

      I’m just making this little correction on the strncpy() function:

      char *strncpy(char *dest, const char *src, size_t n)


      > Copies at most n characters from src to dest padding with null characters if dest size > src size.

      Otherwise, in his case, he can simply use the old strcpy() function.

      Have a nice day.
      0
    2. fiddy Posted messages 441 Registration date   Status Contributor Last intervention   1 847 > KéKeCest ??
       
      I don't understand why you are correcting me? What didn't you like about my code?

      Otherwise, in his case, he can simply use the old strcpy() function.
      Yes, but be careful of buffer overflow.
      --

      Google is your friend
      0
    3. loupius > KéKeCest ??
       
      Hi KéKeCest??

      I am allowing myself this little correction on the strncpy() function:

      char *strncpy(char *dest, const char *src, size_t n)
      > Copies at most n characters from src to dest, padding with null characters if dest size > src size.


      No, the correct answer is:
      Copies at most n characters from src to dest, padding with null characters if n > src size.

      Have a good day.
      0
    4. KéKeCest ?? > loupius
       
      Hi,

      I would say yes "in principle" - we should compare our sources as justifications.

      Regards.
      0
    5. KéKeCest ?? > fiddy Posted messages 441 Registration date   Status Contributor Last intervention  
       
      Re,

      strncpy(machine,"SC CREATE",sizeof (machine));

      No no like that, it looks OK (:>).

      Seriously.
      0
  2. mano9 Posted messages 32 Status Member
     
    Thank you for your response.

    1 For the size it's okay, I didn't make that mistake

    2 In my "sc create ", I actually need to include the double quote (the second to last) as a character
    in the string.

    3 I do not want to put " ", but rather "

    Please, I await your response.
    0
    1. fiddy Posted messages 441 Registration date   Status Contributor Last intervention   1 847
       
      1 For the size, it's fine, I didn't make that mistake.
      Yet the example you gave contains that mistake.

      2 In my "sc create", I actually need to include the double quote (the penultimate one) as a character in the string.
      Yes, I understood that well, and that's why I'm telling you that it must be escaped with a backslash otherwise it won't work. Reread my example from post 1 to see how to do it.

      3 I don't want to put """, but rather "
      In C, the string is enclosed by two quotes. But these quotes are not part of the string themselves. They are just there to help the compiler understand what it's dealing with.
      --

      Google is your friend
      0
  3. mano9 Posted messages 32 Status Member
     
    Thank you very much. I'm going to try it right away.
    0
  4. tartALAfraise
     
    Hello

    I just wanted to clarify that your double quote is actually "double quote"
    quote is "quotation marks" in the language of Shakespeare (hence the citation = quotation)
    the double appeared in the teaching rooms (English-speaking) of computer science where it was necessary to explain the difference (in programming) between apostrophes and quotation marks by emphasizing the number of apostrophes to draw (single or double).
    I remind you that the audience of these courses (in all languages) is generally not well-versed in language or its formalization (hence the use of simplified or even rudimentary vocabulary to explain something, as well as their difficulty in writing a description of a function or a problem in a language understandable by their peers)
    0