Very basic C coding warning

This is a discussion on Very basic C coding warning within the C Programming forums, part of the General Programming Boards category; Howdy everyone, I did a search in Google and nothing came up for this, but anyway I am learning to ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    9

    Very basic C coding warning

    Howdy everyone,

    I did a search in Google and nothing came up for this, but anyway I am learning to code in C. I am up to, Understand Arrays, and I wrote a small program for the Exercises and while it will compile and run correctly, I am getting a warning. Something tells me accepting warnings at this level, can lead to me accepting them and higher levels and I understand this could have a bad effects. So if someone could enlighten me to the warning in this code.

    Code:
    #include <stdio.h>
    
    main()
    {
        char array_ch[5] = {'A', 'B', 'C', 'D', 'E'};
        int i;
    
        for(i=0; array_ch[i] && i != '/0'; i++)
            printf("%c", array_ch[i]);
    
        return 0;
    }
    Thank you agin, and sorry for such noob questions.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Probably it's because the exit test in your for loop is going to always return true.

    Think about ways to simplify that part of your code...

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    *smacks head*

    Because of the:

    Code:
    (i != '/0')
    ??

    So I would just set my for statment to loop the amount of char constants defined in the array?

    Code:
        for(i=0; i<5 ; i++)
            printf("%c", array_ch[i]);
    Last edited by Gikimish; 10-01-2010 at 10:34 PM.

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by Gikimish View Post
    *smacks head*

    Because of the:

    Code:
    (i != '/0')
    ??
    As you have no doubt understood, there is no '\0' in your array to test for.

    Quote Originally Posted by Gikimish View Post
    So I would just set my for statment to loop the amount of char constants defined in the array?

    Code:
        for(i=0; i<5 ; i++)
            printf("%c", array_ch[i]);
    That would be a good way to do it, yes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 02:29 PM
  2. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 04:27 PM
  3. Using malloc
    By ulillillia in forum C Programming
    Replies: 34
    Last Post: 02-20-2008, 06:41 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 07:51 PM

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