CS Code

Coding, quiz & more

# Guess The Output series 2

Guess the output series two contains 5 C, C++ tricky programs and outputs with explanation.

## 1) Program

First of all we will take a look at rules to declare variables.

What is variable?

In programming, a variable is a container (storage area) to hold data.

To indicate the storage area, each variable should be given a unique name. Variable names are just the symbolic representation of a memory location. For example:

``int abc = 12;``

### Rules for naming a variable

1. A variable name can only have letters (both uppercase and lowercase letters), digits and underscore.
2. The first letter of a variable should be either a letter or an underscore.
3. There is no rule on how long a variable name (identifier) can be. However, you may run into problems in some compilers if the variable name is longer than 31 characters.

Have you seen the rule 2?

The first letter of a variable can be an underscore.

Thats Why,

``int _ = 1;``

is valid.

Also

``````int __ = 2;
int ___ = 3;
int _____ = 4;
int ___abc = 5;``````

are valid variables as per rule.

So the output of the code

``````int _ = 1;
int __ = 2;
int ___ = _ + __;
printf("%d", ___);``````

is 3.

.

Want to learn competitive programming?

## 2) Guess The Output

Guess the output?

Question

``````#include<stdio.h>

int main()
{
float me = 2.2;
double you = 2.2;
if(me == you)
printf("Matched");
else
printf("Next");
}``````

Options:

``````1) Matched
2) Next
3) Error
4) Nothing will print``````

You might think output is Matched.

But the correct answer is Next.

Why?

For floating point numbers (float, double, long double) the values cannot be predicted exactly.

Depending on the number of bytes, the precession with of the value  represented varies.

Float takes 4 bytes and long double takes 10 bytes. So float stores 0.9 with less precision than long double.

Note:

Never compare or at-least be cautious when using floating point numbers with relational operators(== , >, <, <=, >=,!= ) .

## 3) Program

First of all guess the output of following code

``````#include<stdio.h>
int main()
{
int a = 3 + 2 * 3;
printf("%d", a);

return 0;
}``````

Yes the output is 9.

Why?

DO you remember BODMAS Rule.

BODMAS is an acronym and it stands for Bracket, Of, Division, Multiplication, Addition and Subtraction.

But in programming languages multiplication-division has same precedence.

If division comes first in the expression then division occurs first. And if multiplication comes first int the expression then multiplication occurs first.

For Example:

``````#include<stdio.h>
int main()
{
int a = 3 / 2 * 3;
printf("%d", a);

a = 3 * 2 / 3;
printf("\n%d", a);
return 0;
}``````

The output of above code is

``````3
2``````

Now come to the main question.

And the correct answer is A.

Why?

C uses left associativity for evaluating expressions to break a tie between two operators having same precedence.

If you have any doubt feel free to mention your doubts in comment section below and if you gain some knowledge then also mention what you gain from this post .

Thank You….

## 4) Guess The Output

First of all guess the output of following code

``````#include<stdio.h>

int main() {

int a[] = {10,20,30};

printf("%d", a[0]);

return 0;
}``````

10? Yes you are right….

Array is derived data type, it can store multiple values of same data type.

Elements are stored in the array are sequentially.

Assume a[] = {1,2,3} is an integer array having three values 1, 2 and 3.

Suppose integer is of 4 bytes so memory required for array is 4*3(elements)=12 bytes.

``````a[0] = 1
a[1] = 2
a[2] = 3``````

a[0] is also written as 0[a].

Why?

a[0] means *(a+0).

what *(a+0) does mean?

a is name of array. It contains starting address of array and array a is of integer type.

so *(a+0) means the value at address (a+0) and the value at (a+0) i.e. a[0] is 1.

Similarly, *(a+1) means a[1] or 1[a] and so on…

Now guess the output of following code

``````#include<stdio.h>

int main() {

int a[] = {10,20,30};

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

return 0;
}``````

30? YES you are right….

Also guess the result of the code below

``````#include<stdio.h>

int main() {

int a[] = {10,20,30};

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

return 0;
}``````

*a means value of a[0] and then add 1 to it. so the output is 10+1=11.

``````#include<stdio.h>

int main() {

int a[] = {10,20,30};

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

return 0;
}``````

Here *(a+1) means a[1] which is 20 and plus 1 is 21.

So the output of above code is 21.

Finally output of given code https://www.instagram.com/p/CCoKVM-hG4z/ is 11.

## 5) Guess The Output

The output of above code is 2.

Why?

The answer from our community member @the_wandering_engineer is

C doesn’t deal with out of bounds exception and with the concept of pointers it’s about to access memory blocks that it shouldn’t. So C let’s the user assign value to the 9 element. There is a very small chance that the 9th memory block was some system critical block.. other than than… It simply prints 2.