Get your newlines straight

C code snippet :

1
printf("\n");

Python code snippet :

1
print('\n') #can also be "\n", it makes no difference

Let the number of newlines printed by the first code snippet (C) be a a and that printed by the second code snippet (Python) be b b .

What's the value of ( a + b ) (a+b) ?


Inspired while browsing 4chan's /sci/ board.


The answer is 3.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

In C/C++

1
printf("\n");

will print 1 newline, a= 1 \boxed{1}

In Python

1
print('\n')

will print 2 newlines, b= 2 \boxed{2}

a + b = 3 \huge a+b=3

Yup, that's the intended solution. +1 :)

Python's print() function prints a newline by default after printing the stuff that's intended to be printed using it, which makes print('\n') print an additional newline after the intended newline is printed.

To override this, one would generally use the end parameter of print() in py3. However, for the purpose of printing a single newline, print('') works quite well.

Prasun Biswas - 5 years, 5 months ago

Log in to reply

Actually, for the purpose of printing a single new line in py3, print() works too.

展豪 張 - 5 years, 3 months ago

Log in to reply

Nice! Seems like I missed noticing that. Thanks for mentioning this. +1 :)

Prasun Biswas - 5 years, 3 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...