输出printf与put
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
int main()
{
/* stdio.h常用函数 */
int a = 123;
int* pa = &a;
printf("%d\n", a);
printf("%u\n", a);
printf("%f\n", (float)a);
printf("%0x\n", &a); //6ff75c x大小写都可以
printf("%p\n",pa); //006FF75C 默认大写,前面补0
printf("%o\n", a); //173 只能用小写o
printf("%lu\n", a); //123 32位无符号整数(在64位环境上也兼容)
//printf("%llu\n", a); //72079223493230715 仅在x64环境可使用
float a2 = 1.23456;
printf("%e\n", a2); //1.234560e+00
printf("%g\n", a2); //1.23456 把输出的值按照%e或者%f类型中输出长度较小的方式输出
char a3[100] = "asd";
printf("%s\n", a3);
//printf("%c\n", a3); //? 无法识别
printf("%c\n", a3[0]);
printf("%p\n", a3); //00EFFB08
printf("%p\n", &a3[0]); //00EFFB08
//a3字符数组名 a3[0]字符数组中的一个成员(1个字符)
//%c 时,后面必须是一个char类型,或者能被识别为char类型(入(int)32 会被理解为以ASCII表示的char)
puts("puts用法:");
puts(a3); //自动换行
}
输出sprintf
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
int main()
{
/* stdio.h常用函数 */
char buffer[200], s[] = "computer", c = 'l';
int i = 35, j;
float fp = 1.7320534f;
// 格式化并打印各种数据到buffer
j = sprintf(buffer, " String: %s\n", s);
j += sprintf(buffer + j, " Character: %c\n", c);
j += sprintf(buffer + j, " Integer: %d\n", i);
j += sprintf(buffer + j, " Real: %f\n", fp);
printf("Output:\n");
printf("%s\n", buffer);
printf("character count = % d\n",j);
}
string.h常用函数
memcpy与memmove与strcpy与strncpy
printf("---------string.h常用函数测试---------\n");
char b1[100] = "asd";
char b2[100];
memcpy(b2, b1, 4); //目的和源存储区不可重叠 b2 = b1 ,4
printf("%s\n", b2); //asd
memmove(b2, b2+1, 3); //目的和源存储区可重叠
printf("%s\n", b2); //sd
strcpy(b2, "asd"); //b2 = b1 b2必须足够大, 只能永远char[] 或 char*
printf("%s\n", b2); //sd
char b3[100];
strncpy(b3, "asd", 2);
printf("%c%c%c\n", b3[0],b3[1],b3[2]); //as?
strcat与strncat
// d串与s串必须以\0结尾, d串要够长
// s串为const类型
char d[20] = "GoldenGlobal";
const char *s = "View";
strcat(d, s);
printf("%s", d);
// 默认在最后加\0
char d[20] = "GoldenGlobal";
const char *s = "View";
strncat(d, s, 3);
printf("%s", d);
memcmp(可对结构体使用)
const char* s1 = "123";
const char* s2 = "432";
int r;
r = memcmp(s2, s1, strlen(s1));
printf("%d\n", r); // s2 - s1 大为1 同为0 小为-1
strncmp/strcmp(只可用于字符串)
const char* s1 = "123";
const char* s2 = "432";
int r;
r = strncmp(s2, s1, strlen(s1)); //r = strcmp(s2, s1);
printf("%d\n", r); // s2 - s1 大为1 同为0 小为-1
strxfrm
const char* source = "23234abc";
char des[100];
size_t len = strxfrm(des, source, 50);
// des = source 最多copy50个,遇到\0立刻返回,返回实际copy的字节数
printf("%s\n",des);
printf("%d\n", len);
memchr在字符串中找字符
const char* s = "Hello, Programmers!";
const void *p = memchr(s, 'P', strlen(s));//因为memchr(,,);return void*p;
//s是一个指向char的指针,而任何指针都是个一个4字节的数,在这里应//该是要说明搜索整个字符串的长度,所以应该用strlen(s)
if (p)
printf("%s", (char *)p);
else
printf("Not Found!");
memchr找string中char的地址(void *)
strchr找string中char的位置(int)
const char* s = "Hello, Programmers!";
const void *p = memchr(s, 'P', strlen(s));//因为memchr(,,) return void*p;
//s是一个指向char的指针,而任何指针都是个一个4字节的数
if (p)
printf("%s", (char *)p);
else
printf("Not Found!");
char string[17];
char *ptr,
char c = 'r';
strcpy(string, "Thisisastring");
ptr = strchr(string, c);
if (ptr)
printf("The character %cis at position:%s\n", c, ptr);
else
printf("The character was not found\n");
strstr——字符串中找字符串,返回找到的起始地址
const char haystack[20] = "RUNOOB";
const char needle[10] = "NOOB";
const char* ret;
ret = strstr(haystack, needle);
printf("子字符串是: %s\n", ret);
strlen
char a[10] = "aaa";
printf("%d\n", strlen(a));
_CRT_SECURE_NO_WARNINGS
在VS中调用 strcpy、strcat 等函数时会提示_CRT_SECURE_NO_WARNINGS 警告,原因是这些函数不安全,可能会造成内存泄露等。
This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
解决方法1:
解决方法2:
在写代码的前面加上如下宏定义:
#define _CRT_SECURE_NO_WARNINGS