This blog is under construction

Sunday, 29 April 2012

memcmp example in C

Header file:
    string.h

Synopsis:
     int memcmp(void *dest, const void *src, size_t num);

Description:
     It compares first num bytes of dest with src.  If return value is 0, then both src and dest are same.  If return value is greater than 0, then dest is greater than src.  If return value is less than 0, then dest is less than src.


memcmp function C example:


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

  struct student {
        int rollno;
        int age;
  };

  int main() {
        struct student s1, s2;
        int ret;

        printf("Enter student1 rollno and age:\n");
        scanf("%d%d", &s1.rollno, &s1.age);

        printf("Enter student2 rollno and age:\n");
        scanf("%d%d", &s2.rollno, &s2.age);

        ret = memcmp(&s1, &s2, sizeof (struct student));
        if (ret == 0)
                printf("Both are equal\n");
        else
                printf("Not equal\n");
        return 0;
  }



  Output:
  Enter student1 rollno and age:
  01 22
  Enter student2 rollno and age:
  02 22
  Not equal


No comments:

Post a Comment