C: Difference between revisions

From XPUB & Lens-Based wiki
 
(26 intermediate revisions by the same user not shown)
Line 1: Line 1:
... a programming language to follow [[wikipedia:B (programming language)|B]]. [[wikipedia:C (programming language)|C]] is the core language of Unix and later GNU/Linux and the liberation of it's compiler software, ''[[wikipedia:Gnu Compiler Collection|gcc]]'' (or the Gnu's Not Unix C Compiler), a foundation of the Free Software movement.
... a programming language to follow [[wikipedia:B (programming language)|B]].
 
[[wikipedia:C (programming language)|C]] is the core language of Unix and later GNU/Linux and the liberation of it's compiler software, ''[[wikipedia:Gnu Compiler Collection|gcc]]'' (the Gnu's alternative to the proprietary Unix C compiler (''cc'')), a foundation of the Free Software movement.
 
== Key differences from Python ==
 
For those coming to C from [[Python]], there are several important differences:
* All variables have a fixed type (int, char, etc) and must be declared before use
* Whitespace (spaces, tabs, newlines) does ''not'' generally matter in C. Blocks (like for loops, if statements) are always indicated with curly braces {}. Indentation is still recommended for readability however.
* Source must be compiled first before executing.


== Variables ==
== Variables ==
Line 5: Line 14:
Variables in C are ''strictly typed'' meaning they always are one particular kind of representation of information (an integer number, a character, a string of text).
Variables in C are ''strictly typed'' meaning they always are one particular kind of representation of information (an integer number, a character, a string of text).


* int
* char
* char
* int
 
* float
=== int ===
* double
 
''int'''s or integers are for whole numbers (no fractional parts).


<source lang="c">
<source lang="c">
Line 24: Line 35:
=== Strings ===
=== Strings ===


Strings in C are ''arrays'' of characters. Abstractly a string in C is simply a pointer; that is, a numeric memory location pointing to the first character of the text in the memory.
Strings in C are ''arrays'' of characters.
 
<source lang="c">
<source lang="c">
char text[] = "pioneering jazz electronic organ recordings";
char text[] = "pioneering jazz electronic organ recordings";
int textlen = strlen(text);
printf("%s id %d chars long, and starts with %c\n", text, textlen, text[0]);
</source>
Abstractly a string in C is simply a pointer; that is, a numeric memory location pointing to the first character of the text in the memory.


int textlen = strlen(text);
== Loops ==
for (int i=0; i<textlen; i++) {
 
     printf("%d\n", i);
=== while ===
while (''expression'')
    ''statement''
 
real example:
 
<source lang="c">
var i=0;
while (i<100) {
     printf("%d", i);
    i = i+1;
}
}
</source>
</source>


== Loops ==
=== for ===
 
for (''expr1''; ''expr2''; ''expr3'')
    ''statement''
 
''for'' is actually just a compact form and is the same as saying:
 
''expr1'';
while (''expr2'') {
  ''statement''
  expr3;
}


Like, [[Bash]], C has a ''for'' loop:
A simple counting example:


<source lang="c">
<source lang="c">
for (int i=0; i<10; i++) {
int i;
     printf("Hello %d\n" % i);
for (i=0; i<10; i++) {
     printf("Hello %d\n", i);
}
}
</source>
</source>
== If-Else ==
if (''expression'')
    ''statement''
else
    ''statement''
Else-if allows for more than one possibility:
if (''expression'')
    ''statement''
else if (''expression'')
    ''statement''
else if (''expression'')
    ''statement''
else
    ''statement''

Latest revision as of 11:06, 20 October 2010

... a programming language to follow B.

C is the core language of Unix and later GNU/Linux and the liberation of it's compiler software, gcc (the Gnu's alternative to the proprietary Unix C compiler (cc)), a foundation of the Free Software movement.

Key differences from Python

For those coming to C from Python, there are several important differences:

  • All variables have a fixed type (int, char, etc) and must be declared before use
  • Whitespace (spaces, tabs, newlines) does not generally matter in C. Blocks (like for loops, if statements) are always indicated with curly braces {}. Indentation is still recommended for readability however.
  • Source must be compiled first before executing.

Variables

Variables in C are strictly typed meaning they always are one particular kind of representation of information (an integer number, a character, a string of text).

  • int
  • char

int

int's or integers are for whole numbers (no fractional parts).

int i = 0;
printf("%d\n", i);

(short and long are "qualifiers" that then can be used before the word in as in:

short int foo;
long int bar;

In these cases the word int can be left out.)

Strings

Strings in C are arrays of characters.

char text[] = "pioneering jazz electronic organ recordings";
int textlen = strlen(text);
printf("%s id %d chars long, and starts with %c\n", text, textlen, text[0]);

Abstractly a string in C is simply a pointer; that is, a numeric memory location pointing to the first character of the text in the memory.

Loops

while

while (expression)
    statement

real example:

var i=0;
while (i<100) {
    printf("%d", i);
    i = i+1;
}

for

for (expr1; expr2; expr3)
    statement

for is actually just a compact form and is the same as saying:

expr1;
while (expr2) {
  statement
  expr3;
}

A simple counting example:

int i;
for (i=0; i<10; i++) {
    printf("Hello %d\n", i);
}

If-Else

if (expression)
    statement
else
    statement

Else-if allows for more than one possibility:

if (expression)
    statement
else if (expression)
    statement
else if (expression)
    statement
else
    statement