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!

Recommended Answers

All 5 Replies

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.

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++.

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. :)

>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.

Thanks! That cleared it up wonderfully!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.