© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu

C and C++ Interview Questions

19. What is the difference between a string copy (strcpy) and a memory copy (memcpy)? When should each be used?

Ans:
The strcpy() function is designed to work exclusively with strings. It copies each byte of the source string to the destination string and stops when the terminating null character () has been moved. On the other hand, the memcpy() function is designed to work with any type of data. Because not all data ends with a null character, you must provide the memcpy() function with the number of bytes you want to copy from the source to the destination.

Ex - 1: What will be printed as the result of the operation below?
#define swap(a,b) a=a+b;b=a-b;a=a-b;
void main()
{
    int x=5, y=10;
    swap (x,y);
    printf(“%d %dn”,x,y);
    swap2(x,y);
    printf(“%d %dn”,x,y);
}
int swap2(int a, int b)
{
    int temp;
    temp=a;
    b=a;
    a=temp;
    return 0;
}
Ans: 10, 5
10, 5

Ex - 2: What will be printed as the result of the operation below:
main()
{
    char *p1;
    char *p2;
    p1=(char *)malloc(25);
    p2=(char *)malloc(25);
    strcpy(p1,”Cisco”);
    strcpy(p2,“systems”);
    strcat(p1,p2);
    printf(“%s”,p1);
}
Ans: Ciscosystems


SLogix Student Projects
bottom