#03 (01/24/2024)

Input/Output

printf("format",argument);
Examples:
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);
Compare the following two programs:
#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;
}

Operators for variables

Relational operators:
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");
Difference between one equal sign (=) and two equal signs (==).

Logical operators:

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 ....

Increment/decrement operators
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++;
is the same as
i=100;
i=i+1;
Substitution operators
Symbol Meaning
= a = b b is assigned to a.
+= a + = b a + b is assigned to a. Same as a=a+b;
−= a − = b ab is assigned to a. Same as a=ab;
*= 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;
So all of the following increment i by 1.
i=i+1
i+=1
i++
++i

The 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.

Control statements

if Statement

#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 Statement

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;
}
Here is a program to compute
S = 1 + 2 + 3 + 4 + 5 + 6 + …+ 100.
#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;
}
Try to memorize the following pattern:
sum = 0.0;
for (i=0; i<= 100; i++) sum = sum + function(i);
for
100

i=0 
f(i) = f(0) + f(1) + f(2) + …+ f(100).
Note: The iteration variable (i. j, k...) must be declared as int.

while Statement

Does the following program output 9 or 10 ?
#include <stdio.h>
int main()
{
int i=0;

while (i<10) i++;

printf("%d\n",i);
return 0;
}
For multiple statements, it is necessary to use a block with curly brackets ({ and }).
#include <stdio.h>
int main()
{
int i=0;

while (i<10) 
 {i++;
  printf("i = %d\n",i);
 }

printf("%d\n",i);
return 0;
}

do while Statement

The do while loop is similar to the while loop except that the test occurs at the end of the loop body.
#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;
}

switch Statement

A switch statement can branch out to different tasks.
#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;
}
Here is what happens when you forget break;.
#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;
}
Common mistake: Figure out why they are wrong !!
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++)

Example

By numerically summing up
1− 1

2
+ 1

3
1

4
+ 1

5
− …,
compute an approximate value of ln 2 ( = 0.693147...).

Programming

#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;
}
Note: pow(a,b) is a function found in math.h which returns ab.

Functions in C

If there is no value to be returned, use void instead of int or others.
#include <stdio.h>

void write()
{
 printf("Hello world !\n");
}

int main()
{
write();
return 0;
}
Otherwise, the type of the function you use must be declared.
#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;
}




File translated from TEX by TTH, version 4.03.
On 04 Feb 2024, 21:33.