#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2

    Why is this program not working?


    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	char ch, A, B, C, D, E, F;
    	
    	printf("Enter a character: ");
    	ch = getchar();
    	
    	if (ch == A || ch == B || ch == C)
    		printf("2");
    	if (ch == D || ch == E || ch == F)
    		printf("3")
    	
    	return 0;
    }
    I am trying to print some integers which correspond to certain alphabets. Exactly where am I going wrong?
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,156
    Rep Power
    2222
    You have not assigned any values to the char variables A, B, C, D, E, of F. Yes, there's something in those memory locations, but since you didn't put them there that means that each of those variables contains garbage.

    Did you instead intend to have written this?:
    Code:
    	if (ch == 'A' || ch == 'B' || ch == 'C')
    		printf("2");
    	if (ch == 'D' || ch == 'E' || ch == 'F')
    		printf("3")
    In that case, you're not comparing ch to other variables, but rather to literal values.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Originally Posted by dwise1_aol
    You have not assigned any values to the char variables A, B, C, D, E, of F. Yes, there's something in those memory locations, but since you didn't put them there that means that each of those variables contains garbage.

    Did you instead intend to have written this?:
    Code:
    	if (ch == 'A' || ch == 'B' || ch == 'C')
    		printf("2");
    	if (ch == 'D' || ch == 'E' || ch == 'F')
    		printf("3")
    In that case, you're not comparing ch to other variables, but rather to literal values.
    Yes, that's exactly what I intended to write! Silly mistake! Thanks for pointing it out! :)

IMN logo majestic logo threadwatch logo seochat tools logo