sprintf
#include
int main ()
{
char buffer [50];
int n, a=5, b=3;
n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);
printf ("[%s] is a string %d chars long\n",buffer,n);
return 0;
}
strchr
#include
#include
int main ()
{
char str[] = "This is a sample string";
char * pch;
printf ("Looking for the 's' character in \"%s\"...\n",str);
pch=strchr(str,'s');
while (pch!=NULL)
{
printf ("found at %d\n",pch-str+1);
pch=strchr(pch+1,'s');
}
return 0;
}
#define STRINGIFY_(M) #M
#define STRINGIFY(M) STRINGIFY_(M)
#include
int main()
{
//printf("%c\n",STRINGIFY(a));
printf(STRINGIFY(X));
}
string num
#include
#include
#define DIGIT(n) ('0'+(n))
#define DIGIMOD(n,f) DIGIT((n)/(f) %10)
#define RJDIGIT(n,f) ((n) >= (f) ? DIGIMOD(n,f):' ')
#define MINUSOR(n,alt) (n>0?(alt):(n=-n,'-'))
typedef unsigned char uint8_t;
char conv[8] = { 0 };
char* i8tostr3(const uint8_t xx)
{
conv[4] = RJDIGIT(xx, 100);
conv[5] = RJDIGIT(xx, 10);
conv[6] = DIGIMOD(xx, 1);
return &conv[4];
}
int main()
{
//i8tostr3((uint8_t)643);
printf("%s",i8tostr3((uint8_t)343));
//printf("USA");
}
strstr
/* strstr example */
#include
#include
int main ()
{
char str[] ="This is a simple string";
char * pch;
pch = strstr (str,"simple");
printf("%s\n",pch);
printf("%p\n",str);
printf("%p\n",pch);
strncpy (pch,"sample",4);
puts (str);
return 0;
}
strtol
#include /* printf */
#include /* strtol */
int main ()
{
char szNumbers[] = "2001 60c0c0 -1101110100110100100000 0x6fffff";
char * pEnd;
long int li1, li2, li3, li4;
li1 = strtol (szNumbers,&pEnd,10);
printf("%s\n", pEnd);
li2 = strtol (pEnd,&pEnd,10);
printf("%s\n", pEnd);
li3 = strtol (pEnd,&pEnd,2);
printf("%s\n", pEnd);
li4 = strtol (pEnd,NULL,0);
printf("%s\n", pEnd);
printf ("The decimal equivalents are: %ld, %ld, %ld and %ld.\n", li1, li2, li3, li4);
return 0;
}
#include
#include
int main()
{
char str[30] = "2030300 This is test";
char *ptr;
long ret;
ret = strtol(str, &ptr, 10);
printf("数字(无符号长整数)是 %ld\n", ret);
printf("字符串部分是%s", ptr);
return(0);
}