| (1) | _____ |
All functions in C must return a value. |
| (2) | _____ |
C only uses "call by value". |
| (3) | _____ |
Uninitialized pointers should never be set
to NULL.
|
char *school = "JMU"; school[0] = 'G';which is part of a file that contains everything that is needed for it to compile and link.
char school[] = "JMU"; school[0] = 'G';which is part of a file that contains everything that is needed for it to compile and link.
const char *school = "JMU"; school[0] = 'G';which is part of a file that contains everything that is needed for it to compile and link.
const char school[] = "JMU"; school[0] = 'G';which is part of a file that contains everything that is needed for it to compile and link.
const char *school = "JMU"; char *temp = school; temp[0] = 'G';which is part of a file that contains everything that is needed for it to compile and link.
char *s = "James Madison University";
printf("%s", s+6);
char *s = "James\0Madison University";
printf("%s", s);
char *s = "James\0Madison University";
printf("%s", s+6);
rectangle.h
struct rectangle
{
int x; // x-coordinate of the upper left vertex
int y; // y-coordinate of the upper left vertex
int width;
int height;
};
Indicate whether each of the following statements will
or will not compile (assuming that everything has been included properly).
rectangle r;
struct rectangle s;
struct rectangle t = {0, 0, 50, 100};
printf("%d\n", t.width);
struct rectangle *p;
p = &t;
printf("%d\n", p.height);
struct rectangle *p;
p = &t;
printf("%d\n", p->height);
#include <stdio.h>
int max(int a, int b)
{
if (a > b) return a;
else return b;
}
int min(int *a, int *b)
{
(*a)++;
(*b) = (*b) + 2;
if (*a < *b) return *a;
else return *b;
}
int
main(void)
{
int temp, x, y;
x = 3;
y = 7;
temp = max(x, y);
printf("max: %d \n", temp);
temp = min(&x, &y);
printf("min: %d \n", temp);
printf("x,y: %d,%d \n", x, y);
}
int n = 0;
int
main(void)
{
n += 100;
printf("%d\n", n);
return 0;
}
int n = 0;
int
main(void)
{
int n;
n += 5;
printf("%d\n", n);
return 0;
}
int n = 0;
int
main(void)
{
int n = 1;
n += 100;
printf("%d\n", n);
return 0;
}
int
total(int x)
{
static int total = 0;
total += x;
return total;
}
int
main(void)
{
total(5);
total(10);
printf("%d\n", total(15));
return 0;
}
int n = 0;
int
main(void)
{
extern int n;
n += 100;
printf("%d\n", n);
return 0;
}
int
add(int a, int b)
{
return a+b;
}
op
that could hold a pointer to the function add().
add() to op.
op
to add the values 5 and 6.
typdef to make the code more
readable.)
#include <stdio.h>
typedef int binop_t(int, int);
int
add(int a, int b)
{
return a+b;
}
int multiply(int a, int b)
{
return a*b;
}
int operate(binop_t *op, int a, int b)
{
return op(a, b);
}
int main(void)
{
binop_t *op = add;
printf("%d\n", op(5, 6));
printf("%d\n", operate(multiply, 5, 6));
return 0;
}
#include <stdio.h>
void
change(int *a, int length)
{
int i;
for (i=1; i<length; i++)
{
a[i] += *(a+i-1);
}
}
int
main(void)
{
int i;
int n = 4;
int value[4];
for (i=0; i<n; i++)
{
value[i] = 100 * i;
}
change(value, n);
for (i=0; i<n; i++)
{
printf("%d\n", value[i]);
}
}
total() function:
int total(int *a, int n)
{
int sum;
for (int i=0; i<n; i++) sum += a[i];
return sum;
}
what will be printed by the following? Write "UNKNOWN" if the answer is unknown (e.g., because it depends on the contents of a memory location and there is not enough information to know what is in that memory location).
int x[4] = {1,2,3,4};
cout << total(x,4) << "\n";
total() function:
int total(int *a, int n)
{
int sum;
sum = 0;
for (int i=0; i<n; i++) sum += *(a++);
return sum;
}
what will be printed by the following? Write "UNKNOWN" if the answer is unknown (e.g., because it depends on the contents of a memory location and there is not enough information to know what is in that memory location).
int x[4] = {1,2,3,4};
cout << total(x,4) << "\n";
total() function:
int total(int *a, int n)
{
int sum;
sum = 0;
for (int i=0; i<n; i++) sum += *(a+i);
return sum;
}
what will be printed by the following? Write "UNKNOWN" if the answer is unknown (e.g., because it depends on the contents of a memory location and there is not enough information to know what is in that memory location).
int x[4] = {1,2,3,4};
cout << total(x,4) << "\n";
total() function:
int total(int *a, int n)
{
int sum;
sum = 0;
for (int i=0; i<n; i++) sum += *(++a);
return sum;
}
what will be printed by the following? Write "UNKNOWN" if the answer is unknown (e.g., because it depends on the contents of a memory location and there is not enough information to know what is in that memory location).
int x[4] = {1,2,3,4};
printf("%d\n", total(x,4));
print_args.c
#include <stdio.h>
int
main(int argc, char **argv)
{
int i;
printf("argc: %d\n", argc);
for (i=0; i<argc; i++)
{
printf("argv[%d]: %s\n", i, argv[i]);
}
return 0;
}
and assuming that it is compiled and linked into an executable named
print_args, what will be output for each of the following?
./print_args
./print_args 2 9
./print_args 2+9
malloc().
/**
* Returns a substring of the given "string".
* The correct amount of memory required
* by the substring is allocated from the heap.
*
* @param s A pointer to the string
* @param start Index of the first char in the substring
* @param length Length of the substring
*/
char *substring(const char *s, int start, int length)
{
}
Copyright 2017