C language is easy to get started, because there are a few commonly used Syntax structures. Below are some simple program examples of these syntax structures, you can quickly review the C language. The program contains the use of bool variables, characters, constants, mathematical functions, special characters, for loops, and functions.
1. Use the bool variable
# Include <stdio. h> # include <stdbool. h> int main (void) {float input; bool isTrue = (scanf ("% f", & input) = 1); while (isTrue) {printf ("you typed %. d \ n ", (int) input); // forced type conversion isTrue = (scanf (" % f ", & input) = 1);} return 0 ;}
2. Character
# Include <stdio. h> # include <stdlib. h> int main () {char beep; // equivalent to int beep; while (scanf ("% c", & beep) = 1) {printf ("you typed a % c \ n", beep);} // scanf will read the carriage return into the variable together, and the final result cannot meet expectations! The following method can be used: return 0 ;}
#include <stdio.h>#include <stdlib.h>int main(){ char beep; while(gets(&beep)){ printf("you typed a %c \n", beep); } return 0;}
3. Constants
# Include <stdio. h> # include <stdlib. h> # include <limits. h> # include <float. h> # define PI 3.1416 # define TEXT "hello world" # define HTML 'H' int main () {// constant definition and C Preprocessor system predefined constant const int china = 345; printf ("% d, % d \ n", INT_MAX, china, FLT_MAX_10_EXP, CHAR_MIN );}
4. mathematical functions
# Include <stdio. h> # include <math. h> # define PI 3.1415926 void main (void) {// The row where I is, draw the cosine curve int I, j, blankNUm; float cosValue; for (I = 0; I <21; I ++) {cosValue = 1-i/10.0; blankNUm = (int) (180/5/PI * acos (cosValue); for (j = 0; j <blankNUm; j ++) printf (""); printf ("*"); for (j = 0; j <(73-2-2 * blankNUm ); j ++) printf (""); I! = 20? Printf ("* \ n"): printf ("");} return 0 ;}
# Include <stdio. h> int main (void) {// calculate e, the first 50 items. In fact, the first 10 results are the same, just for demonstration. Using double can meet the precision requirement of int I; double factorial = 1.0, e = 0; for (I = 1; I <= 50; I ++) {factorial * = I; e + = 1.0/factorial ;} printf ("% f \ n", e); return 0 ;}
5. Special characters
# Include <stdio. h> # include <stdlib. h> int main () {float salary; printf ("\ aplease enter you salary by month:"); // \ a ring age, looks like printf ("$ ______ \ B"); // \ B returns if (scanf ("% f", & salary) = 1) {printf ("\ t $ %. 2f per manth is $ %. 2f per year ", salary, salary * 12.0); printf (" \ rgEE! Wa h \ n "); // \ r move the cursor to the starting position of the current row. This is very subtle.} return 0;}/* please enter you salary by month: $23.4 _ * // * gEE! Wa h $23.40 per manth is $280.80 per year */
6. for Loop
#include <stdio.h>#include <stdlib.h>int main(void){ int k; for(k=1,printf("%d:hi \n",k);printf("k =%d \n",k),k*k<26; k+=2,printf("now k =%d\n",k)) { printf("k is %d in the loop \n",k); } return 0;}#include <stdio.h>#include <stdlib.h>int main(void){ int i; int word[8]; for(i=0;i<8;i++){ word[i] =pow(2,i); } i =0; do{ printf("%d ",word[i]); }while(++i<8); return 0;}#include <stdio.h>#include <stdlib.h>int main(void){ int i; double arr1[8],arr2[8]; for(i=0;i<8;i++){ scanf("%lf",&arr1[i]); } arr2[0] =arr1[0]; for(i=1;i<8;i++){ arr2[i] =arr2[i-1] +arr1[i]; } for(i=0;i<8;i++){ printf("%.2f\t",arr1[i]); } printf("\n"); for(i=0;i<8;i++){ printf("%.2f\t",arr2[i]); } return 0;}#include <stdio.h>#include <stdlib.h>int main(void){ int i,max; while(scanf("%d",&max)==1){ double sum1=0,sum2=0; for(i=1;i<=max;i++){ sum1+=1.00/i; i%2==0 ?(sum2-=1.0/i):(sum2+=1.0/i); } printf("sum1= %lf sum2= %lf\n",sum1,sum2); } return 0;}
7. Functions
#include <stdio.h>#include <stdlib.h>int square(int s); void echo(void);int main(){ int i; for(i=0;i<10;i++) { printf("squire %d's value is%d\n",i,square(i)); } printf("what\nNo/nBonzo?\n"); echo(); return 0;}int square(int s){ return s*s;}void echo(void){ printf("a function without input");}/* another */#include <stdio.h>#include <stdlib.h>void one_three(void);void two(void);int main(){ printf("start now:\n"); one_three(); printf("done!"); return 0;}void one_three(void){ printf("one\n"); two(); printf("three\n");}void two(void){ printf("two\n");}