Thread: Very basic C coding warning

  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 09:34 PM.

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    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, 01:29 PM
  2. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03: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, 06:51 PM