Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. About strings in c language

About strings in c language

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
17 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Ajay kumar1247

    do you know how to assign strings in c program?please answer me.

    S Offline
    S Offline
    Sivaraman Dhamodharan
    wrote on last edited by
    #3

    "assign strings in C" A Question for you. Does C language support Object Oriented Concepts?

    Programming Article

    D 1 Reply Last reply
    0
    • A Ajay kumar1247

      do you know how to assign strings in c program?please answer me.

      O Offline
      O Offline
      Orjan Westin
      wrote on last edited by
      #4

      const char* string = "string";

      If you want a string that is not constant, that would be assigned differently, and the answer would depend on whether you mean "assign memory space" or "assign the characters forming the string" when you say "assign", as these are two separate activities in C. What do you want to know? What have you tried? What books and/or articles have you read, and what is it you do not understand? Who was that masked stranger?

      D 1 Reply Last reply
      0
      • O Orjan Westin

        const char* string = "string";

        If you want a string that is not constant, that would be assigned differently, and the answer would depend on whether you mean "assign memory space" or "assign the characters forming the string" when you say "assign", as these are two separate activities in C. What do you want to know? What have you tried? What books and/or articles have you read, and what is it you do not understand? Who was that masked stranger?

        D Offline
        D Offline
        dusty_dex
        wrote on last edited by
        #5

        Here is wide character string version for international users.

        const wchar_t* string = "string";

        Unicode/MBCS gets very messy developing on Windows, dependent on whether UNICODE is defined at compile time. see this article for a comprehensive explanation. What-are-TCHAR-WCHAR-LPSTR-LPWSTR-LPCTSTR-etc? :)

        "It's true that hard work never killed anyone. But I figure, why take the chance." - Ronald Reagan That's what machines are for. Got a problem? Sleep on it.

        L 2 Replies Last reply
        0
        • S Sivaraman Dhamodharan

          "assign strings in C" A Question for you. Does C language support Object Oriented Concepts?

          Programming Article

          D Offline
          D Offline
          dusty_dex
          wrote on last edited by
          #6

          Objective-C does. Standard C, no.

          "It's true that hard work never killed anyone. But I figure, why take the chance." - Ronald Reagan That's what machines are for. Got a problem? Sleep on it.

          M 1 Reply Last reply
          0
          • D dusty_dex

            Here is wide character string version for international users.

            const wchar_t* string = "string";

            Unicode/MBCS gets very messy developing on Windows, dependent on whether UNICODE is defined at compile time. see this article for a comprehensive explanation. What-are-TCHAR-WCHAR-LPSTR-LPWSTR-LPCTSTR-etc? :)

            "It's true that hard work never killed anyone. But I figure, why take the chance." - Ronald Reagan That's what machines are for. Got a problem? Sleep on it.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #7

            I thnk you forgot the L prefix.

            Use the best guess

            1 Reply Last reply
            0
            • D dusty_dex

              Here is wide character string version for international users.

              const wchar_t* string = "string";

              Unicode/MBCS gets very messy developing on Windows, dependent on whether UNICODE is defined at compile time. see this article for a comprehensive explanation. What-are-TCHAR-WCHAR-LPSTR-LPWSTR-LPCTSTR-etc? :)

              "It's true that hard work never killed anyone. But I figure, why take the chance." - Ronald Reagan That's what machines are for. Got a problem? Sleep on it.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #8

              That isnt going to work. As Richrd says it needs to be const wchar_t* string = L"string"; or at the vey least use the T() macro and use tchars.

              ============================== Nothing to say.

              D 1 Reply Last reply
              0
              • A Ajay kumar1247

                do you know how to assign strings in c program?please answer me.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #9

                strcpy? Ever looked at that? Even beter strncpy, is that what you mean? For example;

                char mystr[100];

                memset(mystr, 0, 100); // not necessary but I like it tidy

                strcpy(mystr, "ask a better question");

                ============================== Nothing to say.

                H 1 Reply Last reply
                0
                • L Lost User

                  strcpy? Ever looked at that? Even beter strncpy, is that what you mean? For example;

                  char mystr[100];

                  memset(mystr, 0, 100); // not necessary but I like it tidy

                  strcpy(mystr, "ask a better question");

                  ============================== Nothing to say.

                  H Offline
                  H Offline
                  H Brydon
                  wrote on last edited by
                  #10

                  Erudite_Eric wrote:

                  char mystr[100];   memset(mystr, 0, 100); // not necessary but I like it tidy strcpy(mystr, "ask a better question");

                  I have always held that less code is better code. When doing this kind of thing I prefer the syntax:

                  char mystr[100] = {0};

                  strcpy(mystr, "ask a better question");

                  which I think is clearer and lets the compiler do all the work. [Also I would use strcpy_s() and probably several other stylistic issues...]

                  -- Harvey

                  L 1 Reply Last reply
                  0
                  • L Lost User

                    That isnt going to work. As Richrd says it needs to be const wchar_t* string = L"string"; or at the vey least use the T() macro and use tchars.

                    ============================== Nothing to say.

                    D Offline
                    D Offline
                    dusty_dex
                    wrote on last edited by
                    #11

                    Yes, I realise that now. :doh: I've been avoiding wide-char & unicode for as long as possible because it does my frickin' head in. Only now am I coming to regret not dealing with it sooner. :sigh:

                    "It's true that hard work never killed anyone. But I figure, why take the chance." - Ronald Reagan That's what machines are for. Got a problem? Sleep on it.

                    L 1 Reply Last reply
                    0
                    • H H Brydon

                      Erudite_Eric wrote:

                      char mystr[100];   memset(mystr, 0, 100); // not necessary but I like it tidy strcpy(mystr, "ask a better question");

                      I have always held that less code is better code. When doing this kind of thing I prefer the syntax:

                      char mystr[100] = {0};

                      strcpy(mystr, "ask a better question");

                      which I think is clearer and lets the compiler do all the work. [Also I would use strcpy_s() and probably several other stylistic issues...]

                      -- Harvey

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #12

                      Yeah, I actually allocate all arrays on the heap because buffer overruns are tracked better and dont trash the stack making it easier to debug so I always have the memset. Of course dont forget the compiler is just doing the memset for you, it is invisible, but still takes place so isnt any different when the code actually runs.

                      ============================== Nothing to say.

                      1 Reply Last reply
                      0
                      • D dusty_dex

                        Yes, I realise that now. :doh: I've been avoiding wide-char & unicode for as long as possible because it does my frickin' head in. Only now am I coming to regret not dealing with it sooner. :sigh:

                        "It's true that hard work never killed anyone. But I figure, why take the chance." - Ronald Reagan That's what machines are for. Got a problem? Sleep on it.

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #13

                        Avoiding unicode? Why, its great! Really, all the languages, all the localisation taken care of, propper strings. No messing around! Go for it! :)

                        ============================== Nothing to say.

                        D 1 Reply Last reply
                        0
                        • L Lost User

                          Avoiding unicode? Why, its great! Really, all the languages, all the localisation taken care of, propper strings. No messing around! Go for it! :)

                          ============================== Nothing to say.

                          D Offline
                          D Offline
                          dusty_dex
                          wrote on last edited by
                          #14

                          The problem isn't unicode, the problem is (was) patchy support with certain windows APIs having to switch between different encodings to get API-X to behave then switch back again. X|

                          "It's true that hard work never killed anyone. But I figure, why take the chance." - Ronald Reagan That's what machines are for. Got a problem? Sleep on it.

                          1 Reply Last reply
                          0
                          • D dusty_dex

                            Objective-C does. Standard C, no.

                            "It's true that hard work never killed anyone. But I figure, why take the chance." - Ronald Reagan That's what machines are for. Got a problem? Sleep on it.

                            M Offline
                            M Offline
                            Marco Bertschi
                            wrote on last edited by
                            #15

                            So does C++.

                            cheers Marco Bertschi


                            Software Developer Twitter | Facebook | Articles


                            You have absolutely no idea how glad I am that I have no idea at all. - OriginalGriff

                            D 1 Reply Last reply
                            0
                            • A Ajay kumar1247

                              do you know how to assign strings in c program?please answer me.

                              M Offline
                              M Offline
                              Marco Bertschi
                              wrote on last edited by
                              #16

                              Have a look at

                              char[]

                              Char[] are used to store ASCII string in C.

                              cheers Marco Bertschi


                              Software Developer Twitter | Facebook | Articles


                              You have absolutely no idea how glad I am that I have no idea at all. - OriginalGriff

                              1 Reply Last reply
                              0
                              • M Marco Bertschi

                                So does C++.

                                cheers Marco Bertschi


                                Software Developer Twitter | Facebook | Articles


                                You have absolutely no idea how glad I am that I have no idea at all. - OriginalGriff

                                D Offline
                                D Offline
                                dusty_dex
                                wrote on last edited by
                                #17

                                No sign of ++ in the OP heading. That's why I didn't mention it.

                                "It's true that hard work never killed anyone. But I figure, why take the chance." - Ronald Reagan That's what machines are for. Got a problem? Sleep on it.

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups