
|
|
#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 |
|
|
|
|
$ gcc -lm add.c -o add $ add (interactive session) |
$ gcc -lm add.c -o add2 $ add2 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;
}
|