name:
example.c
from the examples
directory of user jill27
to your current
working directory.
#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); }
(5 pts) In the following program, which method of reading a sentence from the keyboard is preferred and why?
#include <stdio.h> #define SSIZE 81 main () { char sentence[SSIZE]; printf("Enter a sentence : "); /* method one */ gets(sentence); /* method two */ fgets(sentence, SSIZE, stdin); }
#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); }
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?
int *ptr; ptr = (int *) malloc(sizeof(int));Why is it necessary to put
(int *)
before the call to malloc
?
#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.
#define SIZE 15 typedef struct { double x; double y; } point; typedef point pointlist[SIZE];
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; }
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.
point apoint;What is the type of the expression:
apoint.x + apoint.y * 3;
#include <stdio.h> void inc(void); main() { int x = 7; inc(); printf("x is: %d\n", x); } void inc(void) { int x; x++; }
#include <stdlib.h> main() { int i; int *ptr; ptr = &i; free((void *) ptr); }
#include <stdio.h> #include <stdlib.h> main() { int i; int *ptr; ptr = &i; *ptr = 4; i = 3; printf("*ptr is: %d\n.", *ptr); }
arrayOfInts
after the following declaration.
int arrOfInts[10] = {0};