Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3442766
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:42:33+00:00 2026-05-18T08:42:33+00:00

I do not understand the output of the following program: #include <iostream> #define FOO

  • 0

I do not understand the output of the following program:

#include <iostream>

#define FOO std::cout << __LINE__ << ' ' \
                      << __LINE__ << '\n';
int main()
{
    FOO

    std::cout << __LINE__ << ' ' \
              << __LINE__ << '\n';
}

The first output is 7 and 7, indicating that the expansion of FOO is a single logical line, but the second output is 9 and 10, indicating two distinct logical lines. Why is there a difference?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:42:33+00:00Added an answer on May 18, 2026 at 8:42 am

    Because

    1:  #include <iostream>
    2: 
    3:  #define FOO std::cout << __LINE__ << ' ' \
    4:                        << __LINE__ << '\n';
    5:  int main()
    6:  {
    7:      FOO // the first two __LINE__s come from here, that's one line of code
    8: 
    9:      std::cout << __LINE__ << ' ' \ // 3rd __LINE__ comes from here
    10:              << __LINE__ << '\n'; // 4th __LINE__ comes from here
    11: }
    

    __LINE__ expands to physical lines, not logical lines:

    The line number of the current source line is one greater than the number of new-line characters read or introduced in translation phase 1 (2.2) while processing the source file to the current token.

    While the lines ended by \ are concatenated in translation phase 2.

    The other only logical implementation would be to print 3 and 4 for the invocation of FOO, but that seems not very useful.

    You can also look at this the following way: __LINE__ is not any different from any other macro. It’s just updated automatically by the compiler at the beginning of every line. So the code is kind of interpreted this way:

    #include <iostream>
    
    #define __LINE__ 3
    #define FOO std::cout << __LINE__ << ' ' \
                          << __LINE__ << '\n';
    int main()
    {
    #define __LINE__ 7
        FOO
    
    #define __LINE__ 9
        std::cout << __LINE__ << ' ' \ // Yeah, you're right
    #define __LINE__ 10
                 << __LINE__ << '\n';
    }
    

    This is not valid code, but it demonstrates how the things work. Apply the usual macro expansion rules and you’ll get the output you’ve got.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The output of following program #include<stdio.h> int main(){ int *p[10]; printf(%ld %ld\n,sizeof(*p),sizeof(p)); } is
I tried the following program on Visual Studio 2010. #include <iostream> using namespace std;
I have been trying to understand of this following C-program: #include <stdio.h> int arr[]
I could not identify how the following program outputs 6 and -250. #include<stdio.h> int
#include<stdio.h> #define a(x) (x * x) int main() { int i = 3, j;
I wrote following program int main () { char a=0xf; a=a+1; printf(%c\n,a); } the
Consider the following C++ program: #include <cstdlib> // for exit(3) #include <string> #include <iostream>
I have the following piece of code: #include <stdio.h> #include <stdlib.h> int main(int argc,
I have the following C program: int main() { int c[10] = {0, 0,
if wrote the following code and do not understand why the trace returns false:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.