Talk:C (programming language)/Archive 11: Difference between revisions

Content deleted Content added
MalnadachBot (talk | contribs)
m Replaced obsolete font tags and reduced Lint errors. (Task 12)
m Replaced deprecated <source> tags with <syntaxhighlight> (via WP:JWB)
Line 12:
 
:::I think you're confusing "strongly typed" and "statically typed": those ''constraint violations'' that you mentioned are due to the static types of the variables you use. This static type check can be easily circumvented, even without casts. Example:
<sourcesyntaxhighlight lang=c>
int a;
float b;
Line 18:
float* q = p;
b = *q;
</syntaxhighlight>
</source>
:::This will compile without errors or warnings, making C a weakly typed language — a strongly typed one will either not allow this, or throw an exception at runtime. C can't even be strongly typed, because it has a weak type system and lacks any concept of "objects" and associated type information at runtime. By the way, I found an excellent explanation here: [http://stackoverflow.com/questions/430182/is-c-strongly-typed Is C strongly typed?]. [[User:Adrianwn|Adrianwn]] ([[User talk:Adrianwn|talk]]) 09:28, 30 December 2009 (UTC)
 
Line 43:
'''begin removed text'''<br/>
To this day, Boolean values are commonly represented by integers in C programs. The comparison operators ('<code> > </code>', '<code>==</code>', etc.) are defined to return a signed integer (<code>int</code>) result, either zero (for false) or nonzero (for true). The Boolean operators (&&, ||) and conditional statements (if, while) in C operate on integer values, with the same interpretation. For example, the following C code
<sourcesyntaxhighlight lang="c">
int t = (x > y);
if (t) { printf("True!\n");}
else { printf("False!\n"); }
</syntaxhighlight>
</source>
is equivalent to
<sourcesyntaxhighlight lang="c">
int t;
if (x > y) { t = -1; }
Line 55:
if (t != 0) { printf("True!\n"); }
else { printf("False!\n"); }
</syntaxhighlight>
</source>
 
However, since the C language standards allow the result of a comparison to be any non-zero value, the statement <code>if(t==1){...}</code> is not equivalent to <code>if(t){...}</code> or to <code>if(t!=0){...}</code>.
Line 62:
 
For convenience, many programmers and [[C header file]]s use C's [[typedef]] facility to define a Boolean type (which may be named <code>Boolean</code>, <code>boolean</code>, <code>bool</code>, etc.). The Boolean type may be just an alias for a numeric type like <code>int</code> or <code>char</code>. In that case, programmers often define also [[C preprocessor|macros]] for the true and false values. For example,
<sourcesyntaxhighlight lang="c">
typedef int bool;
#define FALSE 0
Line 70:
...
if (f) { ... }
</syntaxhighlight>
</source>
The defined values of the <code>TRUE</code> and <code>FALSE</code> macros must be adequate for the chosen Boolean type. Note that, on the now common [[two's complement]] computer architectures, the signed value <code>-1</code> is converted to a non-zero value (<code>~0</code>, the bit-wise complement of zero) when [[type casting|cast]] to an unsigned type, or assigned to an unsigned variable.
 
Another common choice is to define the Boolean type as an [[enumerated type]] (<code>enum</code>) allows naming elements in the language of choice. For example, the following code uses the English names FALSE and TRUE as possible values of the type ''boolean''. In this case, care must be taken so that the false value is represented internally as a zero integer:
<sourcesyntaxhighlight lang="c">
typedef enum { FALSE, TRUE } boolean;
...
Line 80:
...
if (b) { ... }
</syntaxhighlight>
</source>
 
Again, since a true result may be any non-zero value, the tests <code>if(t==TRUE){...}</code> and <code>if(t)</code>, which are equivalent in other languages, are not equivalent in C.
Line 100:
 
Original:
<sourcesyntaxhighlight lang="c">
printf("hello, world\n");
</syntaxhighlight>
</source>
Modify to:
<sourcesyntaxhighlight lang="c">
printf("%s", "hello, world\n");
</syntaxhighlight>
</source>
 
The '''"%s"''' must be included to avoid a [[Format string attack]]. Please, discuss... [[Special:Contributions/83.55.41.191|83.55.41.191]] ([[User talk:83.55.41.191|talk]]) 21:38, 1 January 2010 (UTC)
Line 249:
::"Consensus" is such a useful word! Simply mentioning it is sufficient to convince everyone that things should stay as they are. This even works when no real consensus exists, as can be seen by the regularly occuring discussions (remember that [[WP:VOTE|Wikipedia decisions are not made by popular vote]]). If anything, WP-wide there is a consensus that syntax highlighting ''should in fact be used'', as can be seen in every article about a programming language for which syntax highlighting is available.
::Since those discussions didn't lead to a consensus in the past, and since no one seems to be able to have the colors defined by the MediaWiki software changed, I propose another solution:
<sourcesyntaxhighlight lang="cpp">
long int SomeFunction();
/* int */ OtherFunction();
Line 265:
 
return test2;
}</sourcesyntaxhighlight>
::Since C is mostly a subset of C++, using C++ syntax highlighting works just fine, and it seems good enough for the C++ article. Opinions?
::On a related note: why was '''some_function ()''' changed to '''SomeFunction ()'''? This is rather unusual for C, so I suggest it should be changed back. – [[User:Adrianwn|Adrian Willenbücher]] ([[User talk:Adrianwn|talk]]) 06:33, 13 October 2010 (UTC)
Line 272:
::::That's a pretty weak argument for the following reasons:
:::::1) Highlighting the syntax doesn't pretend anything. After all, you could say the same about indentation (indentation is not part of the language definition), so the example should look like this in order to be not "dishonest":
<sourcesyntaxhighlight lang="text">
long int SomeFunction();
/* int */ OtherFunction();
Line 288:
 
return test2;
}</sourcesyntaxhighlight>
:::::Actually, those line breaks don't have any meaning for the program either, so they should be removed as well, together with redundant whitespace:
<sourcesyntaxhighlight lang="text">long int SomeFunction();/* int */OtherFunction();/* int */CallingFunction(){long int test1;register /* int */ test2;test1 = SomeFunction();if (test1 > 0)test2 = 0;elsetest2 = OtherFunction();return test2;}</sourcesyntaxhighlight>
:::::There, now we have removed everything which doesn't influence the semantics of the program. What a clear, informative example!
:::::2) As was mentioned before, just about every other programming language article uses syntax highlighting. Are you saying that those should be changed, too? If not, why is C different? – [[User:Adrianwn|Adrian Willenbücher]] ([[User talk:Adrianwn|talk]]) 14:16, 13 October 2010 (UTC)