Bit Counting Program in C

Posted on 2008-11-11
Last Modified: 2012-05-05
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

Question by:spngegirl01
    LVL 45

    Accepted Solution

    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);
    LVL 3

    Expert Comment

    Your program is crashing because you are ysing argv[1] which is not available.
    LVL 45

    Expert Comment

    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.
    LVL 53

    Expert Comment

    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.).

    Author Comment

    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

    Author Comment

    Never mind, I fixed the problem, thank you all so much!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now