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


2 comments:

  1. Do you need Finance? Are you looking for Finance? Are you looking for finance to enlarge your business? We help individuals and companies to obtain finance for business expanding and to setup a new business ranging any amount. Get finance at affordable interest rate of 3%, Do you need this finance for business and to clear your bills? Then send us an email now for more information contact us now via (financialserviceoffer876@gmail.com) whats-App +918929509036 Dr James Eric Finance Pvt Ltd Thanks

    ReplyDelete