C Programming Exercises and Solutions: A Guide to Understanding Fundamental Concepts, Study notes of C programming

A collection of c programming exercises designed to reinforce fundamental concepts. Each exercise includes code snippets, explanations, and expected outputs, providing a comprehensive learning experience. The exercises cover topics such as pointers, data types, operators, control flow, and functions, enabling users to solidify their understanding of c programming principles.

Typology: Study notes

2024/2025

Available from 01/28/2025

m-g-96
m-g-96 🇮🇳

2 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1.
#include <stdio.h>
int main()
{
Int a = 5, *b, c;
b = &a;
printf("%d", a * *b * a + *b);
return (0);
}
Output: 130
2. #include <stdio.h>
int main()
{
int i, j = 3;
float k = 7;
i = k % j; // trying to modulus float value with int
printf("%d", i);
return (0);
}
Output: compile time error
3. #include <stdio.h>
4. int main()
5. {
pf3
pf4
pf5

Partial preview of the text

Download C Programming Exercises and Solutions: A Guide to Understanding Fundamental Concepts and more Study notes C programming in PDF only on Docsity!

#include

int main()

Int a = 5, *b, c;

b = &a;

printf("%d", a * *b * a + *b);

return (0);

Output: 130

  1. #include int main() { int i, j = 3; float k = 7; i = k % j; // trying to modulus float value with int printf("%d", i); return (0); } Output: compile time error
  2. #include
  3. int main()
  4. {
  1. int a;
  2. int b = 5;
  3. a = 0 && --b;
  4. printf("%d %d", a, b);
  5. } Explanation: In the logical AND operator, if any of the condition is false then the whole result is false. Here 0 acts as a false value in c therefore the whole result is false and – b is not executed. Therefore the result is 0 5. #include int main() { int a = 0; while (a < 5) { printf("%d\n", a++); } } Explanation: Here, the while loop is going to execute 5 times. We know that a++ is post increment and in post-increment we first assign then increment.when first time while loop execute, while(0<5) the printf function contains \n which acts as a backslash escape character. Therefore it prints 0\n in the first loop, 1\n in the 2nd loop, 3\n in the 3rd loop and so on. #include int main() { int x = 5; if (x >= 10) printf("Hello"); printf("GFG"); else printf("hi"); } Explanation: It will give compilation error because when there is only one statement in the if clause, then curly braces are not required but if there are more than one statement then we must enclose with curly braces and here we are not giving curly braces. Therefore we will get compile time error with a message else without a previous if. It is a problem of handing if statement which is not allowed in C.

1. Find the output

printf(“Hello”); } Ccc() { printf(“Bye”); } void main() { int (*ptr[3])(); ptr[0] = Aaa; ptr[1] = Bbb; ptr[2] = Ccc; ptr2; } Answer : a. Hello b. Hi c. Bye d.compilation error

5. struct temp

int a; struct temp no_roll; double c; }obj; Answers: a. Underscore cannot be used in variable declaration b. it will be compiled successfully c. obj must be a pointer to struct d. error: struct variables cannot be included in its own structure

int takefirst(int a,int b,int c) { return (++a,b+2,c); } void main( ) { static int m,n; m = takefirst(n,m,n); printf(“%d”,m); }

Answer : a. 2 b.1 c.0 d. syntax error in return statement

7. Find output of fallowing program

void main() { printf("Today \r is a7\b great \tday");} Answers :

8. What is output?

void main( ) { int i=107,x=5; printf((x>7)?”%d”:”%c”,i); } Answers: a. compilation error b. k c. z d. 107

9. If a= - 11 and b= - 3 what is the value of a%b?

Answers : a. 2 b.- 2 c.3 d.- 3

10. Find output

int show(int v) { if(v==1||v==0) return 1; if(v % 2==0) return show(v/2) +2;