您的位置:首页 > 编程语言 > C语言/C++

C/C++读入写出空格和\0的区别

2016-04-02 16:06 387 查看
// Struct_1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
struct stu {
char *name;
int score;

}stus[3]={{"Tom",2},{"Dave",3},{"Tony",4}};

void avg(struct stu *p){
int i=0;
for(i=0;i<3;i++,p++){
cout<<"姓名:"<<p->name<<"分数:"<<p->score<<endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
struct stu *q =stus;
avg(q);

char str[]={"abcd"};
cout<<strlen(str)<<"    "<<sizeof(str)<<endl;

char str1[30]="My name \0  is ";
char str2[30];
//gets(str2);  //获取键盘输入
//strcat(str1,str2);
puts(str1);  ///键盘读入的\0会输出,程序赋值遇到\0结束
printf("%s",str1);///键盘读入的\0会输出,程序赋值遇到\0结束
cout<<str1<<endl;///键盘读入的\0会输出,程序赋值遇到\0结束

cout<<"输入字符串"<<endl;
char  s[30];
scanf("%s",s);////键盘输入遇到空格停止读入
cout<<s<<endl;
cin>>s;//键盘输入遇到空格停止读入
cout<<s<<endl;
   char s2[30];
   gets(s2);////键盘输入可以读入空格
   puts(s2);///
char str3[30];
char str4[30]="c language";
strcpy(str3,str4);
puts(str3);

char str5[10]="asdfg";
char *str6="tyuu";
cout<<str5<<"    "<<str6<<endl;
printf("%s,,,,%s\n",str5,str6);

cout<<strcmp(str5,str6);

system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  scanf 0 空格