• C

Bit Counting Program in C

My assignment is:

(Exercise 11-4 from Practical C Programming, Steve Oualline)
"Write a program that counts the number of bits set in an integer. For example,
the number 5 (decimal), which is 0000000000000101 (binary), has two bits set."

More specifically, write a function, countBits() that takes an int as an
argument and returns an integer count of the number of set bits. Then write a
main function that reads an integer from the command line and outputs the
return value of countBits() to the screen.

I've rewritten the program from scratch and finally limited the number of small errors (mismatched variables, undefined terms, etc) but now there is some huge system failure that pops up every time I try to compile the program.  What is wrong????
#include <stdio.h>
#include <string.h>
int countBits(int a[], int b)
  int c;
  int d;
  c = 0;
  for (d=0; d<b; d++)
      if(a[d] == 1)
  return c;
int main(int argc, char *argv[])
  int a;
int d;
int f;
int g;
int h;
int i;
int fil[10];
 char line[1000];
d = 0;
printf("Enter the integer whose count bit is to be found:\n");
fgets(line, sizeof(line), stdin);
sscanf(argv[1], "%d", &g);
while (g!=0)
    f= g/2;
    i = g - (f*2);
  fil[d] = i;
    g = f;
a = countBits(fil, d);
printf("The set bits are %d\n", a);
return 0;

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pls post the exact error message/number

fgets(line, sizeof(line), stdin);
sscanf(argv[1], "%d", &g);

Your number is in line ... you should be using

fgets(line, sizeof(line), stdin);
sscanf(line, "%d", &g);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Your program is crashing because you are ysing argv[1] which is not available.
Also, it is a good practice to use meaningful variable names ... names such as a,d,f,g,h are not easy to keep track of. Instead names such as num, count, num_bits are more meaningful.
Thriving as a woman in IT

The IT workforce is diversifying, but the gender gap in tech remains very real. Overcoming stereotypes, and the glass ceiling is important not only for individual women working in the field but for the industry as a whole. Here are eight things women in IT do to succeed.

Btw, I don't think you're taking on this assignment correctly. You're passing an array of bit values to the countBits function, while the assignment said to pass an int value alone.

Also, if you're going to do bit counting, you might want to look into the bitwise operators (bitwise AND, OR, shift, etc.).
spngegirl01Author Commented:
Enter the integer whose count bit is to be found:
Segmentation Fault (core dumped)

That is the error I receive for every integer I enter
spngegirl01Author Commented:
Never mind, I fixed the problem, thank you all so much!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.