Contents
- .
- ./arithmetic.c
- ./arrays.c
- ./array_walk.c
- ./cargs.c
- ./charbychar.c
- ./datatypes_and_print.c
- ./datatypes.c
- ./dynmem.c
- ./hello.c
- ./index.html
- ./sizeof.c
- ./strcmp_example.c
- ./string.c
- ./strings.c
- ./strlen_example.c
. 1/16
[top][prev][next]
./arithmetic.c 2/16
[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/16
[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]); // some compilers zero out the memory; others don't.
}
./array_walk.c 4/16
[top][prev][next]
#include <stdio.h>
int main() {
int iarray[100];
int n, num, status, sum, i;
int* iptr;
iptr = iarray;
n=0;
while( n < 100 ) {
// scanf returns the total number of bytes read
status = scanf("%d", &num);
if( status == 0 || num == 0 ) {
break;
}
*iptr++ = num;
n++;
}
for( iptr = iarray, sum=0; n > 0; n--) {
sum += *iptr++;
}
printf("sum = %d\n", sum);
}
./cargs.c 5/16
[top][prev][next]
/* Print out the command line arguments
* - they are an array of strings
*/
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
int i, j;
for (i = 0; i < argc; i++) {
j = 0;
while (argv[i][j] != '\0') {
printf("%c", argv[i][j]);
j++;
}
printf("\n");
}
for (i = 0; i < argc; i++)
printf("%s\n", argv[i]);
}
./charbychar.c 6/16
[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 7/16
[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);
}
./datatypes.c 8/16
[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);
}
./dynmem.c 9/16
[top][prev][next]
/* A review of the basic arithmetic operators in C. */
#include <stdio.h>
#include <stdlib.h>
int main() {
int* iptr = malloc(sizeof(int));
*iptr = 6;
printf("%d\n", *iptr);
}
./hello.c 10/16
[top][prev][next]
#include <stdio.h>
main() {
printf("Hello World, and Goodbye!\n");
}
./index.html 11/16
[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/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/03-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=".//array_walk.c">array_walk.c</a></li>
<li><a href=".//arrays.c">arrays.c</a></li>
<li><a href=".//cargs.c">cargs.c</a></li>
<li><a href=".//charbychar.c">charbychar.c</a></li>
<li><a href=".//datatypes.c">datatypes.c</a></li>
<li><a href=".//datatypes_and_print.c">datatypes_and_print.c</a></li>
<li><a href=".//dynmem.c">dynmem.c</a></li>
<li><a href=".//hello.c">hello.c</a></li>
<li><a href=".//sizeof.c">sizeof.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=".//strings.c">strings.c</a></li>
<li><a href=".//strlen_example.c">strlen_example.c</a></li>
</ul>
</body>
./sizeof.c 12/16
[top][prev][next]
#include <stdio.h>
int function(int x[]) {
return sizeof(x);
}
int main() {
int a[20];
printf("sizeof(int) = %d; sizeof(a) = %d\n", sizeof(int), sizeof(a));
printf("function returns %d\n", function(a));
}
./strcmp_example.c 13/16
[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 14/16
[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);
}
./strings.c 15/16
[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);
}
./strlen_example.c 16/16
[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.