-2

I am trying to create a basic alarm where the user inputs a start time (HHMMSS) and a end time (HHMMSS) and I wish to just display this time in the format (HH:MM:SS) (24 hour time). I am currently at this but I have hit a Segmentation Error. I am fairly new to Coding in C so any help is greatly appreciated.

int main() { 
    int present_time; 
    int time_for_alarm; 
    char outputHolder[30]; 
    printf ("Please input present time\n"); 
    scanf ("%d", &present_time); 
    printf ("Please input time for alarm\n"); 
    scanf ("%d", &time_for_alarm); 
    strftime(present_time, sizeof(present_time), "%H:%M:%S", outputHolder); 
    while (present_time < time_for_alarm) { 
        sleep(1); 
        printf ("%d\n", present_time); 
        present_time++; 
    } 
    sleep(1); 
    printf ("ALARM"); 
    return (0); 
}
user694733
  • 15,208
  • 2
  • 42
  • 68
  • 3
    Please don't post links to images of code. Post the code, as in *paste it into the question*. – unwind Sep 21 '17 at 11:02
  • I was having issues with the formatting, and I really needed to get this done. Sorry ,I will make sure I don't do it again :) – M. DelV Sep 21 '17 at 11:05
  • 3
    Posting pictures of code is an excellent way to get your question closed and down-voted to oblivion. – Lundin Sep 21 '17 at 11:07
  • If your code is ill formatted, someone may fix it but for pictures this won't work. – Gerhardh Sep 21 '17 at 11:09
  • 2
    Don't you get warnings for your call to `strftime`? The output buffer and its size go first and the last argument is a `struct tm`. Also, this function doesn't work on any old interger like `123000`that is supposed to be a time; it uses the definitions of `` – M Oehm Sep 21 '17 at 11:10
  • 1
    Please don't post code in comments. It's unreadable. [Edit] your question instead. – user694733 Sep 21 '17 at 11:11
  • @M.DelV you also may delete you obsolete comments – Jabberwocky Sep 21 '17 at 11:15
  • 2
    I'm not sure how you don't get any warnings with this line: `strftime(present_time, sizeof(present_time), "%H:%M:%S", outputHolder);` since the prototype of `strftime` is as follows: `size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr);` So as a start you should add a `tm` struct, `struct tm *info;` and then use `strftime` like this: `strftime(outputholder, 30, "%H:%M:%S", info);` note that the amount of bytes is the same as the size of your output holder. – Arrrow Sep 21 '17 at 11:27
  • 1
    Welcome to Stack Overflow! Please [edit] your question to show us what kind of debugging you've done. I expect you have run your [mcve] within Valgrind or a similar checker, and to have investigated with a debugger such as GDB, for example. Ensure you've enabled a full set of compiler warnings, too. What did the tools tell you, and what information are they missing? And read Eric Lippert's [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Toby Speight Sep 21 '17 at 12:16

3 Answers3

0

Adding the necessary includes:

#include <stdio.h>
#include <time.h>
#include <unistd.h>

reduces the error messages down to just

gcc-7 -std=c11 -fPIC -g -Wall -Wextra -Wwrite-strings -Wno-parentheses -Wpedantic -Warray-bounds      46342229.c    -o 46342229
46342229.c: In function ‘main’:
46342229.c:13:14: warning: passing argument 1 of ‘strftime’ makes pointer from integer without a cast [-Wint-conversion]
     strftime(present_time, sizeof(present_time), "%H:%M:%S", outputHolder);
              ^~~~~~~~~~~~
In file included from 46342229.c:2:0:
/usr/include/time.h:205:15: note: expected ‘char * restrict’ but argument is of type ‘int’
 extern size_t strftime (char *__restrict __s, size_t __maxsize,
               ^~~~~~~~
46342229.c:13:62: warning: passing argument 4 of ‘strftime’ from incompatible pointer type [-Wincompatible-pointer-types]
     strftime(present_time, sizeof(present_time), "%H:%M:%S", outputHolder);
                                                              ^~~~~~~~~~~~
In file included from 46342229.c:2:0:
/usr/include/time.h:205:15: note: expected ‘const struct tm * restrict’ but argument is of type ‘char *’
 extern size_t strftime (char *__restrict __s, size_t __maxsize,
               ^~~~~~~~

From this, it can easily be seen that your strftime() call has the arguments mis-ordered - you probably want something like strftime(outputHolder, sizeof outputHolder, "%H:%M:%S", &alarm_tm);, where alarm_tm is a suitably filled-in struct tm.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
0

I just wanted to add something about the usage of strftime().

Its prototype is

size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr)

The last argument must be the address of a struct tm.

Create a struct tm pointer and read into its tm_hour, tm_min and tm_sec elements.

time_t t;
time(&t);
struct tm *ptr=localtime(&t);
printf ("Please input present time\n"); 
scanf ("%2d%2d%2d", &ptr->tm_hour, &ptr->tm_min, &ptr->tm_sec); 

Then use strftime().

strftime(outputHolder, sizeof(outputHolder), "%H:%M:%S", ptr);
J...S
  • 5,079
  • 1
  • 20
  • 35
-1

You are not using strftime the correct way.

Read both of the following, they explain and show how to use time and stftime in C correctly.

  1. How to print time in format: 2009‐08‐10 18:17:54.811
  2. http://man7.org/linux/man-pages/man3/strftime.3.html
Idan
  • 333
  • 2
  • 8