Recent questions tagged c-programming

12 12 votes
1 1 answer
1.1k
1.1k views
Assume:$\texttt{short = 2 bytes}$, $\texttt{int = 4 bytes}$, $\texttt{char = 1 byte}$$\texttt{int}$ needs $4$-byte alignment and the final structure size is rounded to a ...
14 14 votes
2 2 answers
580
580 views
Assume:struct Student { double gpa; }; struct Student alice; struct Student *sptr = &alice;Which of the following correctly assigns $\texttt{4.0}$ to $\texttt{alice.gpa}$...
9 9 votes
1 1 answer
441
441 views
Consider the following code idea from the source:typedef struct { double x, y; } Point; void reset(Point p) { p.x = p.y = 0; } int main() { Point a = {12.0, 42.0}; Point ...
6 6 votes
1 1 answer
425
425 views
What is printed by the following program?#include <stdio.h struct Student { int id, year; char grade; }; int main() { struct Student s; s.id = 10001; s.year = 2010; s.gra...
8 8 votes
1 1 answer
390
390 views
Which option correctly completes the given exercise?#include <stdio.h /* define the person struct here using the typedef syntax */ int main() { person john; john.name = "...
11 11 votes
2 2 answers
453
453 views
What is the output of the following code?#include <stdio.h void func(int a, int *bptr) { a = 42; *bptr = 42; return; } int main(void) { int x = 100, y = 100; func(x, &y);...
11 11 votes
3 3 answers
457
457 views
Consider the macro:#define square(x) (x*x)Now consider the statement:c = square(a + b);After macro expansion, which expression is produced?$\texttt{c = ((a + b) * (a + b)...
11 11 votes
4 4 answers
376
376 views
Consider the statement:double ans = 18.0 / squared(2 + 1);For each of the following macro definitions, find the value of $\texttt{ans}$.#define squared(x) x*x #define squ...
7 7 votes
3 3 answers
403
403 views
What does the following code do?#include <stdio.h int main() { char c; while ((c = getchar()) != EOF) { if (c >= 'A' && c <= 'Z') c = c - 'A' + 'a'; putchar(c); } return ...
9 9 votes
2 2 answers
374
374 views
Consider the following program:#include <stdio.h int main(void) { char ch; printf("Enter your input: "); while ((ch = getchar()) != 'C') { putchar(ch); } return 0; }For t...
5 5 votes
1 1 answer
293
293 views
Assume:$\texttt{char}$ takes $1$ byte $\texttt{int}$ takes $4$ bytes $\texttt{int}$ must be stored at an address divisible by $4$ Structure size is rounded to a multiple ...
5 5 votes
2 2 answers
291
291 views
What is the output of the following code?#include <stdio.h struct Book { char name[4]; int pages; }; int main() { struct Book b1 = {"CAT", 50}; struct Book b2; b2 = b1; b...
5 5 votes
2 2 answers
287
287 views
What is the output of the following code?#include <stdio.h struct Item { int code; int price; }; void f(struct Item x) { x.code = x.code + 1; x.price = x.price + 50; } vo...
7 7 votes
3 3 answers
350
350 views
What is the output of the following code?#include <stdio.h struct Student { int roll; int marks; }; int main() { struct Student s[3] = { {1, 50}, {2, 60}, {3, 70} }; stru...
6 6 votes
2 2 answers
298
298 views
What will happen when the following code is compiled?#include <stdio.h struct Node { int data; }; int main() { struct Node n = {25}; struct Node *p = &n; printf("%d", *p....
6 6 votes
3 3 answers
305
305 views
What will happen when the following code is compiled?#include <stdio.h struct Point { int x; int y; }; int main() { struct Point p1 = {2, 4}; struct Point p2; p2 = p1; p2...
7 7 votes
2 2 answers
288
288 views
What is the output of the following code?#include <stdio.h struct Item { int code; int price; }; void update(struct Item *p) { p->price = p->price + 20; (*p).code = (*p)....
6 6 votes
2 2 answers
259
259 views
What is the output of the following code?#include <stdio.h struct Box { int width; int height; }; int main() { struct Box b = {4, 6}; struct Box *p = &b; p->width = p->wi...
5 5 votes
2 2 answers
298
298 views
What is the output of the following code?#include <stdio.h struct Student { int roll; int marks; }; int main() { struct Student s[3] = { {1, 70}, {2, 80}, {3, 90} }; s .m...
6 6 votes
2 2 answers
281
281 views
What is the output of the following code?#include <stdio.h struct Book { char name; float price; int pages; }; int main() { struct Book b1 = {'C', 150.5, 300}; b1.pages =...
5 5 votes
2 2 answers
278
278 views
What is the main issue in the following code? Assume both $\texttt{malloc()}$ calls succeed.#include <stdio.h #include <stdlib.h int main() { int *p = (int *)malloc(sizeo...
4 4 votes
2 2 answers
267
267 views
What will happen in the following code?#include <stdio.h #include <stdlib.h int main() { int *p = (int *)malloc(sizeof(int)); *p = 10; free(p); printf("%d", *p); return 0...
5 5 votes
2 2 answers
278
278 views
What is the output of the following code? Assume $\texttt{malloc()}$ succeeds.#include <stdio.h #include <stdlib.h int main() { int *p = (int *)malloc(3 * sizeof(int)); p...
6 6 votes
2 2 answers
238
238 views
What is the output of the following code?#include <stdio.h int main() { int x = 25; void *p = &x; printf("%d", *(int *)p); return 0; }$\texttt{25}$ Address of $\texttt{x}...
7 7 votes
2 2 answers
275
275 views
Which of the following correctly describes the declarations?int *a[10]; int (*p)[5];$\texttt{a}$ is a pointer to an array of $10$ integers, and $\texttt{p}$ is an array o...
6 6 votes
2 2 answers
301
301 views
What is the output of the following code?#include <stdio.h int main() { int a[3] = { {1, 2}, {3, 4}, {5, 6} }; int (*p) = a; p++; printf("%d %d %d", (*p)[0], *(*(p + 1)...
8 8 votes
3 3 answers
388
388 views
What is the output of the following code?#include <stdio.h int main() { int a [3] = { {2, 4, 6}, {8, 10, 12} }; printf("%d %d %d", a , *(*(a + 1) + 1), *(*a + 2)); retur...
7 7 votes
3 3 answers
304
304 views
What is the output of the following code?#include <stdio.h void update(int q, int *r) { q = q + 5; *q = r; q = q + 2; } int main() { int x = 3, y = 8; int *p = &x; u...
8 8 votes
3 3 answers
318
318 views
What is the output of the following code?#include <stdio.h int main() { int a = 5, b = 9; int *p = &a; int q = &p; *p = *p + 2; *q = &b; q = q + 3; printf("%d %d %d", ...
8 8 votes
3 3 answers
337
337 views
What is the output of the following code?#include <stdio.h int main() { int x = 10; int *p = &x; int q = &p; q = q + 4; *p = *p + 1; printf("%d %d", x, q); return 0; ...