C strings

Member
Posts: 281
Joined: 2009.04
Post: #1
I can't find any info atall on C strings. I've figured that C doesn't do strings outofthebox, so how do I make them? I've looked at my favourite C guide, but I don't get it. Can someone help me? HuhCool

~ Bring a Pen ~
Quote this message in a reply
Member
Posts: 63
Joined: 2005.12
Post: #2
mikey Wrote:I can't find any info atall on C strings. I've figured that C doesn't do strings outofthebox, so how do I make them? I've looked at my favourite C guide, but I don't get it. Can someone help me?
My first bit of advice would be: if you're really intent on using a low-level language, use C++ rather than C (unless this option is precluded for some reason). Note that I'm assuming you're actually talking about C here, and not Objective-C.

Neither C nor C++ offers language-level support for strings, but each provides various utilities for working with strings as part of its standard library. The C++ standard library makes it fairly easy and safe to work with and manipulate strings. In C, however, a fair amount of manual work is required.

In C, strings are usually represented using raw arrays of characters. The C standard library includes a variety of functions for working with null-terminated strings - Google 'strlen', 'strcpy', etc., and you should find some good references. For formatted input and output, check out scanf() and printf().
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #3
_jyk_ Wrote:My first bit of advice would be: if you're really intent on using a low-level language, use C++ rather than C (unless this option is precluded for some reason).

Now why would you advise something like that? No need to unnecessarily complicate things.

This book is an invaluable resource for learning C, and will tell you everything you need to know about the language, including how strings work in it. It should be your first stop for questions like this.
Quote this message in a reply
Member
Posts: 63
Joined: 2005.12
Post: #4
Quote:Now why would you advise something like that? No need to unnecessarily complicate things.
I see favoring C++ over C as a simplification, not a complication.

In general, if you write code to perform some string-related operation in C and then in C++, the C++ version will be both shorter (fewer lines of code) and safer. How exactly does that complicate things? It seems like a simplification to me.

These sorts of language-related issues are highly subjective though. I stand by my earlier statement, but it should be viewed as no more than what it is - a piece of advice, which the OP can take or leave :)
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #5
_jyk_ Wrote:I see favoring C++ over C as a simplification, not a complication.

C++ is the most complicated programming language, so it's definitely not a good recommendation for beginners.
Quote this message in a reply
Member
Posts: 63
Joined: 2005.12
Post: #6
Quote:C++ is the most complicated programming language, so it's definitely not a good recommendation for beginners.
Right, but it's no worse a recommendation than C, IMO.

C++ is a more complicated language than C, but the added language-level complexity makes possible a variety of techniques and tools that actually make it less complicated to work with than C in many cases.

I actually wouldn't recommend either language as a starting point for a beginner, but if it's between C and C++, I'd recommend C++. That's just my own view on the matter though. Again, it's highly subjective.
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #7
_jyk_ Wrote:C++ is a more complicated language than C, but the added language-level complexity makes possible a variety of techniques and tools that actually make it less complicated to work with than C in many cases.

C is much easier for beginners than C++.
Quote this message in a reply
Member
Posts: 63
Joined: 2005.12
Post: #8
Quote:C is much easier for beginners than C++.
That's a valid opinion, but you're stating it as if it were fact, which it's not.

A Google search for (e.g.) 'which is easier to learn, c or c++' will return many articles, FAQ entries, and forum threads on the subject. A quick perusal should make it pretty clear that there's no firm consensus on the issue and that there are valid arguments on both sides.
Quote this message in a reply
Member
Posts: 446
Joined: 2002.09
Post: #9
mikey Wrote:I can't find any info atall on C strings.

http://en.wikipedia.org/wiki/String.h
http://en.wikipedia.org/wiki/Sprintf
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #10
_jyk_ Wrote:That's a valid opinion...

It is not an opinion; it is a fact that C is easier for beginners to learn than C++.
Quote this message in a reply
Member
Posts: 63
Joined: 2005.12
Post: #11
Quote:It is not an opinion; it is a fact that C is easier for beginners to learn than C++.

Here are a few hits returned by Google for phrases such as 'which is easier, c or c++':

http://ma.rtij.nl/acllc-c++.FAQ.html#q2.4
http://faq.cooldictionary.com/learn-c-language.php
http://www.research.att.com/~bs/learn.html

The first is an FAQ entry that seems to offer a fairly balanced and concise summary of the issue. The third is obviously a bit biased (Stroustrup is the author), but the section titled I don't know C or C++, should I learn C first? nevertheless provides some good arguments for why C++ might be easier for a novice programmer.

There are other things to take into consideration as well, such as what the individual's goals are. For someone who wants to get up and running quickly but would like to use a relatively low-level language, C++ will almost certainly offer a smoother and faster start than will C. Many basic tasks that require a lot of tricky boilerplate code in C can be accomplished much more concisely and safely using C++. If you want to explain to a beginner how to combine two strings in C, you must discuss issues such as null-termination, buffer overruns, pointers, and (perhaps) dynamic memory management. In C++, the solution is far more straightforward and intuitive: just add them together! (Using the '+' symbol, no less.)

Now, you might argue that it's better to learn the low-level details before learning about higher-level tools that abstract those details away, but what we're talking about here is which approach is easier for a beginner; 'better' is another issue (and is also highly context-dependent).

My own preference is towards C++, as I've already stated, but I can see both sides of the issue. I certainly respect your opinion, but I still don't understand why you would say it's a 'fact' that C is easier for beginners than C++.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #12
I do understand your point that C++'s increased complexity can allow for potentially easier implementation of some things, but what this means is that the programmer will be writing more code that they don't actually understand. Details of what's actually happening are abstracted away by higher-level interfaces, and there's a temptation to just use them and move on without actually learning how things work underneath, thereby writing far worse code as a result.

Presumably, the reason Jake is insisting that C being easier to learn is a fact is that to really know C++, you need to know basically everything there is to know about C and a whole lot more. What I'm hearing from your perspective is that it's easier to get some code up and running for the first time in C++, because you don't have to learn as much up front. Both valid points.
Quote this message in a reply
Member
Posts: 749
Joined: 2003.01
Post: #13
Nice summary Alex.
Personally I also find C++ easier to use, you don't have to know "everything" about C++ to benefit of classes and the stuff in the standard library and STL. Sure you can do the same things in C but it's not easy for a beginner.

©h€ck øut µy stuƒƒ åt ragdollsoft.com
New game in development Rubber Ninjas - Mac Games Downloads
Quote this message in a reply
Member
Posts: 241
Joined: 2008.07
Post: #14
The thing that may be confusing you is that C strings are simply an array of primitive type char that is terminated by the null character, '\n'. There are C libraries that assist in manipulation of these strings but if you understand it, you don't need any of that and can write your own string manipulation functions.

String, NSString, or other string classes are simply wrappers around the C string written in C++ to provide users with a way to manipulate strings. You could write a C++ or Objective-C class to pretty easily do the same thing as std::string or NSString respectively.

The first place you should start is understanding arrays. Once you understand them, understand how pointers and arrays are essentially the same thing. Then learn about dynamic memory and finally you can take a look at the stdio.h or string.h libraries.

I hope that's helpful. If someone would have presented it to me like that when I was learning, it would have been very helpful but the fact still remains that you will need a lot of low level programming understanding to deal with C Strings.

It's pretty important to understand the difference between C and C++. C++ is probably a better way to start out but if you're trying to understand arrays, pointers, and dynamic memory that's all C.
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #15
ThemsAllTook Wrote:Presumably, the reason Jake is insisting that C being easier to learn is a fact is that to really know C++, you need to know basically everything there is to know about C and a whole lot more.

Well, I wouldn't say you need to know *everything* about C to really know C++, but you'll need to know a lot. I understand the viewpoint that C++ allows you to do powerful things with little effort up front, but that doesn't make it easier for beginners to learn... Nor does it help you learn to write readable code, which is a major problem with C++. Don't start with C++ unless you have plenty of experience with at least one other language and have a clue about what you're doing. If you're a beginner, C is easier to learn and that's that.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  generate MD5 strings with Objective-C? fernandovalente 3 4,249 Jan 27, 2010 09:29 AM
Last Post: Hog
  Where are strings? Tekkan 2 2,702 Jun 17, 2007 02:44 AM
Last Post: Fenris
  Converting integer/numeric values to Strings vnvrymdreglage 5 3,572 Oct 23, 2006 07:18 PM
Last Post: vnvrymdreglage
  Converting strings to functions Joseph Duchesne 10 5,411 Feb 23, 2005 11:42 PM
Last Post: Skorche