



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Examples and explanations of the printf function in c, focusing on the usage of different format specifiers and handling of variables. It covers topics such as printing characters, integers, strings, and arrays, as well as potential issues like memory allocation and buffer overflows.
Typology: Study notes
1 / 7
This page cannot be seen from the preview
Don't miss anything!




a) the character a b) 97 (ASCII code of a) c) some arbitrary number d) it may crash/will be unpredictable e) it won't compile What will this code print: printf("%i", 'a'); 1 a) the character a b) 97 (ASCII code of a) c) some arbitrary number d) it may crash/will be unpredictable e) it won't compile What will this code print: printf("%c", 'a'); 2 a) the character a b) 97 (ASCII code of a) c) some arbitrary number d) it may crash/will be unpredictable e) it won't compile What will this code print: printf("%c"); 3 a) the character a b) 97 (ASCII code of a) c) some arbitrary number d) it may crash/will be unpredictable e) it won't compile What will this code print: printf(); 4 a) the character a b) 97 (ASCII code of a) c) some arbitrary number d) it may crash/will be unpredictable e) it won't compile What will this code print: printf("%i", "a"); 5 What will this code do: *int main(void) { char s; fgets(s, 61, stdin); printf("%s", s); } a) accept input, print the first 60 characters b) accept input, print arbitrary text c) will not accept input, print arbitrary text d) it may crash/will be unpredictable e) it won't compile 6
What will this code do: int main(void) { char s[61]; fgets(s, sizeof(s), stdin); printf("%s", s); } a) accept input, print the first 60 characters b) accept input, print arbitrary text c) will not accept input, print arbitrary text d) it may crash/will be unpredictable e) it won't compile 7 What will this code do (assume malloc doesn't fail): *int main(void) { char s = malloc(61); fgets(s, sizeof(s), stdin); printf("%s", s); } a) accept input, print user's input, up to the first 60 characters b) accept input, print arbitrary text c) it may crash/will be unpredictable d) it won't compile e) none of the above (^8) What will this code do (assume malloc doesn't fail): *int main(void) { char s = malloc(61); fgets(s, 61, stdin); printf("%s", s); } a) accept input, print the first 60 characters b) accept input, print arbitrary text c) it may crash/will be unpredictable d) it won't compile e) none of the above 9 a)sizeof(s)/sizeof(s[0]) b)sizeof(s) c)strlen(s) d)sizeof(s)/sizeof(char) e) None of the above How can we find the length of string s? *void example(char s); 10 How can we find the # of elements in arr? void example(int arr[]); a)sizeof(arr)/sizeof(arr[0]) b)sizeof(arr) c)strlen(arr) d)sizeof(arr)/sizeof(int) e) None of the above 11 How can we find the # of elements in arr? int arr[SOME_NUMERIC_CONSTANT]; a)sizeof(arr)/sizeof(arr[0]) b)sizeof(arr) c)strlen(arr) d)sizeof(arr)/sizeof(int) e) None of the above 12
19 a) fopen("example.txt", "w"); b) fopen("example.txt",'w'); c) fopen("example.txt","r+"); d) fopen("example.txt","a"); e) None of the above If you'd like to write to a file, appending to the end if it already exists, which of the following should you use: 20 a) fseek(f, 200, SEEK_END); b) fseek(f, 200, SEEK_SET); c) fseek(f, 200, SEEK_BEGIN); d) fseek(f, 200, SEEK_CUR); e) None of the above Which of the following will ensure that a subsequent fread from FILE pointer f will read from the last 200 bytes of the file: 21 a) I think I know b) I know what that is, but I'm not sure why we need it. c) What's a free function? Why do we have a free function? 22 a) I think I know b) I know what one does, but I don't think they're different c) What's a ++? What's the difference between i++ and ++i 23 a) b) c) either is fine d) both are erroneous Which of the following is a valid implementation of a linked list node: *struct node { int value; struct node next; }; struct node { int value; struct node next; }; 24 a) Yes b) No c) I don't know Is there anything wrong with this code to resize a malloc'd character buffer? **char resize(char old, int size) { char ret = malloc(size); memcpy(ret, old, sizeof(old)); return ret; } / called elsewhere: / arr = resize(arr, old_size + 10);
25 a) 0xFFFF b) 0xFF c) 0xF0FF d) 0xF0F e) None of the above 0xF00F & 0xFF | 0xF0F ^ 0x000F == 26 a) x = x >> n b) x = x ^ (1 << n) c) x = x >> (n-1) d) x = x ^ (1 << (n-1)) e) None of the above Which of the following will toggle the nth^ least- significant bit in int x 27 a) x = x >> n b) x = (1 & x) >> n c) x = x >> (n-1) d) x = (1 & x) >> (n-1) e) None of the above Which of the following will return 1 if the nth least-significant bit in int x is 1, 0 otherwise 28 a) global b) static local c) static global d) automatic local e) None of the above What kind of variable has local scope, but global lifetime 29 a) 8 b) 4 c) 16 d) 2 How many possible values can s.x take? struct foo { int x:3, y:2, z:1}; struct foo s; 30 a)1, 2 b)2, 1 c) In may crash/will be unpredictable d) It will not compile/will give warnings and not do what it's supposed to do What will this program print: void swap(int a, int b) { int t = a; a = b; b = t; } int main() { int a = 1, b = 2; swap(a, b); printf("%d, %d\n", a, b); }
37 a)1, 2 b)2, 1 c) In may crash/will be unpredictable d) It will not compile/will give warnings and not do what it's supposed to do What will this program print: **struct foo { int v; }; void swap(struct foo a, struct foo b) { int t = a->v; a->v = b->v; b->v = t; } int main() { struct foo a = {1}, b = {2}; swap(&a, &b); printf("%d, %d\n", a.v, b.v); }