cplusplus:c_examples
This is an old revision of the document!
C Examples
from
Differences Between C and C++
by Robert Niemann (Century College)
11/22/2015
- example01.c
#include <stdio.h> int main() { int this_is_a_number; printf( "Please enter a number: " ); scanf( "%d", &this_is_a_number ); printf( "You entered %d", this_is_a_number ); getchar(); return 0; }
- example02.c
#include <stdio.h> main() { int dec = 5; char str[] = "abc"; char ch = 's'; float pi = 3.14; printf("%d %s %f %c\n", dec, str, pi, ch); }
- example03.c
#include <stdio.h> main() { char ch; int x; double y; printf("enter a character, an integer, and a double:\n"); scanf("%c %d %lf", &ch, &x, &y); printf("%c %d %lf\n", ch, x, y); }
- example04.c
/* Writing a text file. */ #include <stdio.h> int main() { FILE *ptr_file; int x; ptr_file =fopen("output.txt", "w"); if (!ptr_file) return 1; for (x=1; x<=10; x++) fprintf(ptr_file,"%d\n", x); fclose(ptr_file); return 0; }
- example05.c
/* Reading a text file. */ #include <stdio.h> int main() { FILE *ptr_file; char buf[1000]; ptr_file =fopen("input.txt","r"); if (!ptr_file) return 1; while (fgets(buf,1000, ptr_file)!=NULL) printf("%s",buf); fclose(ptr_file); return 0; }
cplusplus/c_examples.1461281479.txt.gz · Last modified: 2016/04/21 23:31 by mithat