<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Wieso darf man eine Variable vor &amp;quot;int main()&amp;quot; deklarieren, aber nicht direkt danach?]]></title><description><![CDATA[<pre><code>#include &lt;iostream&gt;

    using std::cout;
    using std::cin;
    using std::endl;
    int carrots; /* declare an integer variable */

    int main(void)

{

    carrots = 25; /* assign a value to the variable */

    cout &lt;&lt; &quot;I have &quot; &lt;&lt; carrots &lt;&lt; &quot; carrots.&quot; &lt;&lt; endl &lt;&lt; endl; /* more than one statement per line - this is not a clear code style */

    carrots = carrots - 1; /* modify the variable */

    cout &lt;&lt; &quot;Crunch, crunch. Now I have &quot;;
    cout &lt;&lt; carrots &lt;&lt; &quot; carrots.&quot; &lt;&lt; endl;
    cin.get();
    cin.get();
    cin.get();
    cin.get(); /* press 5-times enter to close the window.*/

    return 0;
}
</code></pre>
<p>Das klappt zwar... aber ist es schlecht?</p>
<pre><code>#include &lt;iostream&gt;

    using std::cout;
    using std::cin;
    using std::endl;

    int main(void)
    int carrots; /* declare an integer variable */

{

    carrots = 25; /* assign a value to the variable */

    cout &lt;&lt; &quot;I have &quot; &lt;&lt; carrots &lt;&lt; &quot; carrots.&quot; &lt;&lt; endl &lt;&lt; endl; /* more than one statement per line - this is not a clear code style */

    carrots = carrots - 1; /* modify the variable */

    cout &lt;&lt; &quot;Crunch, crunch. Now I have &quot;;
    cout &lt;&lt; carrots &lt;&lt; &quot; carrots.&quot; &lt;&lt; endl;
    cin.get();
    cin.get();
    cin.get();
    cin.get(); /* press 5-times enter to close the window.*/

    return 0;
}
</code></pre>
<p>Jedoch funktioniert das nicht mehr, wenn &quot;int carrots;&quot; direkt nach dem &quot;int main()&quot; deklariert wird. Weswegen ist das so?</p>
<p>(Das mag zwar nicht die wichtigste Frage sein, aber wäre doch schön zu wissen, weswegen das so ist.)</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/340353/wieso-darf-man-eine-variable-vor-quot-int-main-quot-deklarieren-aber-nicht-direkt-danach</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Apr 2026 04:09:20 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/340353.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 02 Nov 2016 12:45:37 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Wieso darf man eine Variable vor &amp;quot;int main()&amp;quot; deklarieren, aber nicht direkt danach? on Wed, 02 Nov 2016 12:46:48 GMT]]></title><description><![CDATA[<pre><code>#include &lt;iostream&gt;

    using std::cout;
    using std::cin;
    using std::endl;
    int carrots; /* declare an integer variable */

    int main(void)

{

    carrots = 25; /* assign a value to the variable */

    cout &lt;&lt; &quot;I have &quot; &lt;&lt; carrots &lt;&lt; &quot; carrots.&quot; &lt;&lt; endl &lt;&lt; endl; /* more than one statement per line - this is not a clear code style */

    carrots = carrots - 1; /* modify the variable */

    cout &lt;&lt; &quot;Crunch, crunch. Now I have &quot;;
    cout &lt;&lt; carrots &lt;&lt; &quot; carrots.&quot; &lt;&lt; endl;
    cin.get();
    cin.get();
    cin.get();
    cin.get(); /* press 5-times enter to close the window.*/

    return 0;
}
</code></pre>
<p>Das klappt zwar... aber ist es schlecht?</p>
<pre><code>#include &lt;iostream&gt;

    using std::cout;
    using std::cin;
    using std::endl;

    int main(void)
    int carrots; /* declare an integer variable */

{

    carrots = 25; /* assign a value to the variable */

    cout &lt;&lt; &quot;I have &quot; &lt;&lt; carrots &lt;&lt; &quot; carrots.&quot; &lt;&lt; endl &lt;&lt; endl; /* more than one statement per line - this is not a clear code style */

    carrots = carrots - 1; /* modify the variable */

    cout &lt;&lt; &quot;Crunch, crunch. Now I have &quot;;
    cout &lt;&lt; carrots &lt;&lt; &quot; carrots.&quot; &lt;&lt; endl;
    cin.get();
    cin.get();
    cin.get();
    cin.get(); /* press 5-times enter to close the window.*/

    return 0;
}
</code></pre>
<p>Jedoch funktioniert das nicht mehr, wenn &quot;int carrots;&quot; direkt nach dem &quot;int main()&quot; deklariert wird. Weswegen ist das so?</p>
<p>(Das mag zwar nicht die wichtigste Frage sein, aber wäre doch schön zu wissen, weswegen das so ist.)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2513879</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2513879</guid><dc:creator><![CDATA[Bladestorm]]></dc:creator><pubDate>Wed, 02 Nov 2016 12:46:48 GMT</pubDate></item><item><title><![CDATA[Reply to Wieso darf man eine Variable vor &amp;quot;int main()&amp;quot; deklarieren, aber nicht direkt danach? on Wed, 02 Nov 2016 12:55:21 GMT]]></title><description><![CDATA[<p>Bladestorm schrieb:</p>
<blockquote>
<p>Das klappt zwar... aber ist es schlecht?</p>
</blockquote>
<p>Ja, globale Variablen sind schlecht.</p>
<p>Bladestorm schrieb:</p>
<blockquote>
<p>Jedoch funktioniert das nicht mehr, wenn &quot;int carrots;&quot; direkt nach dem &quot;int main()&quot; deklariert wird. Weswegen ist das so?</p>
<p>(Das mag zwar nicht die wichtigste Frage sein, aber wäre doch schön zu wissen, weswegen das so ist.)</p>
</blockquote>
<p>Weil die Sprache so definiert wurde.</p>
<p>Historischer Hinweis: in K&amp;H C (uralt) wurden dort die Funktionsparameter definiert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2513882</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2513882</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Wed, 02 Nov 2016 12:55:21 GMT</pubDate></item><item><title><![CDATA[Reply to Wieso darf man eine Variable vor &amp;quot;int main()&amp;quot; deklarieren, aber nicht direkt danach? on Wed, 02 Nov 2016 13:12:19 GMT]]></title><description><![CDATA[<p>Dir ist schon klar, dass das <code>int main</code> keine Variablendeklaration ist, sondern der Anfang einer Funktionsdefinition? Anfang deshalb, weil der Rest der Funktionsdefinition, also die { } und alles dazwischen, fest dazu gehört. Und nun hast du einfach etwas dazwischen geschrieben und wunderst dich, dass das nicht funktioniert? Würde es dich auch wundern, wenn</p>
<pre><code>cout &lt;&lt; &quot;Hello World!&quot;;
-&gt;
cout &lt;&lt; int carrots; &quot;Hello World&quot;
</code></pre>
<p>nicht funktioniert?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2513888</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2513888</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Wed, 02 Nov 2016 13:12:19 GMT</pubDate></item><item><title><![CDATA[Reply to Wieso darf man eine Variable vor &amp;quot;int main()&amp;quot; deklarieren, aber nicht direkt danach? on Wed, 02 Nov 2016 14:06:17 GMT]]></title><description><![CDATA[<p>ACH SO! Danke.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2513899</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2513899</guid><dc:creator><![CDATA[Bladestorm]]></dc:creator><pubDate>Wed, 02 Nov 2016 14:06:17 GMT</pubDate></item><item><title><![CDATA[Reply to Wieso darf man eine Variable vor &amp;quot;int main()&amp;quot; deklarieren, aber nicht direkt danach? on Wed, 02 Nov 2016 16:46:59 GMT]]></title><description><![CDATA[<p>Die Variable wird da vor allem definiert. Die Deklaration gibt es gratis dazu.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2513918</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2513918</guid><dc:creator><![CDATA[DirkB]]></dc:creator><pubDate>Wed, 02 Nov 2016 16:46:59 GMT</pubDate></item><item><title><![CDATA[Reply to Wieso darf man eine Variable vor &amp;quot;int main()&amp;quot; deklarieren, aber nicht direkt danach? on Wed, 02 Nov 2016 20:34:07 GMT]]></title><description><![CDATA[<p>@ Bladestorm<br />
Es hilft zum Verständnis wenn du die Leerzeilen zwischen &quot;int main()&quot; und dem darauffolgenden &quot;{&quot; weglässt:</p>
<pre><code class="language-cpp">#include &lt;Foo.h&gt;
#include &lt;Bar.h&gt;

int eineGlobaleVariable = 123;

int EineFunktion(int einParameter)
{
    // Code von &quot;EineFunktion&quot;
    return 123;
}

int nochEineGlobaleVariable = 234;

void NochEineFunktion()
{
    // Code von &quot;NochEineFunktion&quot;
}

int dieDritteGlobaleVariable = 345;

int main() // Die Main-Funktion
{
    // Code der &quot;main&quot; Funktion
    return 0;
}

int dieVierteGlobaleVariable = 456;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2513938</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2513938</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Wed, 02 Nov 2016 20:34:07 GMT</pubDate></item></channel></rss>