Why this works?!

V

v4vijayakumar

Why this works?!

#include <stdio.h>

int main()
{
const char * str =
"123"
"456"
"\n";

printf("%s", str);

return 0;
}
 
U

user923005

Why this works?!

#include <stdio.h>

int main()
{
const char * str =
"123"
"456"
"\n";

        printf("%s", str);

        return 0;



}

In translation phase 6: "Adjacent string literal tokens are
concatenated."
 
B

Ben Bacarisse

v4vijayakumar said:
Why this works?!

#include <stdio.h>

int main()
{
const char * str =
"123"
"456"
"\n";

printf("%s", str);

return 0;
}

Why questions can be hard. I suppose the answer is that it is a
useful facility so it got included in the formal definition of the
language.

One common use is in building format strings. Imagine a program that
uses lots of doubles, but you are not sure if you want need to make
them all floats later to save space. You can write:

typedef double floating_point;
#define FP_FMT "%lf"
...
floating_point x, y;
scanf(FP_FMT "," FP_FMT, &x, &y);

and you change to float simply by changing:

typedef float floating_point;
#define FP_FMT "%f"

A similar technique is used by C99 to provide formats for the integer
types whose sizes you can easily determine, like int_fast16_t
(e.g. SCNdFAST16 is the right string to read one of these as decimal
digits).

The other nice use is to interpolate a max-width in string scanf
format:

#define SIZE 10
#define str(x) #x
#define xstr(x) str(x)


char buf[SIZE + 1];
...
scanf("> %" xstr(SIZE) "s", buf);
 
M

Martin Ambuhl

v4vijayakumar said:
Why this works?!
#include <stdio.h>
int main()
{
const char * str =
"123"
"456"
"\n";
printf("%s", str);
return 0;
}

String literals separated by (at most) white space are concatenated. so
your code is equivalent to

#include <stdio.h>
int main()
{
const char * str = "123" "456" "\n";
printf("%s", str);
return 0;
}

which is equivalent to

#include <stdio.h>
int main()
{
const char * str = "123456\n";
printf("%s", str);
return 0;
}

Two minot points, however:
1) It is best to specify that main takes no arguments when it does not
int main(void).
2) It is wise to avoid tabs in code posted to newsgroups or anytime code
will evr be read by other people. Tabs are interpreted differently
by different hardware and by different software. You have no
control over, or even reasonable expectation about, how those tabs
will be rendered.
 
C

CBFalconer

Martin said:
String literals separated by (at most) white space are concatenated.
so your code is equivalent to

Note step 6 below. From N869.txt.

5.1.1.2 Translation phases

[#1] The precedence among the syntax rules of translation is
specified by the following phases.5)

1. Physical source file multibyte characters are mapped
to the source character set (introducing new-line
characters for end-of-line indicators) if necessary.
Trigraph sequences are replaced by corresponding
single-character internal representations.

.....

6. Adjacent string literal tokens are concatenated.
____________________

5) Implementations shall behave as if these separate phases
occur, even though many are typically folded together in
practice.
 
A

Army1987

Ben said:
v4vijayakumar said:
Why this works?!
[string literal concatenation]
The other nice use is to interpolate a max-width in string scanf
format:

#define SIZE 10
#define str(x) #x
#define xstr(x) str(x)


char buf[SIZE + 1];
...
scanf("> %" xstr(SIZE) "s", buf);
Since this only works if SIZE is a decimal constant anyway, couldn't you
just use e.g. #define SIZE_DEC "10"?

To the OP: another use of this is to allow to split very long lines, e.g.
"This is a very, very, very, very, very, very, very, very, very, very, "
"very long string.\n" (Yes, I'm out of imagination today...)
"Or maybe you could include comments " /* like this one */ "in string"
"literals" /* not something vitally useful, but... */ ".\n"
 
B

Ben Bacarisse

Army1987 said:
Ben said:
v4vijayakumar said:
Why this works?!
[string literal concatenation]
The other nice use is to interpolate a max-width in string scanf
format:

#define SIZE 10
#define str(x) #x
#define xstr(x) str(x)


char buf[SIZE + 1];
...
scanf("> %" xstr(SIZE) "s", buf);
Since this only works if SIZE is a decimal constant anyway, couldn't you
just use e.g. #define SIZE_DEC "10"?

It is nicer to have the 10 in only one place -- used for the array
size (+1) and the input specification.
 
J

J. J. Farrell

v4vijayakumar said:
Why this works?!

#include <stdio.h>

int main()
{
const char * str =
"123"
"456"
"\n";

printf("%s", str);

return 0;
}

Because it's correct C. Why shouldn't it work?
 
B

Bill Reid

CBFalconer said:
Martin said:
String literals separated by (at most) white space are concatenated.
so your code is equivalent to

Note step 6 below. From N869.txt.

5.1.1.2 Translation phases

[#1] The precedence among the syntax rules of translation is
specified by the following phases.5)

1. Physical source file multibyte characters are mapped
to the source character set (introducing new-line
characters for end-of-line indicators) if necessary.
Trigraph sequences are replaced by corresponding
single-character internal representations.

....

6. Adjacent string literal tokens are concatenated.
____________________

5) Implementations shall behave as if these separate phases
occur, even though many are typically folded together in
practice.

So is that this works why?

printf("Hello "
"world\n");

or this:

no_init_file_option=alert_user
(ALERT_ERROR,&no_init_file_menu,
"Initialization file not found in program directory\n"
"Program cannot be started without file in directory");

??!??!!??
 
J

James Kuyper

Bill said:
....

So is that this works why?

printf("Hello "
"world\n");

or this:

no_init_file_option=alert_user
(ALERT_ERROR,&no_init_file_menu,
"Initialization file not found in program directory\n"
"Program cannot be started without file in directory");

Yes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top