-1

This is the code but I am getting errors, too few arguments in realtime and expected expression before char.

I am trying to write a method then pass information(input) through the method and get a value(output)

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



char realtime(char input[20], char output[25]){

    //char input[20];
    //char output [25];
    char year[5];
    char month[3];
    char day[3];
    char hour[3];
    char min[3];
    char sec[3];
    strncpy(year, input, 4);
    year[4] = '\0';
    strncpy(month, input + 4, 2);
    month[2] = '\0';
    strncpy(day, input + 6, 2);
    day[2] = '\0';
    strncpy(hour, input + 8, 2);
    hour[2] = '\0';
    strncpy(min, input + 10, 2);
    min[2] = '\0';
    strncpy(sec, input + 12, 2);
    sec[2] = '\0';
    sprintf(output, "%s-%s-%sT%s:%s:%s", year, month, day, hour, min, sec);
    return 0;
}

int main(){
    char input = "20181204193456";
    realtime( char input[20], char output[25]);
    printf("Parsed Date %s", output);
}

6 Answers6

1
char input = "20181204193456";

is invalid. This is a single character input that is initialized with the content of a pointer that points to a string literal "20181204193456". This is not what you want. You want:

char input[] = "20181204193456";

This is a character array (read as: 'string') that is intialized with the content of string literal "20181204193456". The length of the array includes termination character on the end, but without specifing the size [] we left it as automatically detected by the compiler.

realtime(char input[20], char output[25]);

This is wrong and invalid. You want:

char outout[25];
realtime(input, output);

First you want to reserve 25 characters of output space to have anything to write to. Then you want to run the function, with the first parameter input and the second being the output.

After fixing such errors your good to go.

However:

sprintf(output, "%s-%s-%sT%s:%s:%s", year, month, day, hour, min, sec);

is ok, but it's better to use snprintf:

snprintf(output, 25, "%s-%s-%sT%s:%s:%s", year, month, day, hour, min, sec);

Without the 25 and snprintf if the length is accidentally longer it will result in writing past the array size. It may seem reduntant here, but it's better to use it everywhere.

strncpy(arr, ..., len - 1)
arr[len] = '\0'
sprintf(... "%s", arr);

is ok. But you can just:

strlcpy(arr, ..., len)
sprintf(... "%s", arr);

or even better:

sprintf(... "%.*s", len, arr);

so the whole function is just:

char realtime(char input[20], char output[25]){
    snprintf(output, 25, "%.4s-%.2s-%.2sT%.2s:%.2s:%.2s", 
         input, input + 4, input + 6, 
         input + 8, input + 10, input + 12);
}
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
1

You need to define a variable in main to hold the output, and the definition and call to realtime is incorrect. Maybe this would work better:

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

char realtime(char *input, char *output){

    //char input[20];
    //char output [25];
    char year[5];
    char month[3];
    char day[3];
    char hour[3];
    char min[3];
    char sec[3];
    strncpy(year, input, 4);
    year[4] = '\0';
    strncpy(month, input + 4, 2);
    month[2] = '\0';
    strncpy(day, input + 6, 2);
    day[2] = '\0';
    strncpy(hour, input + 8, 2);
    hour[2] = '\0';
    strncpy(min, input + 10, 2);
    min[2] = '\0';
    strncpy(sec, input + 12, 2);
    sec[2] = '\0';
    sprintf(output, "%s-%s-%sT%s:%s:%s", year, month, day, hour, min, sec);
    return 0;
}

int main(){
    char input[] = "20181204193456";
    char output[25];

    realtime(input, output);
    printf("Parsed Date %s", output);

    return 0;
}
Simon Bagley
  • 318
  • 2
  • 15
0

You are defining a char not a string. So try:

 char input[20] = "20181204193456";
Mike
  • 4,041
  • 6
  • 20
  • 37
0

You can't assign a string literal to a char array like that. Instead of this:

char input = "20181204193456";

you have to do this:

char input[20];
strcpy(input, "20181204193456");

Also, this call here:

realtime( char input[20], char output[25]);

Is just a copy of the function signature. Instead do this:

realtime(input, output);

You also need to define output beforehand. All in all, your main function should look like this:

int main() {
    char input[20];
    strcpy(input, "20181204193456");
    char output[20];
    realtime(input, output);
    printf("Parsed Date %s", output);
    // depending on the system, maybe wait for user input or pause here so the window doesn't immediately close
    return 0;
}

Also note the added return 0; indicating successful program execution.

Of course, it can also be done entirely without input by directly handing the string literal to the function:

int main() {
    char output[20];
    realtime("20181204193456", output);
    printf("Parsed Date %s", output);
    return 0;
}
Blaze
  • 16,736
  • 2
  • 25
  • 44
  • Why bother with strcpy when there's a literal? – nm_tp Dec 10 '18 at 14:44
  • You're right. I tried to keep it as close to OP's design as possible, but it makes more sense without `input` of course. I added it to the answer. – Blaze Dec 10 '18 at 14:48
0

First of all, in your main function, you declare and initialize:

char input = "20181204193456";

In C, char is 1 byte type. Strings in C are represented by arrays of chars. Therefore, you would need:

char input[15] = "20181204193456";

Although there are only 14 characters in the string, you need 15 because, in C, every string is to be terminated with a 0 byte. This is done automatically in this case. Another way would be:

char input[] = "20181204193456"

In which case, the compiler will determine how much space you need.

Next, your function call is not correct:

realtime( char input[20], char output[25]);

When calling a function, you should only use parameter names, not their types, like this:

realtime(input, output);

Given that output isn't declared, you'll first need to declare it. The working code follows:

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



char realtime(char input[20], char output[25])
{
    char year[5];
    char month[3];
    char day[3];
    char hour[3];
    char min[3];
    char sec[3];
    strncpy(year, input, 4);
    year[4] = '\0';

    strncpy(month, input + 4, 2);
    month[2] = '\0';
    strncpy(day, input + 6, 2);
    day[2] = '\0';
    strncpy(hour, input + 8, 2);
    hour[2] = '\0';
    strncpy(min, input + 10, 2);
    min[2] = '\0';
    strncpy(sec, input + 12, 2);
    sec[2] = '\0';
    sprintf(output, "%s-%s-%sT%s:%s:%s", year, month, day, hour, min, sec);

    return 0;
}

int main()
{
    char input[15] = "20181204193456";
    char output[25];
    realtime(input, output);
    printf("Parsed Date %s", output);
}
nm_tp
  • 463
  • 3
  • 15
-1

Declare the arrays input and output globally so that you could use it across methods (or) functions. Something like this,

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

char realtime(char input[20], char output[25]){

    //char input[20];
    //char output [25];
    char year[5];
    char month[3];
    char day[3];
    char hour[3];
    char min[3];
    char sec[3];
    strncpy(year, input, 4);
    year[4] = '\0';
    strncpy(month, input + 4, 2);
    month[2] = '\0';
    strncpy(day, input + 6, 2);
    day[2] = '\0';
    strncpy(hour, input + 8, 2);
    hour[2] = '\0';
    strncpy(min, input + 10, 2);
    min[2] = '\0';
    strncpy(sec, input + 12, 2);
    sec[2] = '\0';
    sprintf(output, "%s-%s-%sT%s:%s:%s", year, month, day, hour, min, sec);
    return 0;
    }
int main(){
    //char input = "20181204193456";

    char input[20] = "20181204193456";
    char output[20];      

    realtime( input, output);
    printf("Parsed Date %s", output);
}
Tom Taylor
  • 3,344
  • 2
  • 38
  • 63
  • 1
    This is generally bad advice and it doesn't work in this example. The function parameters shadow the global variable names. Inside the function `input` and `output` refer to the parameters. – Blastfurnace Dec 10 '18 at 11:31
  • Why would you say that it would not work in this example? – Tom Taylor Dec 10 '18 at 11:32
  • Why exactly do you suggest they should be global variables? – Blastfurnace Dec 10 '18 at 11:34
  • You don't think they can be passed to functions if they're defined inside `main()`? – Blastfurnace Dec 10 '18 at 11:35
  • Then it should be `pass by reference`. Here he is trying to do `pass by value` and since he is new to C I would not like to confuse / dump too many things on him.. – Tom Taylor Dec 10 '18 at 11:36
  • 2
    They're arrays, they decay to pointers when passed to functions. They are passed "by reference". – Blastfurnace Dec 10 '18 at 11:38
  • Agreed ! What you said is right. Thanks a lot for pointing out ! Have suggested to declare the `input` and `output` arrays within main and using it. Thanks again @Blastfurnace – Tom Taylor Dec 10 '18 at 11:42
  • There are no "references" in C. Just pointers, which are addresses. When you pass something "by reference", you're just sending a pointer, i.e. that pointer is being sent by value. See this https://stackoverflow.com/questions/4305673/does-c-have-references – nm_tp Dec 10 '18 at 14:51
  • And that is what pass by reference mean. You are passing the "addresses" - and not the actual values - with the help of which you are referencing the value in memory. – Tom Taylor Dec 10 '18 at 14:56
  • @rm-rfstar: Well, you both are somewhat right. While the ISO language document for C isn't using the "by reference terminology" it is used that way. But just by language definition `&` is the "address of" operator. So while being the same thing, technically it is correct to say "there is no pass by reference in plain C" – dhein Dec 11 '18 at 05:44