/* Read in two integers and print their sum. */ #include <stdio.h> int main(void) { int a, b, sum; printf("Input two integers: "); scanf("%d%d", &a, &b); sum = a + b; printf("%d + %d = %d\n", a, b, sum); return 0; } |
/* Read in two integers and print their sum. */ #include <stdio.h> int main(void) { int a, b, sum; printf("Input two integers: "); scanf("%d%d", &a, &b); sum = a + b; printf("%d + %d = %d\n", a, b, sum); return 0; } |
/* Read in two integers and print their sum. */ #include <stdio.h> int main(void) { int a, b, sum; printf("Input two integers: "); scanf("%d%d", &a, &b); sum = a + b; printf("%d + %d = %d\n", a, b, sum); return 0; } |
/* Read in two integers and print their sum. */ #include <stdio.h> int main(void) { int a, b, sum; printf("Input two integers: "); scanf("%d%d", &a, &b); sum = a + b; printf("%d + %d = %d\n", a, b, sum); return 0; } |
/* Read in two integers and print their sum. */ #include <stdio.h> int main(void) { int a, b, sum; printf("Input two integers: "); scanf("%d%d", &a, &b); sum = a + b; printf("%d + %d = %d\n", a, b, sum); return 0; } |
/* Read in two integers and print their sum. */ #include <stdio.h> int main(void) { int a, b, sum; printf("Input two integers: "); scanf("%d%d", &a, &b); sum = a + b; printf("%d + %d = %d\n", a, b, sum); return 0; } |
/* * A comment can be written in this fashion * to set it off from the surrounding code. */ |
// This is a comment in C++. |
auto | do | goto | signed | unsigned |
break | double | if | sizeof | void |
case | else | int | static | volatile |
char | enum | long | struct | while |
const | extern | register | switch | |
continue | float | return | typedef | |
default | for | short | union |
tax = price * tax_rate |
0 0123 123456789000 /* too large? */ 0123 /* an octal integer */ -49 /* a constant expression */ 123.0 /* a floating constant */ |
"a string of text" "" /* the null string */ "a = b + c" /* nothing is executed */ "a string with double quotes \" within" "a string backslash \\ is in this string" |
a+b /* the expression a plus b */ a_b /* a 3-character identifier */ |
int a, b, c = 0; a = ++c; b = c++; printf("%d %d %d\n", a, b, c++); /* 1 1 2 is printed */ |
| <=> |
|
/* Some powers of 2 are printed. */ #include <stdio.h> int main(void) { int i = 0, power = 1; while (++i <= 10) printf("%6d", power *= 2); printf("\n"); return 0; } |
2 4 8 16 32 64 128 256 512 1024 |
#include <stdio.h> #include <stdlib.h> int main(void) { int i, n; printf("\n%s\n%s", "Some randomly distributed integers will be printed.", "How many do you want to see? "); scanf("%d", &n); for (i = 0; i < n; ++i) { if (i % 10 == 0) putchar('\n'); printf("%7d", rand()); } printf("\n\n"); return 0; } |
Some randomly distributed integers will be printed. How many do you want to see? 19 16838 5758 10113 17515 31051 5627 23010 7419 16212 4086 2749 12767 9084 12060 32225 17543 25089 21183 25137 |
#include <stdio.h> #include <stdlib.h> #include <time.h> #define NCALLS 1000000 /* number of fct calls */ #define NCOLS 10 /* number of columns */ #define NLINES 5 /* number of lines */ int main(void) { int i, val; long begin, diff, end; begin = time(NULL); srand(time(NULL)); printf("\nTIMING TEST: %d calls to rand()\n\n", NCALLS); for (i = 0; i < NCALLS; ++i) { val = rand(); if (i < NCOLS * NLINES) { if (i % NCOLS == 0) putchar('\n'); printf("%7d", val); } if (i == NCOLS * NLINES) printf("\n.......\n"); } end = time(NULL); diff = end - begin; printf("%s%ld\n%s%ld\n%s%ld\n%s%.10f\n\n", " end time: ", end, " begin time: ", begin, " elapsed time: ", diff, "time for each call: ", (double) diff / NCALLS); return 0; } |
TIMING TEST: 1000000 calls to rand() 14290 14842 22261 26349 1071 676 8285 21259 11015 15521 27374 23015 23493 22998 16951 19599 10507 9277 8695 19685 25234 2196 10584 26157 12484 24519 17900 8792 19995 25374 4712 10254 31287 14118 25891 11645 706 14689 19020 19428 32260 31917 26094 32734 4933 8309 31141 25199 27878 28313 ....... end time: 763870220 begin time: 763870218 elapsed time: 2 time for each call: 0.0000020000 |