Contents
- .
- ./arithmetic.c
- ./arrays.c
- ./datatypes.c
- ./ForFunzies.java
- ./hello.c
- ./index.html
- ./strings.c
. 1/8
[top][prev][next]
./arithmetic.c 2/8
[top][prev][next]
/* A review of the basic arithmetic operators in C. */
#include <stdio.h>
int main() {
int x,y;
int r1,r2,r3,r4,r5;
double d1;
x=4;
y=7;
r1=x+y;
r2=x-y;
r3=x/y;
d1 = 1/2;
r4=x*y;
printf("r1 r2 r3 r4: %d %d %d %d\n",r1,r2,r3,r4);
printf("d1: %f\n", r3, d1);
r3++;
r4--;
r5=r4%r1;
printf("r3 r4 r5: %d %d %d\n",r3,r4,r5);
// WARNING:
// If your data is an integer and you format it as
// a float, chaos will ensue.
printf("%f %f %f\n", 1 / 2, 1.0 / 2, 3.4/2);
}
./arrays.c 3/8
[top][prev][next]
#include <stdio.h>
main() {
int a[3];
int b[2];
a[0] = 1;
a[1] = -1;
a[2] = 2;
b[0] = 4;
printf("%d %d %d\n", a[0], a[1], a[2]);
printf("%d\n", a[3]);
printf("%d\n", a[4]);
printf("%d\n", a[5]);
}
./datatypes.c 4/8
[top][prev][next]
/* A review of the basic data types in C. */
#include <stdio.h>
int main() {
int x,y;
char a;
float f,e;
double d;
x=4;
y=7;
a='H';
f=-3.4;
d=54.123456789;
e=54.123456789;
printf("%d %c %f %lf\n",x,a,e,d);
printf("%d %c %.9f %.9lf\n",x,a,e,d);
}
./ForFunzies.java 5/8
[top][prev][next]
/**
* Comparing how printf works in Java to how it works in C.
*/
public class ForFunzies {
public static void main(String args[]) {
/*
Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.Integer
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302)
at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2806)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2753)
at java.util.Formatter.format(Formatter.java:2520)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at ForFunzies.main(ForFunzies.java:7)
*/
// When the following is uncommented, you get the above error message.
//System.out.printf("%f %f %f\n", 1 / 2, 1.0 / 2, 3.4/2);
System.out.printf("%d %f %f\n", 1 / 2, 1.0 / 2, 3.4/2);
}
}
./hello.c 6/8
[top][prev][next]
#include <stdio.h>
main() {
printf("Hello World, and Goodbye!\n");
}
./index.html 7/8
[top][prev][next]
<html>
<head><title>Examples for /home/faculty/sprenkle/public_html/cs330/examples/03-c</title>
<link rel="stylesheet" type="text/css" href="http://www.cs.wlu.edu/~sprenkle/cs330/assignments/assign.css" />
</head>
<body>
<h1>Examples for /home/faculty/sprenkle/public_html/cs330/examples/03-c</h1>
<ul>
<li><a href=".//code.html">All IN ONE FILE (pretty syntax)</a>
<li><a href=".//ForFunzies.java">ForFunzies.java</a></li>
<li><a href=".//arithmetic.c">arithmetic.c</a></li>
<li><a href=".//arrays.c">arrays.c</a></li>
<li><a href=".//datatypes.c">datatypes.c</a></li>
<li><a href=".//hello.c">hello.c</a></li>
<li><a href=".//strings.c">strings.c</a></li>
</ul>
</body>
./strings.c 8/8
[top][prev][next]
/*
* Removes the 'a's from a string
*/
#include <stdio.h>
#include <string.h>
main() {
int i, j;
char s[6];
s[0] = 'a';
s[1] = 'b';
s[2] = 'a';
s[3] = 'c';
s[4] = 1;
i = 0;
j = 0;
while (s[i] != 0) {
if (s[i] != 'a') {
s[j] = s[i];
j++;
}
i++;
}
s[j] = 0; // alternatively, s[j] = '\0'; character has value 0
printf("%s\n", s);
}
Generated by GNU Enscript 1.6.6.