cs_lang:c
This is an old revision of the document!
Table of Contents
Learning C
Simple examples
Hello World
/* Hello World program */ #include<stdio.h> main() { printf("Hello World"); }
Compile and run the program:
$ 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
Read a file
#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 */ }
Equivalent in python
for line in open("./file.txt", "r"): print line
cs_lang/c.1310460462.txt.gz · Last modified: 2011/07/12 10:47 by cedric