About strings in c language
-
do you know how to assign strings in c program?please answer me.
"assign strings in C" A Question for you. Does C language support Object Oriented Concepts?
-
do you know how to assign strings in c program?please answer me.
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?
-
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?
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.
-
"assign strings in C" A Question for you. Does C language support Object Oriented Concepts?
-
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.
-
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.
-
do you know how to assign strings in c program?please answer me.
-
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.
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
-
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.
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.
-
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
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.
-
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.
-
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.
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.
-
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.
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
-
do you know how to assign strings in c program?please answer me.
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
-
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