name:
example.c
from the examples
directory of user jill27
to your current working directory.
cp ~jill27/examples/example.c .
#include <stdio.h> #define MAX 21 main () { FILE *infile; char word[MAX]; infile = fopen("in.dat", "r"); fscanf(infile, "%s", word); printf("%s", word); fclose(infile); }
If the file in.dat
does not exist.
If the user types in a filename that is more than 20 characters in length.
a.out
in such a way that it treats the contents of the file in.dat
as if they
were being typed in from the keyboard.
a.out < in.dat
#include <stdio.h> #define SSIZE 81 main () { char sentence[SSIZE]; printf("Enter a sentence : "); /* method one */ gets(sentence); /* method two */ fgets(sentence, SSIZE, stdin); }
Using fgets
is preferred because it won't write outside the
bounds of the string sentence
if the user types in a sentence
of more than 80 characters.
#include <stdio.h> void inc2(int i, int *j) { i++; *j++; } main () { int a = 5, b = 10; inc2(a, &b); printf("a: %d and b: %d.\n", a, b); }
This program prints:
a: 5 and b: 11.
int *arr; arr = (int *) malloc(10 * sizeof(int)); arr[3] = 5;Why is it legal to refer to
arr
as if it were declared as an array?
Because the C compiler represents arrays internally as a pointer of type
address of the base type of the array. So, this declaration of arr
is the same as declaring arr
as an array of ints.
int *ptr; ptr = (int *) malloc(sizeof(int));Why is it necessary to put
(int *)
before the call to malloc
?
malloc()
returns a pointer to void
, so this result
must be cast (converted) to a pointer to int
to be assigned
to ptr
so that the types match.
#define ROWS 5 #define COLS 10 typedef int table[ROWS][COLS];Write a C function that takes a parameter of type
table
and returns
the sum of all of the integers contained in the table.
int sum(table t) { /* return the sum of all integers in the table */ int tsum = 0; int row, col; for (row = 0; row < ROWS; row++) { for (col = 0; col < COLS; col++) { tsum += t[row][col]; } } return (tsum); }
#define SIZE 15 typedef struct { double x; double y; } point; typedef point pointlist[SIZE];
pointlist
and
initializes both fields of each struct in the parameter to be 0.0
.
void initpoints(pointlist p) { /* initialize an array of points */ int count; for (count = 0; count < SIZE; count++) { p[count].x = 0.0; p[count].y = 0.0; } }
pointlist
and
writes the contents of that parameter to file out.dat
, one point
per line in format: (x, y)
. That is, after your function has been
called, each line of out.dat
will contain the x
and y
fields of a point
in parenthesis, separated by a comma. You can assume
that
stdio.h
has been included and that your function will be able to open
file out.dat
correctly.
void tofile(pointlist p) { /* write the contents of an array of points to out.dat */ int count; FILE *fptr; fptr = fopen("out.dat", "w"); for (count = 0; count < SIZE; count++) { fprintf(fptr, "(%lf, %lf)\n", p[count].x, p[count].y); } }
void incpoint(point *apoint) { /* this function updates both fields of a point by incrementing both by 1.0 */ apoint.x += 1.0; apoint.y += 1.0; }
Parameter apoint
is an address of a struct, not a struct.
So, fields have to be accessed using ->
and not the
.
notation.
pointlist somepoints;Write the call to
scanf
that would be needed to read a new value into
the x
field of the third point
in somepoints
from the
keyboard.
scanf("%lf", &somepoints[2].x);
point apoint;What is the type of the expression:
apoint.x + apoint.y * 3;
Its type is double.
stdio.h
) to use library functions in C?
Including the header file shows the compiler prototypes for the functions in the library. If the compiler hasn't seen the prototype, it can't check that the function is being called correctly.
ls | more
#include <stdio.h> void inc(void); main() { int x = 7; inc(); printf("x is: %d\n", x); } void inc(void) { int x; x++; }
This program prints:
x is 7
#include <stdlib.h> main() { int i; int *ptr; ptr = &i; free((void *) ptr); }
Memory should only be returned using free()
if it was allocated
using malloc()
. This program could crash or the value of
i
could change unpredictably.
#include <stdio.h> #include <stdlib.h> main() { int i; int *ptr; ptr = &i; *ptr = 4; i = 3; printf("*ptr is: %d\n.", *ptr); }
This program prints:
*ptr is: 3.
arrayOfInts
after the following declaration.
int arrOfInts[10] = {0};
All 10 index positions in arrOfInts
contain 0.
Because a pointer set to NULL
can be used to represent the
empty list. It is difficult to represent an empty list with a list element.