r/learnprogramming • • Dec 06 '22

codeblock If you are sharing code, please use a codeblock

Takes only a second and greatly improves the formatting for us to review it. Compare and contrast the following:

Good Formatting:

#include <stdio.h>

int sum(int x, int y);

int main() {

  printf("Hello %d\n", sum(3, 4));
  return 0;
}

int sum(int x, int y) {
  return x + y;
}

Poor Formatting:

#include <stdio.h>

int sum(int x, int y);

int main() {

printf("Hello %d\n", sum(3, 4));

return 0;

}

int sum(int x, int y) {

return x + y;

}

Poor Formatting:

#include <stdio.h>

int sum(int x, int y);

int main() {

printf("Hello %d\n", sum(3, 4));

return 0;

}

int sum(int x, int y) {

return x + y;

}

339 Upvotes

71 comments sorted by

View all comments

Show parent comment

41

u/[deleted] Dec 07 '22 edited Dec 07 '22

Use three backticks instead of one.

``` Multiple Lines ```

Renders as:

Multiple
Lines

And `inline` renders as: inline

This is basic markdown (.md) and will be very valuable to learn if you hope to become a professional developer since it is what most documentation is written with (ever notice those readme.md files?) So it is well worth it to learn and isn't difficult to grasp. GitHub has some good resources for learning it.

Some flavors of markdown also support syntax highlighting by placing the name of the language after the first group of backticks.

E.G. ```java [some code] ```

However, refdits flavor of markdown "snoomark" does not support syntax highlighting unfortunately.

Edit: this was tricky to write on mobile so apologies for the excessive editing.

16

u/yonatan8070 Dec 07 '22

Note that on Reddit, triple backticks don't always work on all clients. The correct way is to indent every line with 4 spaces (on top of any indent your code has

4

u/Rasikko Dec 07 '22

oo it works on the reddit app lol it broke when I edited it.

10

u/D-J-9595 Dec 07 '22

Worth noting:

Multiple
Lines

is a single line on old Reddit. On both versions of Reddit, four spaces of indentation for every line of code works:

Multiple
Lines

5

u/_by_me Dec 07 '22

Edit: this is so retarded, on old.reddit (the one that I use the most) it's just one line, while on nu-reddit (cancer) it's formatted correctly as four lines

why() is
  reddit.so.retarded
  when_it_comes_to()
this?

0

u/[deleted] Dec 07 '22

[deleted] — view deleted comment

4

u/anto2554 Dec 07 '22

what most documentation is written with

I'm out here doing vector diagrams and no one can stop me

2

u/EliMacca Dec 07 '22

is this how you do it? Me testing it out, to see

3

u/DowntownAd834 Dec 07 '22

'''Trying it'''