3

I have follow code same as it is on different lines:

frame_rate = (float)
                ( ( ( frames * media_timescale) + \   //WHY???
                    ( media_duration >> 1 ) ) /
                 media_duration);

I am not understanding what is backslash doing in source file ? Also to calculate the frame rate simply we can do as follow:

frame_rate = (float) ( (  frames * media_timescale) / media_duration);

Is there any specific intention to write first type of code?

Mohan
  • 1,871
  • 21
  • 34
  • 1
    That isn't a macro, so that \ is *pointless*. The code would be completely valid without it. I suspect someone sniped that code from what was at-one-time a macro, and just left it as-is. – WhozCraig Feb 08 '17 at 09:47
  • 1
    The `+ (media_duration >> 1)` is intended to round the value up, assuming that you convert the result to an `int` at some point. – user3386109 Feb 08 '17 at 09:51

4 Answers4

5

It's a line-continuation escape. It means the compiler will consider the next line as a part of the current line.

It makes the code you show in practice being

frame_rate = (float)
                ( ( ( frames * media_timescale) + ( media_duration >> 1 ) ) /
                 media_duration);

There's no real need for it in this case though, except as maybe some style-guide followed by the author had it as a requirement.

Line continuation is needed to define "multi-line" macros, and are also often used for long strings. But besides those places, it serves no real purpose.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

Backslash is used to split a long line into 2 shorter lines, without signifying an end of the statement. This is usually done when a single line statement might be too long to read.

ctrl-shift-esc
  • 876
  • 1
  • 9
  • 19
1

The backslash is a line continuation. It makes no difference other than for readability purposes (80 characters per line is usually the recommended limit for most style guides).

See: How can I do a line break (line continuation) in Python?

Community
  • 1
  • 1
  • I've removed the rather blatant copy of someone else's example. When you want to reference someone else's work, please use proper quoting and attribution, and only *sparingly to support your own words*. – Martijn Pieters Feb 08 '17 at 10:29
  • And I don't see how a link to a Python post is relevant to a question about C. Python's logical line rules differ in some crucial areas from those in C. – Martijn Pieters Feb 08 '17 at 10:31
0

I don't think anybody could describe this better than the standard. Hence, according to C11/section 5.1.1.2p1, which summarises the stages of translation (compilation, if you will) which occur when you invoke your compiler:

  1. Each instance of a backslash character (\) immediately followed by a new-line character is deleted, splicing physical source lines to form logical source lines. Only the last backslash on any physical source line shall be eligible for being part of such a splice. A source file that is not empty shall end in a new-line character, which shall not be immediately preceded by a backslash character before any such splicing takes place.

Thus, from your example, the first line ends up effectively spliced (joined) with the second due to the combination of the backslash and newline.

autistic
  • 1
  • 3
  • 35
  • 80
  • 1
    According to this section, shoudn't not only macro definitions, but also lines of code broken in two lines by the combination of backslash and newline characters, such as `puts("Single" \ "Line");`, be translated by the preprocessor into `puts("Single" "Line");` and then `puts("Single Line");` when the adjacent strings are concatenated? – dandev486 Jan 16 '22 at 13:12
  • 1
    @dandev486 your understanding seems mostly correct; line splice elimination occurs long before preprocessing. – autistic Jan 17 '22 at 09:00
  • I see, so it actually happens prior to the preprocessing phase, thanks for the clarification! I thought that all steps until macro replacements were performed by the preprocessor. – dandev486 Jan 17 '22 at 09:45
  • 1
    @dandev486 yes, line splice elimination is in translation phase 2 (hence the 2 at the start of the quote from the standard). Translation phase 3 is a bit of tokenization. Translation phase 4 is where preprocessing proper begins. – autistic Jan 17 '22 at 22:05