cs_lang:c
                Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| cs_lang:c [2011/07/12 10:25] – created cedric | cs_lang:c [2012/09/30 23:32] (current) – cedric | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Learning C ====== | ||
| + | |||
| + |   * [[http:// | ||
| + | |||
| + | |||
| + | ====== Simple examples ====== | ||
| + | ===== Hello World ===== | ||
| <code c> | <code c> | ||
| /* Hello World program */ | /* Hello World program */ | ||
| Line 8: | Line 15: | ||
| { | { | ||
|     printf(" |     printf(" | ||
| + | } | ||
| + | </ | ||
| + | Compile and run the program: | ||
| + | <code bash> | ||
| + | $ gcc -Wall hello.c -o hello.out | ||
| + | hello.c:6: warning: return type defaults to ‘int’ | ||
| + | hello.c: In function ‘main’: | ||
| + | hello.c:10: warning: control reaches end of non-void function | ||
| + | $ ./ | ||
| + | Hello World | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Increment a number ===== | ||
| + | <code c> | ||
| + | #include " | ||
| + | |||
| + | char line[100]; | ||
| + | int    | ||
| + | |||
| + | int main() | ||
| + | { | ||
| + |     printf(" | ||
| + |     fgets(line, sizeof(line), | ||
| + |     sscanf(line, | ||
| + | |||
| + |     printf(" | ||
| + | |||
| + | return 0; | ||
| } | } | ||
| + | </ | ||
| + | |||
| + | <code bash> | ||
| + | $ gcc -Wall test1.c  | ||
| + | cedric@debian: | ||
| + | Enter a value:41 | ||
| + | Inc: 42 | ||
| + | </ | ||
| + | |||
| + | ==== Equivalent in python ==== | ||
| + | <code python> | ||
| + | >>> | ||
| + | ...     value = raw_input(" | ||
| + | ...     print " | ||
| + | ... | ||
| + | >>> | ||
| + | Enter a value:41 | ||
| + | Inc: 42 | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | ===== Read a file ===== | ||
| + | <code c> | ||
| + | #include < | ||
| + | |||
| + | FILE *fr; /* declare the file pointer */ | ||
| + | |||
| + | main() | ||
| + | { | ||
| + | char line[1024]; | ||
| + |    fr = fopen (" | ||
| + |    /* " | ||
| + | |||
| + |     | ||
| + | { | ||
| + |           | ||
| + | } | ||
| + |     | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Equivalent in python ==== | ||
| + | <code python> | ||
| + | for line in open(" | ||
| + | print line | ||
| </ | </ | ||
cs_lang/c.1310459151.txt.gz · Last modified:  by cedric
                
                