Reputation: 19
nt main() {
int hh,mm,ss;
char time[2];
printf("enter the time");
scanf("%d %d %d %s",&hh,&mm,&ss,&time);
printf("%d",ss);
printf("hh:mm:ss time:%d:%d:%d %s",hh,mm,ss,time);
//for(i=0;i<3;i++)
//printf("%c",time[i]);
return 0;
}
What is wrong with this program that it's every time gets printed with 0 if the input is any non zero integer
Upvotes: 0
Views: 36
Reputation: 172
First of all give more information when posting a question, rather than just why does this not work...
Now about the code.
For starters gcc wouldn't even let me compile this, because you dont use the & sign when reading an array of characters. And never allocate a small number of memory to the array of characters, especially if you don't know the size of it.
Correct way:
char time[100];
scanf(%s, time);
When I removed the &
sign and compiled it worked as it should, however I have no idea what you wanted with the char time[2];
so removed it and here is a perfectly working (more elegant) code, however do realize if you are scanning an integer do not supply the program a string!
#include <stdio.h>
int main() {
int hh,mm,ss;
printf("Enter the time: ");
scanf("%d %d %d", &hh, &mm, &ss);
printf("hh:mm:ss time: %d:%d:%d\n", hh, mm, ss);
return 0;
}
Upvotes: 1