Multi-character character constant warning

This is a discussion on Multi-character character constant warning within the C Programming forums, part of the General Programming Boards category; Hi, I'm trying to learn C and I'm using K&R second edition. I am copying this example in the book ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Location
    Concord, ON
    Posts
    2

    Question Multi-character character constant warning

    Hi,
    I'm trying to learn C and I'm using K&R second edition. I am copying this example in the book called 'Line counting'. The code is

    Code:
    #include <stdio.h>
    
    main()
    {
            int c, nl;
            nl = 0;
            while((c=getchar()) != EOF){
                    if (c == '/n')
                            ++nl;}
            printf("%d\n",nl);
    }
    When I compile it, I get an error.

    Code:
    visuthan@visuthan:~/Projects/C$ gcc line_counter.c -o line_counter
    line_counter.c: In function ‘main’:
    line_counter.c:8:12: warning: multi-character character constant [-Wmultichar]
    Can someone explain what this means? I tried reading online but I really can't figure out the replies in most posts.

    Also, can anyone tell me how I can create an EOF situation when getchar() is listening?

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    3,304
    Compare your "newline" character in this piece of code:

    Code:
    if (c == '/n')
    ... to the "newline" character in this piece of code:

    Code:
    printf("%d\n",nl);
    The former is written incorrectly, the latter correctly. Carefully note the direction of the slashes.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,614
    Quote Originally Posted by visuthan
    When I compile it, I get an error.
    '/n' should be '\n'

    Quote Originally Posted by visuthan
    Can someone explain what this means?
    By using the wrong slash, you wrote a single character constant comprised of the characters '/' and 'n'.

    Quote Originally Posted by visuthan
    Also, can anyone tell me how I can create an EOF situation when getchar() is listening?
    You probably can trigger EOF by entering CTRL + D or CTRL + Z.
    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

  4. #4
    Registered User
    Join Date
    Jul 2012
    Location
    Concord, ON
    Posts
    2
    Matticus and laserlight. Thanks a lot. I'm so dumb.

    And just so it might help someone else. I have confirmed that CTRL + D creates an EOF whereas CTRL + Z tends to stop the program (process state T).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Character Constant.. how to correct the error? 3:
    By Tinray Reyes in forum C++ Programming
    Replies: 2
    Last Post: 06-04-2011, 08:30 AM
  2. warning: multi-character character constant
    By aadil7 in forum C Programming
    Replies: 2
    Last Post: 12-12-2010, 09:02 PM
  3. "warning: multi-line character constant"
    By Benzakhar in forum C++ Programming
    Replies: 7
    Last Post: 08-24-2008, 03:29 PM
  4. multi-character character constant error
    By robwhit in forum C Programming
    Replies: 3
    Last Post: 07-15-2007, 12:12 AM
  5. wide character (unicode) and multi-byte character
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 05-05-2007, 12:46 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21