cs_lang:c
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
cs_lang:c [2011/07/12 10:25] – created cedric | cs_lang:c [2011/07/12 10:37] – [Hello World] cedric | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Learning C ====== | ||
+ | |||
+ | * [[http:// | ||
+ | |||
+ | |||
+ | ====== Simple examples ====== | ||
+ | ===== Hello World ===== | ||
<code c> | <code c> | ||
/* Hello World program */ | /* Hello World program */ | ||
Line 11: | Line 18: | ||
} | } | ||
+ | </ | ||
+ | |||
+ | 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 | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ===== Read a file ===== | ||
+ | <code c> | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int main () | ||
+ | { | ||
+ | // Open the file | ||
+ | ofstream inputFile (" | ||
+ | // Test | ||
+ | if (!inputFile) | ||
+ | { | ||
+ | cerr << " | ||
+ | exit (1); | ||
+ | } | ||
+ | char buf [1024]; | ||
+ | // Until there are no more lines in the file, | ||
+ | // read and display it. | ||
+ | while (!inputFile.eof()) | ||
+ | { | ||
+ | inputFile.getline (buf, | ||
+ | cout << buf << endl;; | ||
+ | } | ||
+ | // Close the file | ||
+ | inputFile.close ();; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Equivalent in python ===== | ||
+ | <code python> | ||
+ | for line in open(" | ||
+ | print line | ||
</ | </ |
cs_lang/c.txt · Last modified: 2012/09/30 23:32 by cedric