943,940 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2296
  • C++ RSS
Aug 9th, 2005
0

strings in c

Expand Post »
Hello,

Im rather new to C, but I was struck with one curiosity as I delve into it.

From what I understand, there is not supposed to be any support for string-datatypes in the language itself; you need to import libraries to get this support (where the strings are actually implemented as char[]).

However the syntax allows you to specify a string as "blah blah".
What I mean is: the syntax gives special meaning to quotationmarks - interpreting data therein as strings.

Doesn't it seem odd that the syntax would allow you to write something that explicitly requires libraries to be included in order to be actually understood?

I know Java has this same principle, but there the String object is included in java.lang, which is always automatically imported. So there's no way that you could not include the String object.

Comments on this? Have I misunderstood something? Is it or is it not odd? I would like to hear some comments. Thanks!
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
zauber is offline Offline
8 posts
since Jul 2005
Aug 9th, 2005
0

Re: strings in c

I'm going to be waiting for a die-hard C person to tell me i'm completely wrong on this...but anyway, here is my educated guess on the issue. The C programming langauge and pretty much every one i have used, allows the use of the double quotes. This is not to say its to create a "string" object, but rather to allow the compiler to seperate text from a variable. Without the double quote or some kind of marker parsing the source file would be a lot more difficult or even an impossible impossible task.

Java implements a "string" class, but in the underlying code it is implemented the same as it would be done in C. Java's "string" class also has built in array index checking (to prevent over-runs) which is one thing they did to make Java more secure then C. You cannot even resize a string in Java, if you change it you have to create a new one.

And last, keep in mind i used the worlds "String class" and "String object" during this post. Dont forget that C is NOT an object oriented programming language. In otherwords, objects do NOT exist (unless you are niffy and use structs and pointers to funtions to get a pseudo OO language, but a completely differnt issue).

p.s. On a side note, I believe the struts and pointers to func. is how the first C++ language was created, it just transformed origional C++ code into C code via a preprocessor and then compiled it as C code.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Sauce is offline Offline
55 posts
since Jul 2005
Aug 9th, 2005
0

Re: strings in c

Since strings in C are arrays of characters and an array is something quite trivial to programming languages, you don't need to import libraries to get strings to work. You can make your life easier, though, by using headers, that include various functions for string manipulation (string.h for example.) The double quotes show that you are about to use a string, instead of a single char(refferred to with a single quote 'a','b'.. and so on). This could be looked at also from the point of view - making it possible to live and program without committing a suicide when you try to use a string. Imagine instead of writing char bla[] = "I love icecream\0"; to had to write char bla[16]; bla[0] = 'I'; bla[1] = ' '; bla[2] = 'l', etc. or char a[] = {'a','b','c','\0'};. To complete the definition of a string in C you have to remember that every char array(string) has '\0' as a last member, i.e. strings in C are zero-terminated.

Direct string support and respectively a string class is supported in C++.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
freemind is offline Offline
62 posts
since Jun 2005
Aug 9th, 2005
0

Re: strings in c

makes sense. thanks!

look forward to the c-diehards to show up and throw their two cents at me though. I find the low-level aspects of techy things unhealthily interesting.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
zauber is offline Offline
8 posts
since Jul 2005
Aug 9th, 2005
0

Re: strings in c

>From what I understand, there is not supposed to be any support for
>string-datatypes in the language itself
No, it just requires you to implement strings yourself using arrays of char. There's no need for an explicit string type because arrays of char do the job admirably.

>the syntax gives special meaning to quotationmarks - interpreting data therein as strings.
No, it interprets them as arrays of const char, which is consistent with the definition of strings in C.

>Doesn't it seem odd that the syntax would allow you to write
>something that explicitly requires libraries to be included in order to be actually understood?
Yes it would, if that were the case. But seeing as how you aren't required to include the library to use arrays of char as strings, it's not the case.

>I find the low-level aspects of techy things unhealthily interesting.
Then you'll find it interesting that you don't have to represent strings as arrays of char terminated by '\0'. This is only the way that the language prefers it, but you aren't required to use it if something else (such as saving the length as the first element of the array) is more efficient for your needs.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Aug 11th, 2005
0

Re: strings in c

Thanks! That cleared it up wonderfully!
Reputation Points: 10
Solved Threads: 1
Newbie Poster
zauber is offline Offline
8 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: binary tree class
Next Thread in C++ Forum Timeline: Problem with Switch statement ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC