Reputation: 183
The problem is that; when I'm trying to store some string containing pre-defined macros into a variable compiler is giving me an error
#include <stdio.h>
int main()
{
char string[100] = "This line is appended using C programe and,\nThe data is = %s and time is = %s.\n\n", __DATE__, __TIME__;
return 0;
}
And the error is
yyy.c: In function 'main':
yyy.c:5:111: error: expected identifier or '(' before string constant
char string[100] = "This line is appended using C programe and,\nThe data is = %s and time is = %s.\n\n", __DATE__, __TIME__;
^~~~~~~~
And when I'm trying with the below syntax
#include <stdio.h>
int main()
{
char string[100] = ("This line is appended using C programe and,\nThe data is = %s and time is = %s.\n\n", __DATE__, __TIME__);
return 0;
}
The error that occurred is
yyy.c: In function 'main':
yyy.c:5:24: error: invalid initializer
char string[100] = ("This line is appended using C programe and,\nThe data is = %s and time is = %s.\n\n", __DATE__, __TIME__);
^
but when I'm printing the same line with print statement i.e.
#include <stdio.h>
int main()
{
printf("This line is appended using C programe and,\nThe data is = %s and time is = %s.\n\n", __DATE__, __TIME__);
return 0;
}
There is no error and the output is
This line is appended using C programe and,
The data is = Jul 9 2021 and time is = 11:32:06.
Upvotes: 2
Views: 271
Reputation: 9570
The __DATE__
and __TIME__
macros expand to string constants during compilation, so what you effectively did is giving a list of three string constants in a single variable declaration:
char string[100] = "one string", "another string", "yet another one";
This doesn't fit a structure of a declaration. The string constant is an initializer to a variable and you can't give three initializers to one variable.
You could declare several variables with one declaration and give a separate initializer to each of them:
char string1[100] = "one string", string2[30] = "another string", string3[50] = "yet another one";
and that's why your compiler expects an identifier—another variable's name—after the comma.
But that's obviously not what you intend. Anyway, you'd have to concatenate them before printing or print them all in sequence to get a desired output.
Another way is concatenating them at the syntax level. The C language supports such concatenation of constants:
"this is" " a " "sing" "le str" "ing"
represents a constant
"this is a single string"
So, you can do
char string[] = "The date is " __DATE__ " and time is " __TIME__ ".\n";
to declare and define a single string resulting from joining five constants.
Please note that I omitted the array length in the declaration. When the array is initialized with an explicit initializer, the missing size is automatically computed during compilation to fit the given string length.
Upvotes: 1
Reputation: 350
Use the below syntax it will work
char str[] = "This line is appended using C programe and,\nThe data is = "__DATE__
" and time is = "__TIME__
".\n\n";
Upvotes: 3