- Write a complete C program that displays:
Go Cougars!
- Given a file named cougars.c with source code, what is the command-line to compile it into an executable?
- Given the following files with source code:
main.c
support.c
what is the command-line to compile them into an executable using the two-stage compilation?
- Explain to your neighbor what #include does.
-
#include <stdio.h>
#define LOWER_LIMIT 0
#define UPPER_LIMIT 42
#define valid(x) ((x) > LOWER_LIMIT && (x) < UPPER_LIMIT)
int main( int argc, char* argv[]){
int ans = 41;
printf( "ans (before): %d\n", ans);
if( valid( ans++ ) ){
printf( "ans is between %d and %d\n", LOWER_LIMIT, UPPER_LIMIT);
}
printf( "ans (after): %d\n", ans);
return 0;
}
The code above compiles. Discuss with your neighbor why it produces the the following output:
ans (before): 41
ans (after): 43
Hint: Look at the output of gcc -E cExercises-preprocessor-simple.c
- Enumerate all of the C data types with your neighbor.
- What are possible data types for each of the following?
- 4567
- -4567
- 1234567
- 98.76
- -98.76
- 500.
- .8
- 10E5
- 6.02E23
- 1234.567E89
- 9E-300
- What will be the output of the following (if any)?
int solution;
solution++;
printf("solution: %d\n", solution);
-
Discuss with your neighbor what will the output be of the following (if any)?
#include <stdio.h>
int main(){
int i = 100000;
char c = '!';
c = i;
printf("i: %d, c: %d\n", i, c);
return 0;
}
- What is the value of the following expressions:
int age;
- age = 8
- -age
- 5 + 8
- 5 / 8
- 5 / 8.0
- (float) 7/3
- ((float) 7 ) / 3
- What will be the output of the following (if any)?
int i = 100000;
char c;
c = i;
printf("i: %d, c: %d\n", i, c);
- What will be the output of the following (if any)?
int answer = 42;
int* answerPtr = &answer;
printf("answer's value: %d\n", answer);
printf("answerPtr's value (base-10): %ld\n", answerPtr);
printf("answerPtr's value (hexadecimal): %p\n", answerPtr);
- Write code to sum the first 50 elements of an int array named receiptTotals.
-
Given the following code, determine the data type for each expression:
int x;
int* ptr;
Expression |
Data Type |
Possible Values |
x |
|
|
ptr |
|
|
*ptr |
|
|
&ptr |
|
|
&x |
|
|
- Diagram the elements on the stack and write the output of the following (adapted from HERE):
#include <stdio.h>
int main (){
int numbers[5];
int * p;
p = numbers; *p = 10;
p++; *p = 20;
p = &numbers[2]; *p = 30;
p = numbers + 3; *p = 40;
p = numbers; *(p+4) = 50;
for( int n = 0; n < 5; n++){
printf("%d, ", numbers[n]);
}
return 0;
}
- Write code for each of the following and make sure that it compiles:
- Declare a char named response as a pointer to a char
- Declare ex0 as a pointer to a char
- Declare ex1 as a pointer to a pointer to a char
- Declare ex2 as an array of 10 elements
- Declare ex3 as an array of 10 elements that are pointers to chars
- Declare ex4 as a pointer to an array of 30 char elements
- Declare ex5 as an array of 10 pointers to arrays of 500 char elements
- Declare ex7 as a pointer to const int
- Write a function ex8 that takes a char pointer named pc, increments the address that it points to and returns that value
- Write a function ex9 that takes an int pointer named num, and prints out what num points to and a message saying if what num points to is odd or even
- Add code as directed by the comments to pointersExercise.c and execute to see values update:
int main(){
int number = -1;
int* intPtr1;
int* intPtr2;
return 0;
}