Thread: C program character count

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,726
    Quote Originally Posted by stahta01 View Post
    Is there any reason you have an "*" in front of this?

    Code:
    *Blanks++;
    Edit: I see the reason now; but, you are likely calling CharacterCount wrong.

    Tim S.
    That's another good catch. This:
    Code:
    *Blanks++;
    is equivalent to:
    Code:
    *(Blanks++);
    since the dereference has no net effect, after optimisation the above is thus equivalent to:
    Code:
    Blanks++;
    which merely increments the pointer rather than the actual count that the pointer points to.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    714
    This is also incorrect:

    Code:
    char c;
    
    c = fgetc(fp);
    fgetc() returns an int, not a char.

    If char is unsigned on your platform, then c will never equal EOF. On the other hand, if char is signed on your platform, then c might equal EOF accidentally for some legitimate input. Either way, declaring c as an int will avoid both of these problems.

    You're not saving anything (time or memory) by declaring c to be char (but you're spending a tiny bit more time by typing "char" instead of "int"). As I like to say, why do the wrong thing when the right thing is just as easy (or easier, in this case)?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. count and print the frequency of each ASCII character
    By s_jsstevens in forum C Programming
    Replies: 3
    Last Post: 02-07-2011, 09:33 PM
  2. Character count program
    By tiger6425 in forum C Programming
    Replies: 3
    Last Post: 09-04-2010, 01:35 PM
  3. simple program to count the character not working
    By hitesh1511 in forum C Programming
    Replies: 12
    Last Post: 08-08-2006, 04:13 AM
  4. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM

Tags for this Thread