Theory Assignment 1.
Theory Assignment 1.
Theory Assignment 1.
1. Find the output of the following program and justify your answer.
#include<stdio.h>
int main()
{
static int i; for(i+
+;++i;i++)
{
printf("%d ", i);
if(i == 6)
break;
}
return 0;
}
Output: 2 4 6
The value of I first increases while value initializing and then while checking and after after that while
checking and function increment.
2. Point out the difference between the constants 7, '7', and "7".
3. Predict the output of the following code snippet and justify your answer.
#include <stdio.h>
int main()
{
unsigned char a=50;
char loop;
for(loop=7; loop>=0; loop--)
printf("%d ",(a & (1<<loop))?1:0);
return 0;
}
Output: 0 0 1 1 0 0 1 0
Left assignment operator
4. Mention the use of the register storage class and explain why the ampersand operator is not
allowed on register variables.
Whenever we declare any variable inside C Program then memory will be randomly allocated at
particular memory location.We have to keep track of that memory location. We need to access value
of that memory location using ampersand operator i.e (&).If we store same variable in the register
memory then we can access that memory location directly without using the Address operator.
Register variable will be accessed faster than the normal variable thus increasing the operation and
program execution.
5. For which reason pointers are used to pass arguments in the call by reference method for function.
In order to get returned multiple data to be used in the main function as well as reduce memory of
the program and make execution faster pointers are used to pass arguments in the call by reference
method for function.
6. Predict the output of the following code snippet and justify your answer.
#include <stdio.h>
int main()
{
while(1)
{
printf("Hello");
}
return 0;
}
Output: HelloHelloHelloHello………………………
7.
a=10+2*12/(5%3)+5
a=27
int main()
{
int a=0,b;
a=(5>2)? b=6: (b=8);
printf(“%d %d”, a,b);
return 0;
}
Output: 6 6
8. Predict the output of the following code snippets and justify your answer. (4 marks) i.
int main()
{
int i=0,j=0;
for(i=0;i<5;i++)
{
for(j=0;j<4;j++)
{
if(i>1)
break;
}
printf(“Hi “);
}
}
Output: HiHiHiHiHi
The printf statement is outside inner loop and no change occurs.
ii.
#include<stdio.h>
#define loop while(1)
int main()
{
loop;
printf(“Infinite”);
return 0;
}
Break/Continue: When break is encountered the switch or loop execution is immediately stopped.
When continue is encountered, the statements after it are skipped and the loop control jump to next
iteration.
Example:
Loop{
Block1:
If(condition)
Break;
}
Block 2
}
In the code the as the if condition is satisfied the while loop breaks even the condition for while loop
is satisfied and the block 2 doesn’t run in the case. But if continue statement was used then without
running the Block 2 The while loop is directly looped if continue statement runs.
Part C
10. Write a C program to find the product of digits in a number. For example, if the input number is
21455, the output is 200. Don’t use the concept of arrays. (8 marks)
#include<stdio.h>
int main()
{
int n;
printf("Enter a number ");
scanf("%d",&n);
int a,c=1;
while(n!=0)
{
a=n%10;
c=c*a;
n=(n-a)/10;
}
printf("The product of digits of %d is %d",n,c);
return 0;
}
12. Write the output of the following C snippets and justify your answer (10 marks)
i. #include<stdio.h>
int main()
{
int a=0;
a=5>2? printf (“4”):3;
printf(“%d”,a);
return 0;
}
Output: 41
As condition is true (Ternary operator) 4 is printed and value of a 1 is printed
ii.
int main()
{
int a=0;
a=10+2*12/5*2+5;
printf(“%d”, a);
return 0;
}
Output: 23
Data type is integer
b. Write the output of the following C snippets and justify your answer. (4 marks)
i. int main()
{
int n;
for(n = 7; n!=0; n--)
printf("n = %d", n--);
getchar();
return 0;
}