Worksheet: C0
Worksheets are self-guided activities that reinforce lectures. They are not graded for accuracy, only for completion. Worksheets are due by Sunday night before the next lecture.
- Github Classroom Link: https://classroom.github.com/a/pijY0GkE
Questions
-
Write a program called
youshotme.cthat prints out the following message:You shot me! Ugh. Oh. Ow. Ow. . . . .-. (o o) | O \ \ \ `~~~'Add the program to your repo.
-
Compile the program above with
gcc.- What gcc command did you use?
- Did you have any errors? If so, note them and how you fixed them.
- If you had no errors, introduce one (like removing a
;) and note the error message.
-
The following program should not compile… fix it and explain why.
int main(){ printf("Why oh why does this program fail to compile?!?!\n"); } -
We were expecting the following output
They're not equal :-| They're not equal :-| They're not equal :-| They're not equal :-| They're not equal :-| They're equal :-) They're not equal :-| They're not equal :-| They're not equal :-| They're not equal :-|But it’s not working :( … Can you fix the program below to get it to work?
int main(){ int a = 10; int b = 5; while(a > 0){ if(a = b){ printf("They're equal :-) \n"); }else{ printf("They're not equal :-| \n"); } a-=1; } }Describe what was wrong?
-
What value(s) is/are
truein C? What value(s) is/arefalsein C? -
Convert the following code snippet into a function deceleration and definition.
int minus(int a, int b){return a-b;} -
The following three programs will not compile when trying
gcc main.c one.c two.cFix the issues. The programs are below:
//funcs.h void printOne(); void printTwo();//one.c void printOne(){ printf("one\n"); }//two.c void printTwo(){ printf("two\n"); }//main.c int main(){ for(int i=0;i<10;i++){ if(i%2){ printOne(); }else{ printTwo(); } } } -
Below are Java print statements. Write the C equivalent print statement.
int a = 5; int b = 2; float c = 4.4; System.out.println("a = "+a+" b="+b+" c="+c); -
What is the differences between
fopen()file modewandw+? -
The C program below doesn’t write anything to a file.
int main(){ FILE * stream = fopen("helloworld.txt", "r"); fprintf(stream, "Hello World!\n"); fclose(stream); }Fix the program and describe the error(s).
-
What is the output of this C program?
int main(){ float f = 3.14; int n = 10; printf("%d %f\n",f,n); }Was it what you expected or not? Try and describe the process by which the output was achieved.
-
Consider the format directive
%.3g, useman 3 printfto describe what the output would be if the input was3.141592 -
You’re opening a file, and you get an error!?! Provide two preferred ways to report the error to
stderr, as in …if( fopen( /* ... */) == NULL){ // WHAT GOES HERE?! } -
Fix and describe the error in the code below.
printf("Enter a number: "); int num; scanf("%d", num); printf("You entered %d\n", num); -
Fix and describe the error in the code below.
printf("Enter a number: "); int num; scanf("%f", &num); printf("You entered %d\n", num);