C: Difference between revisions
(→Loops) |
|||
Line 35: | Line 35: | ||
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. | 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 == | == Loops: while and for == | ||
while (''expression'') | |||
''statement'' | |||
for (''expr1'', ''expr2'', ''expr3'') | |||
''statement'' | |||
is in fact a compact form of a while loop and is the same as saying: | |||
''expr1''; | |||
while (''expr2'') { | |||
''statement'' | |||
expr3; | |||
} | |||
Simple counting example: | |||
<source lang="c"> | <source lang="c"> |
Revision as of 21:32, 18 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.
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).
- char
- int
- float
- double
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 and for
while (expression) statement
for (expr1, expr2, expr3) statement
is in fact a compact form of a while loop and is the same as saying:
expr1; while (expr2) { statement expr3; }
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