Contents
- .
- ./arithmetic.c
- ./arrays.c
- ./charbychar.c
- ./datatypes_and_print.c
- ./index.html
- ./strcmp_example.c
- ./string.c
- ./strlen_example.c
. 1/9
[top][prev][next]
./arithmetic.c 2/9
[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;
x = 4;
y = 7;
r1 = x + y;
r2 = x - y;
r3 = x / y;
r4 = x * y;
printf("%d %d %d %d\n", r1, r2, r3, r4);
r3++;
r4--;
r5 = r4 % r1;
printf("%d %d %d\n", r3, r4, r5);
}
./arrays.c 3/9
[top][prev][next]
/* A review of arrays in C. */
#include <stdio.h>
int 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[4]);
printf("%d\n", b[1]);
}
./charbychar.c 4/9
[top][prev][next]
/* What does this code do? */
#include <stdio.h>
#include <string.h>
int main() {
int i, j;
char s[6];
s[0] = 'a';
s[1] = 'b';
s[2] = 'a';
s[3] = 'c';
s[4] = '\0';
printf("%s\n", s);
i = 0;
j = 0;
while (s[i] != '\0') {
if (s[i] != 'a') {
s[j] = s[i];
j++;
}
i++;
}
s[j] = '\0';
printf("%s\n", s);
}
./datatypes_and_print.c 5/9
[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 %.3lf\n", x, a, e, d);
}
./index.html 6/9
[top][prev][next]
<html>
<head><title>Examples for /home/faculty/sprenkle/public_html/cs330/examples/02-c</title>
<link rel="stylesheet" type="text/css" href="http://www.cs.wlu.edu/~sprenkle/cs330/css/themes/spacelab.min.css" />
<link rel="stylesheet" type="text/css" href="http://www.cs.wlu.edu/~sprenkle/cs330/css/course.css" />
<link rel="stylesheet" type="text/css" href="http://www.cs.wlu.edu/~sprenkle/cs330/css/syntax.css" />
<link rel="stylesheet" type="text/css" href="http://www.cs.wlu.edu/~sprenkle/cs330/css/style.css" />
</head>
<body>
<h1>Examples for /home/faculty/sprenkle/public_html/cs330/examples/02-c</h1>
<ul>
<li><a href=".//code.html">All IN ONE FILE (pretty syntax)</a>
<li><a href=".//arithmetic.c">arithmetic.c</a></li>
<li><a href=".//arrays.c">arrays.c</a></li>
<li><a href=".//charbychar.c">charbychar.c</a></li>
<li><a href=".//datatypes_and_print.c">datatypes_and_print.c</a></li>
<li><a href=".//strcmp_example.c">strcmp_example.c</a></li>
<li><a href=".//string.c">string.c</a></li>
<li><a href=".//strlen_example.c">strlen_example.c</a></li>
</ul>
</body>
./strcmp_example.c 7/9
[top][prev][next]
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "abcd";
char str2[] = "abCd";
char str3[] = "abcd";
int result;
// comparing strings str1 and str2
result = strcmp(str1, str2);
printf("strcmp(str1, str2) = %d\n", result);
printf("!strcmp(str1, str2) = %d\n", !result);
// comparing strings str1 and str3
result = strcmp(str1, str3);
printf("strcmp(str1, str3) = %d\n", result);
printf("!strcmp(str1, str3) = %d\n", !result);
return 0;
}
./string.c 8/9
[top][prev][next]
/* A close look at a character array.
Run program multiple times, after running different programs.
*/
#include <stdio.h>
#include <string.h>
int main() {
char s[6];
int i;
printf("in the beginning, s is %s\n", s);
/* stresses the importance of making the end of the string explicit.
What does the data in the array look like to start? */
for( i = 0; i < 6; i++ ) {
printf("%c\n", s[i]);
}
s[0] = 'a';
s[1] = 'b';
printf("at the end, s is %s\n", s);
}
./strlen_example.c 9/9
[top][prev][next]
#include <stdio.h>
#include <string.h>
int main() {
char a[20]="Program";
char b[20]={'P','r','o','g','r','a','m','\0'};
char c[20];
int i;
printf("Enter a string: ");
fgets(c, 20, stdin);
printf("Length of string a = %d \n", strlen(a));
//calculates the length of string before null charcter.
printf("Length of string b = %d \n", strlen(b));
// why is the length one longer than you'd expect?
printf("Length of string c = %d \n", strlen(c));
i=0;
while( c[i] != '\0' ) {
printf("%i: %c\n", i, c[i]);
i++;
}
return 0;
}
Generated by GNU Enscript 1.6.6.