The code
element does not change normal HTML formatting rules, which collapse consecutive whitespace to a single space. It does not do much else either, beyond setting the font to monospace by default, though in special cases it may have some effect (e.g., automatic translation software might treat code
elements so that their content is left untranslated).
You could use CSS to request different formatting:
<style>
code { white-space: pre }
</style>
But this is unnecessarily fragile (CSS rules might be ignored for various reasons), when you can simply use the pre
element (and remove the br
tags):
<pre>
int main(){
printf("Hello World!");
}
</pre>
If you wish to additionally use the code
element for some reason, then the valid way is to nest code
inside pre
, not vice versa. Here you then need to notice that special rules about ignoring a line break after a <pre>
tag do not apply here (since there would be an intervening tag), so to avoid getting an extra line break at the beginning, you would need to write like this:
<pre><code> int main(){
printf("Hello World!");
}</code></pre>
P.S. Neither code
nor pre
changes the rules about the markup-significant characters <
and &
, which should be encoded as <
and &
, respectively. For example, text like a<b&c
would need to be written as a<b&c
.