# (C) Im new and I need help with this tutorial.

• (2 Pages)
• 1
• 2

## 16 Replies - 914 Views - Last Post: 14 July 2013 - 09:30 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=324799&amp;s=dfa3170db14d74009ddfade24581920f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 manolos

Reputation: 0
• Posts: 7
• Joined: 14-July 13

# (C) Im new and I need help with this tutorial.

Posted 14 July 2013 - 06:15 AM

Hello all.
I was looking for simple steps with exercises so I started with learn-c.org (sorry if im not allowed to post sites)
Im on 3rd tutorial, defining strings. This is the exercise.

Exercise
Define the string first_name with the value John using the pointer notation, and define the string last_name with the value Doe using the local array notation.

and the code
#include <stdio.h>

int main() {
/* define first_name */
/* define last_name */
char name[100];

/* testing code */
if (strncmp(first_name, "John") != 0) return 1;
if (strncmp(last_name, "Doe") != 0) return 1;

last_name[0] = 'B';
sprintf(name, "%s %s", first_name, last_name);
if (strncmp(name, "John Boe", 100) == 0) {
printf("Done!\n");
}

return 0;
}

So this is what ive done:

#include <stdio.h>

int main() {
char first_name[]="John";
char last_name[]="Doe";
char name[100];

/* testing code */
if (strncmp(first_name, "John") != 0) return 1;
if (strncmp(last_name, "Doe") != 0) return 1;

last_name[0] = 'B';
sprintf(name, "%s %s", first_name, last_name);
if (strncmp(name, "John Boe", 100) == 0) {
printf("Done!\n");
}

return 0;
}

My errors are those:
9 33 [Error] 'strncmp' was not declared in this scope
10 31 [Error] 'strncmp' was not declared in this scope
14 36 [Error] 'strncmp' was not declared in this scope

I use dev c++ 5.4.2 on win8 64. Thanks for your time.

Is This A Good Question/Topic? 0

## Replies To: (C) Im new and I need help with this tutorial.

### #2 sepp2k

• D.I.C Lover

Reputation: 2277
• Posts: 3,507
• Joined: 21-June 11

## Re: (C) Im new and I need help with this tutorial.

Posted 14 July 2013 - 06:20 AM

strncmp is declared in string.h, so you'll need to #include that.

### #3 andrewsw

• Well, I'll need some beer.

Reputation: 4978
• Posts: 18,882
• Joined: 12-December 12

## Re: (C) Im new and I need help with this tutorial.

Posted 14 July 2013 - 06:24 AM

I'm not a C person, but searching tells me that strncmp lives in

#include <string.h>

### #4 manolos

Reputation: 0
• Posts: 7
• Joined: 14-July 13

## Re: (C) Im new and I need help with this tutorial.

Posted 14 July 2013 - 06:28 AM

sepp2k, on 14 July 2013 - 06:20 AM, said:

strncmp is declared in string.h, so you'll need to #include that.

I delete stdio.h and i only use string.h?

andrewsw, on 14 July 2013 - 06:24 AM, said:

I'm not a C person, but searching tells me that strncmp lives in

#include <string.h>

hmm cause much more errors for some reason

### #5 sepp2k

• D.I.C Lover

Reputation: 2277
• Posts: 3,507
• Joined: 21-June 11

## Re: (C) Im new and I need help with this tutorial.

Posted 14 July 2013 - 06:29 AM

manolos, on 14 July 2013 - 03:28 PM, said:

I delete stdio.h

No, you need stdio.h for sprintf and printf.

If your tutorial hasn't made that clear to you yet (and, as a matter of fact, if it hasn't told you that you need string.h for strncmp), it's not a very good tutorial.

This post has been edited by sepp2k: 14 July 2013 - 06:30 AM

### #6 manolos

Reputation: 0
• Posts: 7
• Joined: 14-July 13

## Re: (C) Im new and I need help with this tutorial.

Posted 14 July 2013 - 06:33 AM

sepp2k, on 14 July 2013 - 06:29 AM, said:

manolos, on 14 July 2013 - 03:28 PM, said:

I delete stdio.h

No, you need stdio.h for sprintf and printf.

If your tutorial hasn't made that clear to you yet (and, as a matter of fact, if it hasn't told you that you need string.h for strncmp), it's not a very good tutorial.

I see.. well I think i will quit this site and i will start with Herbert's C book.
Do anyone know a good site with examples or exercises to learn C?

### #7 snoopy11

• Engineering ● Software

Reputation: 1022
• Posts: 3,094
• Joined: 20-March 10

## Re: (C) Im new and I need help with this tutorial.

Posted 14 July 2013 - 06:40 AM

strncmp takes 3 arguments you manage it in your third try at comparing strings but
not in your first two attempts

Why ?

Regards

Snoopy.

### #8 manolos

Reputation: 0
• Posts: 7
• Joined: 14-July 13

## Re: (C) Im new and I need help with this tutorial.

Posted 14 July 2013 - 06:42 AM

snoopy11, on 14 July 2013 - 06:40 AM, said:

strncmp takes 3 arguments you manage it in your third try at comparing strings but
not in your first two attempts

Why ?

Regards

Snoopy.

Sorry Snoopy, I didnt get the question.

### #9 baavgai

• Dreaming Coder

Reputation: 6379
• Posts: 13,629
• Joined: 16-October 07

## Re: (C) Im new and I need help with this tutorial.

Posted 14 July 2013 - 06:44 AM

strncmp doesn't mean what you think it means.

