arrays - Reading 3 strings from keyboard in C but only get two strings when I print them? -


the problem have following: want read keyboard 3 strings(s1,s2,s3).what best way without problem , want print 3 strings.i give 3 strings in print section s2 , s3,but s1 blank!

here's code guys!:

#include <stdio.h> #include <stdlib.h> #include <string.h> #define m 4  int main(){  char buffer[255]; char s1[m],s2[m],s3[m+1];  printf("give first string: "); scanf("%s",buffer);  while(strlen(buffer) > 4){     printf("lenght of string must <= 4\n");     printf("give first string again : ");     scanf("%s",buffer); } strncpy(s1,buffer,5);  printf("give second string: "); scanf("%s",buffer);   while(strlen(buffer) > 4){     printf("lenght of string must <= 4\n");     printf("give second string again : ");     scanf("%s",buffer); }  strncpy(s2,buffer,5);   printf("give third string: "); scanf("%s",buffer);   while(strlen(buffer) > 5){     printf("lenght of string must <= 5\n");     printf("give third string again : ");     scanf("%s",buffer); } strncpy(s3,buffer,5);   printf("first string : %s\n",s1); printf("second string : %s\n",s2); printf("third string : %s\n",s3);  return 1; } 

strncpy() doesn't null terminate strings , need take care of else code lead undefined behavior.

also

m=4  

and copying 5 character lead undefined behavior.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -