Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional branches

Tags:

c++

c

Why this piece of code compiles?

#include <iostream>

int foo(int x)
{
   if(x == 10)
     return x*10;
}

int main()
{
int a;
std::cin>>a;
std::cout<<foo(a)<<'\n';
}

The compiler shouldn't give me an error like "not all code paths returns a value"? What happens/returns my function when x isn't equal to ten?

like image 532
MasterChief Avatar asked Mar 16 '12 17:03

MasterChief


People also ask

What is an example of conditional branching?

In this example, there are only two branches. The first branch, which selects a loan offer from United Loan, is executed if a case condition containing an XPath Boolean expression is met. Otherwise, the second branch, which selects the Star Loan loan offer, is executed.

What is a conditional branch?

A conditional branch instruction is a branch instruction that may or may not generate a transmission of control that relies upon the value of stored bits in the PSR (processor status register). It provides decision-making capabilities in the control unit.

What is conditional branches in computer architecture?

A conditional branch instruction is a branch instruction that may or may not cause a transfer of control depending on the value of stored bits in the PSR (processor status register). Each conditional branch instruction tests a different combination of Status bits for a condition.

What is conditional branch and unconditional branch?

Branches are used to transmission control, unconditionally or conditionally, to a stated position of the program. Unconditional branches are continually taken. In contrast, conditional branches contain a condition and thus are either taken or not taken, based on either the particular condition is true or false.


1 Answers

The result is undefined, so the compiler is free to choose -- you probably get what happens to sit at the appropriate stack address where the caller expects the result. Activate compiler warnings, and your compiler will inform you about your omission.

like image 61
bitmask Avatar answered Sep 28 '22 03:09

bitmask