User Tools

Site Tools


cs_lang:c

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
cs_lang:c [2011/07/12 10:25] – created cedriccs_lang:c [2011/07/12 10:47] – [Equivalent in python] cedric
Line 1: Line 1:
 +====== Learning C ======
  
 +
 +  * [[http://www.librarything.fr/work/3600740/book/62075147 | The C Programming Language]].
 +
 +
 +====== Simple examples ======
 +===== Hello World =====
 <code c> <code c>
 /* Hello World program */ /* Hello World program */
Line 11: Line 18:
  
 } }
 +</code>
 +
 +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.out 
 +Hello World
 +</code>
 +
 +
 +
 +===== Read a file =====
 +<code c>
 +#include <stdio.h>   /* required for file operations */
 +
 +FILE *fr;            /* declare the file pointer */
 +
 +main()
 +
 +{
 +   char line[80];
 +
 +   fr = fopen ("./file.txt", "rt");  /* open the file for reading */
 +   /* "rt" means open the file for reading text */
 +
 +   while(fgets(line, 1024, fr) != NULL)
 +   {
 +         printf ("%s\n", line);
 +   }
 +   fclose(fr);  /* close the file prior to exiting the routine */
 +}
 +</code>
 +
 +==== Equivalent in python ====
 +<code python>
 +for line in open("./file.txt", "r"):
 +    print line
 </code> </code>
cs_lang/c.txt · Last modified: 2012/09/30 23:32 by cedric