C strings

Member
Posts: 281
Joined: 2009.04
Post: #16
Quote:C is much easier for beginners than C++.

I totally agree, after learning BASIC, printf() was so much easier to understand than cout >>. And C is much simpler to learn.

~ Bring a Pen ~
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #17
bmantzey Wrote:the null character, '\n'

'\n' is newline. '\0' is the null terminator.
Quote this message in a reply
Member
Posts: 63
Joined: 2005.12
Post: #18
ThemsAllTook Wrote: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.
Nice summary, and agreed on all points. IMO there's no 'one right answer' to the question of which language is better to start with; it really depends on a lot of factors, not the least of which is what your goals and objectives are.

AnotherJake Wrote:If you're a beginner, C is easier to learn and that's that.
It just seems odd to me to speak in absolutes about such things. The links I've posted don't seem to be making a dent, but here are a couple more:

http://www.programmersheaven.com/mb/Cand.../?S=B10000
http://stackoverflow.com/questions/49680...r-c/496865

The first reply in the second thread linked above is a particularly effective and concise example of how C++ can be easier for beginners.

From looking around on the internet it's clear that there are people who think C++ can be easier to learn for a beginner than C. You're basically saying that all of those people are wrong, which is fine by me, but it still seems odd to me to speak about such a complex (and subjective) issue in such black-and-white terms.

I can see I'm not making any headway though, so I'll let it rest at this point Smile
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #19
_jyk_ Wrote:The first reply in the second thread linked above is a particularly effective and concise example of how C++ can be easier for beginners.

You mean this?
Quote:
Code:
int main() {
    using namespace std;
    string name;
    cout << "Hello. What's your name? " << flush;
    cin >> name;
    cout << "Hello, " << name << endl;
}
I can't even begin to describe the difficulties of doing something even remotely similar in C.
That guy has no idea what he's talking about.

_jyk_ Wrote:From looking around on the internet it's clear that there are people who think C++ can be easier to learn for a beginner than C. You're basically saying that all of those people are wrong...
I am not "basically" saying that all of those people are wrong -- I *am* saying that all of those people are wrong! C is easier than C++ for beginners to learn, just like water is wet, the earth is round, and bears $hit in the woods.
Quote this message in a reply
Member
Posts: 227
Joined: 2008.08
Post: #20
Okay, personally, C++ is often easier in the hands of a skilled wielder. C++ is syntactically easier. C is more dependable, it will always do what you ask, if you can't tell it the right direction, it'll shoot your face off. In C, there is no operator overloading, so *string1+*string2 will always add the two first chars, while in C++ it may do anything depending on whether it was overloaded.
I am not an experienced C++ master, but i do claim a very deep understanding of C as a language. I agree with you all, C is like Unix, give every one a chainsaw, while C++ nobly tries to clean that but stumbles when it tries to hold on to legacy and be too much.

I do recommend C first, then learn C++, you have to do that anyway.

if that fails: Try Pascal or LLVM-Lua (Short laugh follows)

For C strings use the string.h library remember that at its core, in any language a string is just a "string of characters".
Code:
#include <string.h>
#include <stdio.h>
typedef char* MYString;
MYstring s;
int main(void){
  s="Hello World\n";
  printf(s);
  int length=strlen(s);
  printf("Is %i characters long\n",length);
  char buffer[1000];
  printf("Type here:");
  fgets(buffer,1000,stdin);
  if(strcmp(buffer,s)==0) printf("They are equal\n");
  else printf("They not are equal\n");
  return 0;
}//" " adds the \0 terminator automatically, {...,'\0'} is equivalent, \n is the linefeed character
Quote this message in a reply
Member
Posts: 254
Joined: 2005.10
Post: #21
One of the problems with null terminated strings is that if you forget the null character (at least in C) memory will be read beyond the end of the string until a null character is found. This can of course lead to some very strange bugs. I think it's worth mentioning that while null terminated strings are very common, they are not the only way to implement strings. Another simple way of implementing strings would be to include the length of the string before the string itself. These pascal strings use the length to read the string and will not exceed the bounds of the memory defined for the string at the cost of their size being limited by the datatype chosen for the length.
Quote this message in a reply
Member
Posts: 26
Joined: 2008.08
Post: #22
bmantzey Wrote: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'.

Hold up a 'sec, don't you mean '\0'?

Or have I been confused my whole programming life?

EDIT: whoops, ThemsAllTook already caught it; never mind me.
Quote this message in a reply
Member
Posts: 26
Joined: 2008.08
Post: #23
I find it difficult to compare the difficulty in learning either of the two languages given the overlap, great as it is. That aside, it seems that in learning C++, one has only to learn to avoid certain features of the language to write C (like classes) and remember a syntax oddity or two.

For me, going from C++ to C (as I had learned C++ first) was not a problem at all; for practice, I'd change the extension on a C++ source file and let the compiler tell me what I had to change, for the most part.
Quote this message in a reply
Member
Posts: 749
Joined: 2003.01
Post: #24
Code:
#include <string.h>
#include <stdio.h>
typedef char* MYString;
MYstring s;
int main(void){
  s="Hello World\n";
  printf(s);
  int length=strlen(s);
  printf("Is %i characters long\n",length);
  char buffer[1000];
  printf("Type here:");
  fgets(buffer,1000,stdin);
  if(strcmp(buffer,s)==0) printf("They are equal\n");
  else printf("They not are equal\n");
  return 0;
}//" " adds the \0 terminator automatically, {...,'\0'} is equivalent, \n is the linefeed character
[/quote]

What if you dont want a static buffer size? It gets fairly tedious fast.

I actually use a pretty shallow-easy going C++, it's conceptually very similar to programming in C, heck I even put all my class variables public, but at least I don't have to handle variable sized arrays myself, have easier support for strings, can write car.init() instead of initCar(car), can use operators for 3d math, can use a little inheritance if needed, and other small things that make programming overall more comfortable.

©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: 227
Joined: 2008.08
Post: #25
Or as I like to do:
Code:
#define o(self,method,...) (self).isa->##method##(self,__VA_ARGS)
struct Car;
struct CarClass_t{
  struct Car* (*init)(struct Car*);
}CarClass
typedef struct Car{
  CarClass_t* isa;
  //cars properties here
}Car;
Car* CarInit(Car* self){
  return self;
}
int main(void){
  Car car;
  car.isa=&CarClass;
  CarClass.init=&CarInit;
  o(car,init);
  return 0;
}//as far as I know, ObjC does it this way (With string based lookups though)
Rasp

I use C++ only for namespaces and methods.

Funny how this thread became a debate on C and C++
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,410 Jan 27, 2010 09:29 AM
Last Post: Hog
  Where are strings? Tekkan 2 2,784 Jun 17, 2007 02:44 AM
Last Post: Fenris
  Converting integer/numeric values to Strings vnvrymdreglage 5 3,716 Oct 23, 2006 07:18 PM
Last Post: vnvrymdreglage
  Converting strings to functions Joseph Duchesne 10 5,571 Feb 23, 2005 11:42 PM
Last Post: Skorche