strings in c

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2005
Posts: 8
Reputation: zauber is an unknown quantity at this point 
Solved Threads: 1
zauber zauber is offline Offline
Newbie Poster

strings in c

 
0
  #1
Aug 9th, 2005
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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 55
Reputation: Sauce is an unknown quantity at this point 
Solved Threads: 0
Sauce Sauce is offline Offline
Junior Poster in Training

Re: strings in c

 
0
  #2
Aug 9th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 62
Reputation: freemind is an unknown quantity at this point 
Solved Threads: 1
freemind's Avatar
freemind freemind is offline Offline
Junior Poster in Training

Re: strings in c

 
0
  #3
Aug 9th, 2005
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++.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 8
Reputation: zauber is an unknown quantity at this point 
Solved Threads: 1
zauber zauber is offline Offline
Newbie Poster

Re: strings in c

 
0
  #4
Aug 9th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,813
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 747
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: strings in c

 
0
  #5
Aug 9th, 2005
>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.
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 8
Reputation: zauber is an unknown quantity at this point 
Solved Threads: 1
zauber zauber is offline Offline
Newbie Poster

Re: strings in c

 
0
  #6
Aug 11th, 2005
Thanks! That cleared it up wonderfully!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC