C Program Time Conversion












1















I came upon this Time Conversion Program in HackerRank, I was surprised the way how the program is compiled in HackerRank (or could be my misunderstanding of C).



#include <stdio.h>

int main() {
int hh, mm, ss;
char tt[2];
scanf("%d:%d:%d%s", &hh, &mm, &ss, tt);
printf("%dn", hh);
if(strcmp(tt, "PM") == 0 && hh != 12) hh += 12;
if(strcmp(tt, "AM") == 0 && hh == 12) hh = 0;
printf("%02d:%02d:%02d", hh, mm, ss);
return 0;
}


The above program, when I ran in my computer, using MinGW 32-bit GCC Compiler, I was getting the value of hh as zero.



enter image description here



Fine, I thought could be compiler issue and ran the code in IDEONE, same results.



enter image description here



But when I ran this same code with HackerRank, all testcases were passed, I have no idea, how this is working here?



enter image description here



I am confused at this stage, am I doing this right?










share|improve this question




















  • 3





    You are 1-char short on char tt[2]; You forgot about the nul-terminating character. You need at minimum char tt[3] = ""; to hold AM or PM (the initialization is just good practice)

    – David C. Rankin
    Apr 12 '16 at 10:32













  • @DavidC.Rankin, how does variable hh return zero, just because of char pointer is having a shorter storage space? and how does it run on hackerrank?

    – cs1193
    Apr 12 '16 at 10:35








  • 3





    It runs poorly. You invoke undefined behavior.

    – Lundin
    Apr 12 '16 at 10:41






  • 2





    @cs1193 It depends on the layout of variables on the stack. If tt is right before hh, the final nul character will overwrite the first byte of hh, setting it to zero on a little endian architecture.

    – nwellnhof
    Apr 12 '16 at 11:01
















1















I came upon this Time Conversion Program in HackerRank, I was surprised the way how the program is compiled in HackerRank (or could be my misunderstanding of C).



#include <stdio.h>

int main() {
int hh, mm, ss;
char tt[2];
scanf("%d:%d:%d%s", &hh, &mm, &ss, tt);
printf("%dn", hh);
if(strcmp(tt, "PM") == 0 && hh != 12) hh += 12;
if(strcmp(tt, "AM") == 0 && hh == 12) hh = 0;
printf("%02d:%02d:%02d", hh, mm, ss);
return 0;
}


The above program, when I ran in my computer, using MinGW 32-bit GCC Compiler, I was getting the value of hh as zero.



enter image description here



Fine, I thought could be compiler issue and ran the code in IDEONE, same results.



enter image description here



But when I ran this same code with HackerRank, all testcases were passed, I have no idea, how this is working here?



enter image description here



I am confused at this stage, am I doing this right?










share|improve this question




















  • 3





    You are 1-char short on char tt[2]; You forgot about the nul-terminating character. You need at minimum char tt[3] = ""; to hold AM or PM (the initialization is just good practice)

    – David C. Rankin
    Apr 12 '16 at 10:32













  • @DavidC.Rankin, how does variable hh return zero, just because of char pointer is having a shorter storage space? and how does it run on hackerrank?

    – cs1193
    Apr 12 '16 at 10:35








  • 3





    It runs poorly. You invoke undefined behavior.

    – Lundin
    Apr 12 '16 at 10:41






  • 2





    @cs1193 It depends on the layout of variables on the stack. If tt is right before hh, the final nul character will overwrite the first byte of hh, setting it to zero on a little endian architecture.

    – nwellnhof
    Apr 12 '16 at 11:01














1












1








1








I came upon this Time Conversion Program in HackerRank, I was surprised the way how the program is compiled in HackerRank (or could be my misunderstanding of C).



#include <stdio.h>

int main() {
int hh, mm, ss;
char tt[2];
scanf("%d:%d:%d%s", &hh, &mm, &ss, tt);
printf("%dn", hh);
if(strcmp(tt, "PM") == 0 && hh != 12) hh += 12;
if(strcmp(tt, "AM") == 0 && hh == 12) hh = 0;
printf("%02d:%02d:%02d", hh, mm, ss);
return 0;
}


The above program, when I ran in my computer, using MinGW 32-bit GCC Compiler, I was getting the value of hh as zero.



enter image description here



Fine, I thought could be compiler issue and ran the code in IDEONE, same results.



enter image description here



But when I ran this same code with HackerRank, all testcases were passed, I have no idea, how this is working here?



enter image description here



I am confused at this stage, am I doing this right?










share|improve this question
















I came upon this Time Conversion Program in HackerRank, I was surprised the way how the program is compiled in HackerRank (or could be my misunderstanding of C).



#include <stdio.h>

int main() {
int hh, mm, ss;
char tt[2];
scanf("%d:%d:%d%s", &hh, &mm, &ss, tt);
printf("%dn", hh);
if(strcmp(tt, "PM") == 0 && hh != 12) hh += 12;
if(strcmp(tt, "AM") == 0 && hh == 12) hh = 0;
printf("%02d:%02d:%02d", hh, mm, ss);
return 0;
}


The above program, when I ran in my computer, using MinGW 32-bit GCC Compiler, I was getting the value of hh as zero.



enter image description here



Fine, I thought could be compiler issue and ran the code in IDEONE, same results.



enter image description here



But when I ran this same code with HackerRank, all testcases were passed, I have no idea, how this is working here?



enter image description here



I am confused at this stage, am I doing this right?







c time






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 11 '17 at 20:14









Martijn Pieters

724k14325452348




724k14325452348










asked Apr 12 '16 at 10:27









cs1193cs1193

84611222




84611222








  • 3





    You are 1-char short on char tt[2]; You forgot about the nul-terminating character. You need at minimum char tt[3] = ""; to hold AM or PM (the initialization is just good practice)

    – David C. Rankin
    Apr 12 '16 at 10:32













  • @DavidC.Rankin, how does variable hh return zero, just because of char pointer is having a shorter storage space? and how does it run on hackerrank?

    – cs1193
    Apr 12 '16 at 10:35








  • 3





    It runs poorly. You invoke undefined behavior.

    – Lundin
    Apr 12 '16 at 10:41






  • 2





    @cs1193 It depends on the layout of variables on the stack. If tt is right before hh, the final nul character will overwrite the first byte of hh, setting it to zero on a little endian architecture.

    – nwellnhof
    Apr 12 '16 at 11:01














  • 3





    You are 1-char short on char tt[2]; You forgot about the nul-terminating character. You need at minimum char tt[3] = ""; to hold AM or PM (the initialization is just good practice)

    – David C. Rankin
    Apr 12 '16 at 10:32













  • @DavidC.Rankin, how does variable hh return zero, just because of char pointer is having a shorter storage space? and how does it run on hackerrank?

    – cs1193
    Apr 12 '16 at 10:35








  • 3





    It runs poorly. You invoke undefined behavior.

    – Lundin
    Apr 12 '16 at 10:41






  • 2





    @cs1193 It depends on the layout of variables on the stack. If tt is right before hh, the final nul character will overwrite the first byte of hh, setting it to zero on a little endian architecture.

    – nwellnhof
    Apr 12 '16 at 11:01








3




3





You are 1-char short on char tt[2]; You forgot about the nul-terminating character. You need at minimum char tt[3] = ""; to hold AM or PM (the initialization is just good practice)

– David C. Rankin
Apr 12 '16 at 10:32







You are 1-char short on char tt[2]; You forgot about the nul-terminating character. You need at minimum char tt[3] = ""; to hold AM or PM (the initialization is just good practice)

– David C. Rankin
Apr 12 '16 at 10:32















@DavidC.Rankin, how does variable hh return zero, just because of char pointer is having a shorter storage space? and how does it run on hackerrank?

– cs1193
Apr 12 '16 at 10:35







@DavidC.Rankin, how does variable hh return zero, just because of char pointer is having a shorter storage space? and how does it run on hackerrank?

– cs1193
Apr 12 '16 at 10:35






3




3





It runs poorly. You invoke undefined behavior.

– Lundin
Apr 12 '16 at 10:41





It runs poorly. You invoke undefined behavior.

– Lundin
Apr 12 '16 at 10:41




2




2





@cs1193 It depends on the layout of variables on the stack. If tt is right before hh, the final nul character will overwrite the first byte of hh, setting it to zero on a little endian architecture.

– nwellnhof
Apr 12 '16 at 11:01





@cs1193 It depends on the layout of variables on the stack. If tt is right before hh, the final nul character will overwrite the first byte of hh, setting it to zero on a little endian architecture.

– nwellnhof
Apr 12 '16 at 11:01












1 Answer
1






active

oldest

votes


















1














With the changes made to allow space in tt to accommodate the nul-terminating character, the code functions well in making conversions to military time. By initializing tt to zero all elements during declaration/definition, you have insure tt will be nul-terminated when AM or PM are added. e.g.



#include <stdio.h>
#include <string.h>

int main (void) {

int hh, mm, ss;
char tt[3] = "";

printf (" enter time in HH:MM:SS AM/PM: ");
if (scanf ("%d:%d:%d %[^n]%*c", &hh, &mm, &ss, tt) != 4) {
fprintf (stderr, "error: invalid read of time.n");
}

hh = (strcmp (tt, "PM") == 0 && hh != 12) ? hh + 12 : hh;
if (strcmp (tt, "AM") == 0 && hh == 12) hh = 0;
printf (" %02d:%02d:%02dnn", hh, mm, ss);

return 0;
}


Example Use/Output



$ ./bin/tmread
enter time in HH:MM:SS AM/PM: 11:59:04 PM
23:59:04


Look it over and let me know if you have any questions.






share|improve this answer


























  • scanf with "%s" is dangerous, since it does not limit the string length. See here msdn.microsoft.com/en-us/library/xdb9w69d.aspx or other places for how to avoid this problem in general.

    – Stian Skjelstad
    Apr 14 '16 at 10:50











  • Agreed, it can always be replaced with "%[^n]%*c" which is probably the better solution. It was originally left as %s since we were dealing with AM, PM in a fixed format file, it avoided having to discuss the use of the assignment suppression operator, etc...

    – David C. Rankin
    Apr 14 '16 at 16:11














Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36570537%2fc-program-time-conversion%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














With the changes made to allow space in tt to accommodate the nul-terminating character, the code functions well in making conversions to military time. By initializing tt to zero all elements during declaration/definition, you have insure tt will be nul-terminated when AM or PM are added. e.g.



#include <stdio.h>
#include <string.h>

int main (void) {

int hh, mm, ss;
char tt[3] = "";

printf (" enter time in HH:MM:SS AM/PM: ");
if (scanf ("%d:%d:%d %[^n]%*c", &hh, &mm, &ss, tt) != 4) {
fprintf (stderr, "error: invalid read of time.n");
}

hh = (strcmp (tt, "PM") == 0 && hh != 12) ? hh + 12 : hh;
if (strcmp (tt, "AM") == 0 && hh == 12) hh = 0;
printf (" %02d:%02d:%02dnn", hh, mm, ss);

return 0;
}


Example Use/Output



$ ./bin/tmread
enter time in HH:MM:SS AM/PM: 11:59:04 PM
23:59:04


Look it over and let me know if you have any questions.






share|improve this answer


























  • scanf with "%s" is dangerous, since it does not limit the string length. See here msdn.microsoft.com/en-us/library/xdb9w69d.aspx or other places for how to avoid this problem in general.

    – Stian Skjelstad
    Apr 14 '16 at 10:50











  • Agreed, it can always be replaced with "%[^n]%*c" which is probably the better solution. It was originally left as %s since we were dealing with AM, PM in a fixed format file, it avoided having to discuss the use of the assignment suppression operator, etc...

    – David C. Rankin
    Apr 14 '16 at 16:11


















1














With the changes made to allow space in tt to accommodate the nul-terminating character, the code functions well in making conversions to military time. By initializing tt to zero all elements during declaration/definition, you have insure tt will be nul-terminated when AM or PM are added. e.g.



#include <stdio.h>
#include <string.h>

int main (void) {

int hh, mm, ss;
char tt[3] = "";

printf (" enter time in HH:MM:SS AM/PM: ");
if (scanf ("%d:%d:%d %[^n]%*c", &hh, &mm, &ss, tt) != 4) {
fprintf (stderr, "error: invalid read of time.n");
}

hh = (strcmp (tt, "PM") == 0 && hh != 12) ? hh + 12 : hh;
if (strcmp (tt, "AM") == 0 && hh == 12) hh = 0;
printf (" %02d:%02d:%02dnn", hh, mm, ss);

return 0;
}


Example Use/Output



$ ./bin/tmread
enter time in HH:MM:SS AM/PM: 11:59:04 PM
23:59:04


Look it over and let me know if you have any questions.






share|improve this answer


























  • scanf with "%s" is dangerous, since it does not limit the string length. See here msdn.microsoft.com/en-us/library/xdb9w69d.aspx or other places for how to avoid this problem in general.

    – Stian Skjelstad
    Apr 14 '16 at 10:50











  • Agreed, it can always be replaced with "%[^n]%*c" which is probably the better solution. It was originally left as %s since we were dealing with AM, PM in a fixed format file, it avoided having to discuss the use of the assignment suppression operator, etc...

    – David C. Rankin
    Apr 14 '16 at 16:11
















1












1








1







With the changes made to allow space in tt to accommodate the nul-terminating character, the code functions well in making conversions to military time. By initializing tt to zero all elements during declaration/definition, you have insure tt will be nul-terminated when AM or PM are added. e.g.



#include <stdio.h>
#include <string.h>

int main (void) {

int hh, mm, ss;
char tt[3] = "";

printf (" enter time in HH:MM:SS AM/PM: ");
if (scanf ("%d:%d:%d %[^n]%*c", &hh, &mm, &ss, tt) != 4) {
fprintf (stderr, "error: invalid read of time.n");
}

hh = (strcmp (tt, "PM") == 0 && hh != 12) ? hh + 12 : hh;
if (strcmp (tt, "AM") == 0 && hh == 12) hh = 0;
printf (" %02d:%02d:%02dnn", hh, mm, ss);

return 0;
}


Example Use/Output



$ ./bin/tmread
enter time in HH:MM:SS AM/PM: 11:59:04 PM
23:59:04


Look it over and let me know if you have any questions.






share|improve this answer















With the changes made to allow space in tt to accommodate the nul-terminating character, the code functions well in making conversions to military time. By initializing tt to zero all elements during declaration/definition, you have insure tt will be nul-terminated when AM or PM are added. e.g.



#include <stdio.h>
#include <string.h>

int main (void) {

int hh, mm, ss;
char tt[3] = "";

printf (" enter time in HH:MM:SS AM/PM: ");
if (scanf ("%d:%d:%d %[^n]%*c", &hh, &mm, &ss, tt) != 4) {
fprintf (stderr, "error: invalid read of time.n");
}

hh = (strcmp (tt, "PM") == 0 && hh != 12) ? hh + 12 : hh;
if (strcmp (tt, "AM") == 0 && hh == 12) hh = 0;
printf (" %02d:%02d:%02dnn", hh, mm, ss);

return 0;
}


Example Use/Output



$ ./bin/tmread
enter time in HH:MM:SS AM/PM: 11:59:04 PM
23:59:04


Look it over and let me know if you have any questions.







share|improve this answer














share|improve this answer



share|improve this answer








edited Apr 14 '16 at 16:12

























answered Apr 13 '16 at 8:08









David C. RankinDavid C. Rankin

43.3k33151




43.3k33151













  • scanf with "%s" is dangerous, since it does not limit the string length. See here msdn.microsoft.com/en-us/library/xdb9w69d.aspx or other places for how to avoid this problem in general.

    – Stian Skjelstad
    Apr 14 '16 at 10:50











  • Agreed, it can always be replaced with "%[^n]%*c" which is probably the better solution. It was originally left as %s since we were dealing with AM, PM in a fixed format file, it avoided having to discuss the use of the assignment suppression operator, etc...

    – David C. Rankin
    Apr 14 '16 at 16:11





















  • scanf with "%s" is dangerous, since it does not limit the string length. See here msdn.microsoft.com/en-us/library/xdb9w69d.aspx or other places for how to avoid this problem in general.

    – Stian Skjelstad
    Apr 14 '16 at 10:50











  • Agreed, it can always be replaced with "%[^n]%*c" which is probably the better solution. It was originally left as %s since we were dealing with AM, PM in a fixed format file, it avoided having to discuss the use of the assignment suppression operator, etc...

    – David C. Rankin
    Apr 14 '16 at 16:11



















scanf with "%s" is dangerous, since it does not limit the string length. See here msdn.microsoft.com/en-us/library/xdb9w69d.aspx or other places for how to avoid this problem in general.

– Stian Skjelstad
Apr 14 '16 at 10:50





scanf with "%s" is dangerous, since it does not limit the string length. See here msdn.microsoft.com/en-us/library/xdb9w69d.aspx or other places for how to avoid this problem in general.

– Stian Skjelstad
Apr 14 '16 at 10:50













Agreed, it can always be replaced with "%[^n]%*c" which is probably the better solution. It was originally left as %s since we were dealing with AM, PM in a fixed format file, it avoided having to discuss the use of the assignment suppression operator, etc...

– David C. Rankin
Apr 14 '16 at 16:11







Agreed, it can always be replaced with "%[^n]%*c" which is probably the better solution. It was originally left as %s since we were dealing with AM, PM in a fixed format file, it avoided having to discuss the use of the assignment suppression operator, etc...

– David C. Rankin
Apr 14 '16 at 16:11






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36570537%2fc-program-time-conversion%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini