#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
//sprintf的作用:生成一个指令,把他映射到字符串里,对他初始化
void main1()
{
//char str[100] = "for /l %i in (1,1,5) do calc";
//char *p = "for /l %i in (1,1,5) do calc";
//str[0] = ' ';//可以,str是数组,存储的是字符串每一个字节
//*p = ' ';//报错,p是一个指针,存储字符串的地址
//printf("for /l %%i in (1,1,5) do calc");//打印一个%需要两个%
//printf("for /l %%i in (1,1,%d) do %s",5,"calc");//结果和上面的一样
//printf往屏幕输出,sprintf往字符串里输出
char str[100] = { 0 };
int num;
char op[30] = { 0 };
scanf("%d%s", &num, op);
sprintf(str,"for /l %%i in (1,1,%d) do %s", num, op);//打开num个op
system(str);
system("pause");
}
void main2()
{
char str[100] = { 0 };
char op[30] = { 0 };
scanf("%s", op);
sprintf(str, "taskkill /f /im %s", op);//关闭op进程
system(str);
system("pause");
}
void main3()
{
system("tasklist");//遍历所有进程
for (int i = 0x0; i <= 0xf; i++)
{
char str[30] = { 0 };//储存指令
sprintf(str, "color %x%x", i, 0xf - i);//打印指令
system(str);//变色
}
}
void main4()
{
char str1[100] = "note";
char str2[100] = "pad";
//system(str1 + str2);
char str3[100] = { 0 };
sprintf(str3, "%s%s", str1, str2);//字符串相加
system(str3);//结果是打开notepad
}
void main5()
{
char str[100] = "";
char ch[100] = "notepad1234";
sprintf(str, "%-10.7s", ch);//10宽度,+宽度范围内右边对齐,-左边对齐,.7从左往右挖去7个字符
printf("%s", str);//notepad
system(str);//打开notepad
getchar();
}
int mystrlen(char *str)
{
int n = 0;
for (char *p = str; *p != '\0'; p++)
{
n++;
}
return n;//字符串统计
}
void main6()
{
char str[100] = "china is good";
printf("%d", sizeof(str));//求数组缓冲区长度 100
printf("\n%d", mystrlen(str));//从开头第一个字符到\0字符 13
getchar();
}