#include <stdio.h> int main() { printf("\" is a double quotation character.\n"); return 0; } |
#include <stdio.h> int main() { float a=3.14; printf("%f\n",a); printf("%10f\n",a); printf("%20f\n",a); printf("%30f\n",a); printf("%10.3f\n",a); printf("%10.4f\n",a); printf("%10.5f\n",a); printf("%10.6f\n",a); return 0; } |
#include <stdio.h> int main() { float a, b; printf("%f\n", a=20.0); b=a=30.0; printf("%d\n", a==20.0); return 0; } |
a=b=20; |
|
#include <stdio.h> int main() { int a=3, b=5; printf("%f\n", a/b); return 0; } |
#include <stdio.h> int main() { int a=3, b=5; printf("%f\n", 1.0*a/b); return 0; } |
#include <stdio.h> int main() { int a=3, b=5; printf("%f\n", a/b*1.0); return 0; } |
#include <stdio.h> int main() { int a=3, b=5; printf("%f\n", (float)a/b); return 0; } |
$ a.out | more
$ gcc -lm myprogram.c -o myprogram |
#include <stdio.h> #define PI 3.141592 /* define pi */ int main() { float a; printf("Enter radius = "); scanf("%f", &a); printf("The area of circle is=%f \n", a*a*PI); return 0; } |
x[0] | x[1] | x[2] | x[3] | x[4] | x[5] |
45 | 67 | 12 | 34 | 25 | 39 |
x[0] | x[1] | x[2] | x[3] | x[4] | x[5] |
45 | 67 | 12 | 34 | 25 | 39 |
x[0] | x[1] | x[2] | x[3] | x[4] | x[5] |
45 | 12 | 67 | 34 | 25 | 39 |
x[0] | x[1] | x[2] | x[3] | x[4] | x[5] |
45 | 12 | 34 | 67 | 25 | 39 |
x[0] | x[1] | x[2] | x[3] | x[4] | x[5] |
45 | 12 | 34 | 25 | 67 | 39 |
x[0] | x[1] | x[2] | x[3] | x[4] | x[5] |
45 | 12 | 34 | 25 | 39 | 67 |
#define N 6 for (i=0; i < N-1; i++) if (x[i] > x[i+1]) swap(&x[i], &x[i+1]) |
#include <stdio.h> #define N 6 void swap(float *a, float *b) { float tmp; tmp=*a; *a=*b; *b=tmp; } int main() { float a[N]={45, 67, 12, 34, 25, 39}; int i, j; for (j=1; j < N; j++) {for (i=0; i< N-j; i++) if ( a[i]> a[i+1]) swap(&a[i], &a[i+1]); } for (i=0;i<N; i++) printf("%f ", a[i]); printf("\n"); return 0; } |
|
|
|
|
|
|
"ABC" (double quotation) is for a string of characters and is an array (pointer).
|
#include <stdio.h> int main() { char s[3] = "ABC"; printf("%s\n", s); return 0; } |
#include <stdio.h> int main() { char str[100]; printf("Enter string = "); scanf("%s", str); printf("%s\n",str); return 0; } |
#include <stdio.h> #include <string.h> int main() { char c1[] = "ABCDE", c2[6]; strcpy(c2,c1); printf("%s\n",c2); return 0; } |
#include <stdio.h> #include <string.h> int main() { char s[100]; printf("Enter \"MAE2360\" = "); scanf("%s", s); if (strcmp(s,"MAE2360") == 0) printf("MAE2360 was entered correctly.\n"); else printf("Wrong. %s was entered.\n",s); return 0; } |
#include <stdio.h> #include <string.h> int main() { char c[50]; printf("Enter string = "); scanf("%s", c); printf("You entered %s\n", c); printf("Its length is %d\n", strlen(c)); return 0; } |
Notion | Meaning |
$ a.out > filename | output to file |
$ a.out >> filename | append output to file |
$ a.out < filename | get input from file |
$ gcc myprogram.c $ a.out > result.dat $ more result.dat |
$ gcc myprogram.c $ a.out < data.dat > result.dat $ more result.dat |
c:\dir > filelist.dat |
#include <stdio.h> int main() { FILE *fp; fp = fopen("filename","w"); /* write something on fp */ fclose(fp); return 0; } |
#include <stdio.h> int main() { FILE *fp; fp=fopen("junk.dat","w"); fprintf(fp,"Hello!\n"); fclose(fp); return 0; } |
#include <stdio.h> int main() { FILE *fp; float a,b,c; fp=fopen("junk.dat","r"); fscanf(fp,"%f %f %f", &a, &b, &c); printf("%f %f %f", a,b,c); fclose(fp); return 0; } |
#include <stdio.h> int main() { FILE *fp1, *fp2; float a,b,c; fp1=fopen("junk1.dat","w"); fp2=fopen("junk2.dat","w"); fprintf(fp1,"This is the first file.\n"); fprintf(fp2,"This is the second file.\n"); fclose(fp1); fclose(fp2); return 0; } |
$ gcc -lm add.c -o add $ junk (interactive session) |
$ gcc -lm add.c -o add $ junk 3421 8756 (executes program to manipulate 3421 and 8756 and prints results) |
int main(int argc, char *argv[]) |
#include <stdio.h> int main(int argc, char *argv[]) { int i; printf("Number of parameters = %d\n", argc); for(i=0; i<argc; i++) printf("%d: %s\n", i, argv[i]); return 0; } |
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { printf("%d\n", atoi(argv[1])); return 0; } |
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int i; int b[100]; /* reserve ample space*/ for (i=1; i< argc; i++) b[i]=atoi(argv[i]); for (i=1; i< argc ; i++) printf("%d\n", b[i]); return 0; } |