type name(type var) { code..... return value; }
int main() { return 0; } |
#include <stdio.h> int main() { printf("Hello world\n"); return 0; } |
#include <stdio.h> #include <math.h> int main() { float x, y; x = 6.28; y=sin(x); printf("Sine of %f is %f.\n", x, y); return 0; } |
$ gcc -lm MyProgram.c; a.out |
Type | Content | Format | Range | Example |
int | integers | %d | −2147483647 ∼ +2147483647 | 10 |
float | floating numbers | %f | ±2.9387e−39 ∼ ±1.7014e+38 | 3.14 |
double | double precision floating | %lf | 2−63 ∼ 2+63 | 3.14159265358979 |
char | characters | %c | ASCII code | 'a' |
/* Print a character */ #include <stdio.h> int main() { char a='h'; printf("%c\n",a); return 0; } |
/* Print an integer */ #include <stdio.h> int main() { int a=10; printf("%d\n",a); return 0; } |
/* Print a floating number */ #include <stdio.h> int main() { float a=10.5; printf("%f\n",a); return 0; } |
/* Print floating numbers */ #include <stdio.h> int main() { float a, b=9.0, c; a=10.0; c=-2.3; printf("a = %f\n",a); printf("c = %f\n",c); return 0; } |
printf("format",argument); |
printf("Hello world !\n"); printf("Two integers are %d and %d.\n",a,b); printf("Two floating numbers are %f and %f.\n",a,b); printf("Three floating numbers are %f, %f and %f.\n",a,b,c); |
#include <stdio.h> int main() { int a, b; printf("Enter two integers separated by a comma = "); scanf("%d, %d",&a, &b); printf("a=%d b=%d\n", a, b); return 0; } |
#include <stdio.h> int main() { int a; float b; printf("Enter an integer and a real number separated by a space = "); scanf("%d %f",&a, &b); printf("a=%d b=%f\n", a, b); return 0; } |
Symbol | Meaning |
< | a < b ; a is less than b. |
< = | a < = b ; a is less than or equal to b. |
> | a > b ; a is greater than b. |
> = | a > = b; a is greater than or equal to b. |
== | a = = b ; a is equal to b. |
!= | a ! = b ; a is not equal to b. |
if (a==b) printf("a and b are equal.\n"); else printf("a and b are not equal.\n"); |
Symbol | Meaning |
&& | And |
|| | Or |
! | Not |
if (a>0 && a<100) printf("a is between 0 and 100.\n"); |
if (a>0 || a<-5) printf("a is positive or less than -5.\n"); |
int a=20; if (!(a==10)) printf("a is not equal to 10."); |
if (a==10) { printf("You entered 10.\n"); return 0; } else .... |
Symbol | Meaning | |
++ | b=++a | a is incremented by 1 and assigned to b. Same as a=a+1;b=a; |
b=a++ | a is assigned to b first and incremented by 1. Same as b=a;a=a+1; | |
− − | b=− −a | a is decremented by 1 and assigned to b. Same as a=a−1; b=a; |
b=a− − | a is assigned to b first and decremented by 1. Same as b=a;a=a−1; |
i=100; i++; |
i=100; i=i+1; |
Symbol | Meaning | |
= | a = b | b is assigned to a. |
+= | a + = b | a + b is assigned to a. Same as a=a+b; |
−= | a − = b | a − b is assigned to a. Same as a=a−b; |
*= | a * = b | a * b is assigned to a. Same as a=a*b; |
/= | a / = b | a / b is assigned to a. Same as a=a/b; |
%= | a % = b | Remainder of a / b is assigned to a. Same as a=a%b; |
i=i+1 i+=1 i++ ++iThe difference between ++i and i++ is that the former pre-increments i before any operation while the latter post-increments i after the operation is done.
#include <stdio.h> int main() { int i; printf("Enter an integer "); scanf("%d",&i); if (i>1 && i<100) printf("The number is between 1 and 100.\n"); else printf("The number is not in that range.\n"); return 0; } |