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

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 668 Views - Last Post: 14 July 2013 - 09:30 AM Rate Topic: -----

#1 manolos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3526
  • View blog
  • Posts: 12,034
  • 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>

so add this to the top of your file.
Was This Post Helpful? 0
  • +
  • -

#4 manolos  Icon User is offline

  • New D.I.C Head

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

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

Posted 14 July 2013 - 06:28 AM

View Postsepp2k, 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?

View Postandrewsw, 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>

so add this to the top of your file.

hmm cause much more errors for some reason
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

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

Posted 14 July 2013 - 06:29 AM

View Postmanolos, 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

Was This Post Helpful? 0
  • +
  • -

#6 manolos  Icon User is offline

  • New D.I.C Head

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

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

Posted 14 July 2013 - 06:33 AM

View Postsepp2k, on 14 July 2013 - 06:29 AM, said:

View Postmanolos, 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?
Was This Post Helpful? 0
  • +
  • -

#7 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 804
  • View blog
  • Posts: 2,373
  • 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.
Was This Post Helpful? 0
  • +
  • -

#8 manolos  Icon User is offline

  • New D.I.C Head

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

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

Posted 14 July 2013 - 06:42 AM

View Postsnoopy11, 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. :unsure:
Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5848
  • View blog
  • Posts: 12,707
  • 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

Was This Post Helpful? 2
  • +
  • -

#10 manolos  Icon User is offline

  • New D.I.C Head

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

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

Posted 14 July 2013 - 06:52 AM

View Postbaavgai, 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 :oops:
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is online

  • member icon


Reputation: 4101
  • View blog
  • Posts: 12,699
  • 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

Was This Post Helpful? 0
  • +
  • -

#12 manolos  Icon User is offline

  • New D.I.C Head

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

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

Posted 14 July 2013 - 07:26 AM

View Postjimblumberg, 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?
Was This Post Helpful? 0
  • +
  • -

#13 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3526
  • View blog
  • Posts: 12,034
  • 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.
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is online

  • member icon


Reputation: 4101
  • View blog
  • Posts: 12,699
  • 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
Was This Post Helpful? 0
  • +
  • -

#15 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5848
  • View blog
  • Posts: 12,707
  • Joined: 16-October 07

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

Posted 14 July 2013 - 08:11 AM

You can follow the links for the functions...

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.
Was This Post Helpful? 2
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2