You also use it in two different ways:
strncmp(first_name, "John") /* two params */
strncmp(name, "John Boe", 100) /* three params */

Let's just fix your first one, assuming you want strcmp

#include <stdio.h>
#include <string.h>

int main() {
char first_name[100], last_name[100];
/* load something meaningful with first_name and last_name... */

if (strcmp(first_name, "John") != 0) {
return 1;
} else if (strcmp(last_name, "Doe") != 0) {
return 1;
} else {
char name[100];
last_name[0] = 'B';
sprintf(name, "%s %s", first_name, last_name);
if (strcmp(name, "John Boe") == 0) {
printf("Done!\n");
}
return 0;
}
}

This post has been edited by baavgai: 14 July 2013 - 06:45 AM

### #10 manolos

Reputation: 0
• Posts: 7
• Joined: 14-July 13

## Re: (C) Im new and I need help with this tutorial.

Posted 14 July 2013 - 06:52 AM

baavgai, on 14 July 2013 - 06:44 AM, said:

strncmp doesn't mean what you think it means.

You also use it in two different ways:
strncmp(first_name, "John") /* two params */
strncmp(name, "John Boe", 100) /* three params */

Let's just fix your first one, assuming you want strcmp

#include <stdio.h>
#include <string.h>

int main() {
char first_name[100], last_name[100];
/* load something meaningful with first_name and last_name... */

if (strcmp(first_name, "John") != 0) {
return 1;
} else if (strcmp(last_name, "Doe") != 0) {
return 1;
} else {
char name[100];
last_name[0] = 'B';
sprintf(name, "%s %s", first_name, last_name);
if (strcmp(name, "John Boe") == 0) {
printf("Done!\n");
}
return 0;
}
}

thank you. thats it but i didnt even get how this worked. as sepp2k said, the site is bad for learning the basics

Quote

the site is bad for learning the basics

I meant the learn-c.org site! not this one!! hehe

### #11 jimblumberg

Reputation: 4734
• Posts: 14,759
• Joined: 25-December 09

## Re: (C) Im new and I need help with this tutorial.

Posted 14 July 2013 - 07:23 AM

Quote

I see.. well I think i will quit this site and i will start with Herbert's C book.

I hope you're not talking about one of these books Herb Schildt. If so I suggest you find another author and book. These books are really known for how bad they are, rather then how good.

Jim

This post has been edited by jimblumberg: 14 July 2013 - 07:23 AM

### #12 manolos

Reputation: 0
• Posts: 7
• Joined: 14-July 13

## Re: (C) Im new and I need help with this tutorial.

Posted 14 July 2013 - 07:26 AM

jimblumberg, on 14 July 2013 - 07:23 AM, said:

Quote

I see.. well I think i will quit this site and i will start with Herbert's C book.

I hope you're not talking about one of these books Herb Schildt. If so I suggest you find another author and book. These books are really known for how bad they are, rather then how good.

Jim

Realy? Why? What you recommend?

### #13 andrewsw

• Well, I'll need some beer.

Reputation: 4978
• Posts: 18,882
• Joined: 12-December 12

## Re: (C) Im new and I need help with this tutorial.

Posted 14 July 2013 - 07:33 AM

There are tutorials here @DIC, click the Tutorials link at the top of the page.

For a fuller tutorial I'm hoping someone will recommend one. Is tutorialspoint any good? I've found it a useful site for other stuff, but haven't gone through a full tutorial of theirs.

### #14 jimblumberg

Reputation: 4734
• Posts: 14,759
• Joined: 25-December 09

## Re: (C) Im new and I need help with this tutorial.

Posted 14 July 2013 - 07:54 AM

Quote

Realy? Why?

Several of this authors books have been known to have quite a few errors and mis-stated facts. Here is one such review.

Here is a small list of several recommended C books. But note I don't recommend trying to use The C Programming Language to learn the language, pick one of the books from the beginner section.

Jim

### #15 baavgai

• Dreaming Coder

Reputation: 6379
• Posts: 13,629
• Joined: 16-October 07

## Re: (C) Im new and I need help with this tutorial.

Posted 14 July 2013 - 08:11 AM

Essentially, the compare type functions return a value equal to 0, less than 0 and greater than 0. This is called a "lexicographic" compare.

Some test code:
#include <stdio.h>
#include <string.h>

int main() {
char *s1 = "Robert", *s2 = "Rob";

printf("%s %s %d\n", s1, s2, strcmp(s1, s2));
printf("%s %s %d 4\n", s1, s2, strncmp(s1, s2, 4));
printf("%s %s %d 4\n", s1, s2, strncmp(s1, s2, 3));
return 0;
}

Results:
Robert Rob 101
Robert Rob 101 4
Robert Rob 0 4

What does this mean? The first one is a straight compare. The value is >0, so s2 is greater than s1. ( More on that value in a moment. )

The second uses the strncmp and says to check the first 4 letters: same result.

The second uses the strncmp and says to check the first 3 letters: we have a match.

To illustrate how strcmp works, you can write your own:
int my_strcmp(char *s1, char *s2) {
int pos = 0;
while(s1[pos]==s2[pos]) {
/* we still match */
if (s2=='\0') { return 0; } /* if end of string, we match, return 0 */
/* not at end, move to next pos */
pos++;
}
/* we got here.  return a comparison of the two characters we ended on */
return s1[pos] - s2[pos];
}

This will also get you 101 as a value. The strncmp adds the extra wrinkle of the checking on the size. I'll leave that as an exercise for you.

Hope this helps.