1.字符和字符串的输入输出
#include <stdio.h>
#include <Windows.h>
void main()
{
int i=0,j=0;
char a[4],s[10]="hello",*sp;//hello长度是6,程序运行时在末尾自动加上\0,
sp=s;//指针sp指向数组s的首地址
sp="hello";//使得指针变量sp指向字符串hello在计算机内存中存在的位置
printf("enter elements of a:");
for(i=0;i<4;i++)
scanf("%c",&a[i]);
for(i=0;i<4;i++)
{
printf("%c ",a[i]);
}
printf("\n");
printf("the elements of sp is:");
printf("%s\n",sp);
system("pause");
}
程序输出截图:
2.gets()和puts(),字符串的输入和输出
#include <stdio.h>
#include <Windows.h>
void main()
{
char str[6],*sp;
sp=str;
printf("enter:\n");
gets(sp);//括号里面需为地址或者指针
puts(sp);
system("pause");
}
程序输出截图: