cs_lang:c
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| cs_lang:c [2011/07/12 10:37] – [Hello World] cedric | cs_lang:c [2012/09/30 23:32] (current) – cedric | ||
|---|---|---|---|
| Line 15: | Line 15: | ||
| { | { | ||
| printf(" | printf(" | ||
| - | |||
| - | |||
| } | } | ||
| </ | </ | ||
| Line 29: | Line 27: | ||
| $ ./ | $ ./ | ||
| Hello World | 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 | ||
| </ | </ | ||
| Line 35: | Line 71: | ||
| ===== Read a file ===== | ===== Read a file ===== | ||
| <code c> | <code c> | ||
| - | #include <iostream.h> | + | #include <stdio.h> |
| - | #include <fstream.h> | + | |
| - | int main () | + | FILE *fr; /* declare the file pointer */ |
| + | |||
| + | main() | ||
| { | { | ||
| - | // Open the file | + | char line[1024]; |
| - | | + | fr = fopen (" |
| - | | + | /* "rt" |
| - | if (!inputFile) | + | |
| - | { | + | |
| - | cerr << | + | |
| - | exit (1); | + | printf |
| - | } | + | |
| - | char buf [1024]; | + | fclose(fr); |
| - | // Until there are no more lines in the file, | + | |
| - | // | + | |
| - | while (!inputFile.eof()) | + | |
| - | { | + | |
| - | | + | |
| - | cout << buf << endl;; | + | |
| - | | + | |
| - | // Close the file | + | |
| - | inputFile.close ();; | + | |
| } | } | ||
| </ | </ | ||
| - | ===== Equivalent in python | + | ==== Equivalent in python ==== |
| <code python> | <code python> | ||
| for line in open(" | for line in open(" | ||
| print line | print line | ||
| </ | </ | ||
cs_lang/c.1310459851.txt.gz · Last modified: by cedric
