Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

check this code ...its not running whats the bug in it?

+2 votes
asked Mar 8, 2020 by anonymous
int func(int num)

{

int count = 0;

while (num)

{

count++;

num >>= 1;

}

return (count);

}

4 Answers

0 votes
answered Mar 9, 2020 by sherin saharia (140 points)
Here count is declared locally inside this function. If you try to return the local variable, the variable will be destroyed..
0 votes
answered Mar 9, 2020 by Answer

int func(int num)

{

  int count = 0;

  while (num)

  {

    count++;

    num >>= 1;

  }

  return (count);

}

The red color highlighted one is the reason for the error.

0 votes
answered Mar 10, 2020 by Zain Shahbaz (140 points)
The condition isnt correctly cited in the while loop and num>>=1 condition is not a valid intruction, even I dont understand the meaning of this instruction...
0 votes
answered Mar 11, 2020 by Rick Fencl (140 points)
This function is perfectly fine in C

Java requires that the conditional in the while loop be converted to a boolean expression.

Java:

class Main {

public static void main(String [] args) {
   System.out.println(func (5));
}

static int func (int num) {
  int count = 0;
  while (num>0)  {
      count++;
      num >>= 1;
      System.out.println(num);
    }
  return (count);
}

}

output: 2 1 0 3

C version:

#include <stdio.h>
int func (int num) {
  int count = 0;
  while (num)  {
      count++;
      num >>= 1;
      printf("%d\n", num);
    }
  return (count);
}

int main()
{
    printf("%d\n", func(5));
    return 0;
}

output: 2 1 0 3
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...