i newbie c character arrays. , know read character arrays need use %s format specifier using scanf or gets.
i reading time 2 character array's in c h[2] , m[2] h represents hours , m represents minutes.
char h[2],m[2]; scanf("%s:%s",h,m); printf("%s:%s",h,m);
but when give 11:30
input prints time 11:30::30
output. can me reason?
thank you.
you forgetting few things:
your character arrays need null terminated. create
h
,m
size of 3 instead of 2, allowing null character,'\0'
, placed after string.scanf
you.you can limit size of input string
scanf
.scanf("%2s", h)
place string of 2 charactersstdin
h
.you exclude
:
character first string:scanf("%[^:]:%s", h, m)
putting of together, get:
char h[3], m[3]; // create 2 character arrays of 3 characters. if (scanf("%2[^:]:%2s", h, m) == 2) { // read time given , check 2 items read (as suggested chux) printf("%s:%s", h, m); // print time given. }
Comments
Post a Comment