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; } |
for (initial value; condition ; counter increment) statement; |
#include <stdio.h> int main(){ int i; for (i=0; i< 10; i++) printf("i=%d\n",i); return 0; } |
|
#include <stdio.h> int main() { int i, s = 0; for (i=0; i<= 100; i++) s = s + i; printf("Sum = %d\n", s); return 0; } |
sum = 0.0; for (i=0; i<= 100; i++) sum = sum + function(i); |
|
#include <stdio.h> int main() { int i=0; while (i<10) i++; printf("%d\n",i); return 0; } |
#include <stdio.h> int main() { int i=0; while (i<10) {i++; printf("i = %d\n",i); } printf("%d\n",i); return 0; } |
#include <stdio.h> int main() { int input_value; do { printf("Enter 1 for yes, 0 for no :"); scanf("%d", &input_value); } while (input_value != 1 && input_value != 0); return 0; } |
#include <stdio.h> int main() { int i; printf("Enter an integer="); scanf("%d", &i); switch(i){ case 1: printf("a is 1\n");break; case 2: printf("a is 2\n");break; default: printf("a is neither 1 nor 2\n");break; } return 0; } |
#include <stdio.h> int main() { int a = 1; switch(a){ case 1: case 3: case 4: printf("a is 1, 3 or 4.\n"); break; case 2: printf("a is 2.\n"); break; } return 0; } |
Wrong | Correct |
if (a=0) printf(" Not valid !\n"); | if (a==0) printf(" illegal !\n"); |
int a=10,b=20; float c; c=a/b; | float a=10, b=20, c; c=a/b; |
gcc math1.c | gcc -lm math1.c |
scanf("%f", a) | scanf("%f", &a) |
for (i=0, i<10, i++) | for (i=0; i<10; i++) |
for (i==0, i<10, i++) | for (i=0; i<10; i++) |
|
#include <stdio.h> #include <math.h> int main() { int i,n; float sum=0.0; printf("Enter # of iterations = "); scanf("%d", &n); for (i=1;i<n;i++) sum = sum + pow(-1, i+1)/i; printf("Approximation of ln(2)= %f. ", sum); printf("Exact value of ln(2)= %f.\n", log(2)); return 0; } |
#include <stdio.h> void write() { printf("Hello world !\n"); } int main() { write(); return 0; } |
#include <stdio.h> float cube(float x) { return x*x*x; } int main() { float x; printf("Enter x = "); scanf("%f",&x); printf("The cube of x is %f.\n", cube(x)); return 0; } |