Novice C language learning journey(6)

in #art4 years ago (edited)

梦幻光晕蓝色背景矢量图.png
This code will shows a strange output when run it.Why?
Look at the "strcat" function and "puts" function.These function need string variable.But "getchar" function read a character only,lacking '\0' character.The char array of "a,b,c" did not initialization.So the input is not a string.The variable type is not match.The code as follows:
#include"stdio.h"
#include"stdlib.h"
int main(void){

char a[23];
char b[33];
char c[22];
a[0]=getchar();
b[0]=getchar();
c[0]=getchar();
strcat(a,b);
strcat(a,c);
puts(a);
puts(b);
puts(c);
return 0;

}
捕获.PNG
The modified as follows:
#include"stdio.h"
#include"stdlib.h"
int main(void){

char a[23]="qwe";
char b[33]="rty";
char c[22]="uio";
a[0]=getchar();
b[0]=getchar();
c[0]=getchar();
strcat(a,b);
strcat(a,c);
puts(a);
puts(b);
puts(c);
return 0;

}
捕获444.PNG

Sort:  

Welcome to steemit @sky-999.

Welcome the new steemians. Have a great day!

Congratulations @sky-999! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You distributed more than 50 upvotes. Your next target is to reach 100 upvotes.

You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Do not miss the last post from @steemitboard:

Valentine's day challenge - Give a badge to your beloved!
Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Hey sky-999! If you would like to make your personal feed contain the top trending media and talking points of the whole internet, consider following @coffeebreak and @topicstoday accounts - And welcome to Steem!

 4 years ago  Reveal Comment