Some common C language library function implementation

Source: Internet
Author: User
Tags assert

Often in the written examination and interview, the company will have some C language library function for the interviewer to do, these functions look very simple, in fact, still have to consider a lot. Here are some simple implementations of several commonly used functions.

1. String copy function

String assignment function
char *strcpy (char *strdestination, const char *strsource)
{
	//
	assert (strdestination!= Null && strsource!= null);
	int i = 0;
	while (*strsource!= ' ")
	{
		strdestination[i++] = *strsource++;
	}
	Strdestination[i] = ' the ';
	return strdestination;
}


2. Memory copy function

void *memcpy (void *dest, const void *SRC, size_t count)
{
	assert (dest!= null && src!= null);
	Char *tmp = (char*) dest;
	const char *s = (const char*) src;

	while (count--)
	{
		*tmp = *s;
	}
	return dest;
}


3. Memory Setup function

void *memset1 (void *dest, int c, size_t count)
{
	assert (dest!= NULL);
	char* tmp = (char*) dest;
	int i = 0;
	while (count-)
	{
		tmp[i++] = c;
	}
	return dest;
}


4. Converts a string to an integer

int atoi (const char *string)
{
	char *p = (char*) string;
	char c;
	int   i = 0;
	while (c = *p++)
	{
		if (c>= ' 0 ' && c<= ' 9 ')
		{
			i = i*10 + (c ' 0 ');
		}
		else
			return-1;                     Invalid string
	} return
	i;
}


To achieve so much first.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.