#04 (01/29/2024)

Free C compiler (gcc) for Windows PCs

Extract all the files under the c:\gcc-2.95.2 directory.
Open a command (CMD) window by Win + R, then cmd.
cd to c:\gcc-2.95.2 and run mingw32.bat to add the path. If you want to run gcc from any folder, add the folder c:\gcc-2.95.2 to the PATH environmental variable (details in class).
cd c:\gcc-2.95.2
mingw32
gcc MyProgram.c
To compile a C++ program, issue
g++ MyProgram.cpp
To compile a FORTRAN program, issue
g77 MyProgram.f
This will produce an executable file, a.exe instead of a.out.
gcc for the Mac

Misc. notes

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;
}
Common mistakes: 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 30 Jan 2024, 15:36.