0
int main(){
int n,i=0,c1,c2;
char a[1000];
scanf("%d",&n);

while(n!=0)
{
    a[i]=n%2;
    printf("%d",a[i]); // This prints Values Correctly
    n=n/2;
    i++;

}    
a[i]='\0';
for(i=0;a[i]!='\0';i++)
printf("%d",a[i]);    //This prints only the first element of the array
}

What am I missing here? Why can't I loop through and print the values of the char array although it works when I try to print it inside while loop?

5
  • 1
    a[i]!='\0' is a[i] != 0 Commented Oct 16, 2016 at 14:28
  • So how do i avoid that? I can't use strlen(a) function, it gives junk values Commented Oct 16, 2016 at 14:29
  • Count bit(at after while, int countBit = i;) then i < countBit (for(i=0;i < countBit;i++) Commented Oct 16, 2016 at 14:31
  • Please add a description of what you are trying to do and what exactly is the error that you are getting. Commented Oct 16, 2016 at 14:35
  • If any answer answered your question, please consider accepting it (greed checkmark to the left of the answer). This will give some reputation to you and the answerer and mark the question as answered. Commented Oct 17, 2016 at 8:14

3 Answers 3

3

The array, which has the type char, is used to store integers, the array isn't a string. Because you store remained of division by 2, most elements will have the value 0.

Remove the line that null terminates the array. The variable i already counts the number of elements entered, so iterate and print until you print i elements.

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah it works. One more thing, why does using strlen gives wrong string length?
@Khacho Because the array isn't a used as a string. If you want to store integers use int or signed or unsigned char. If you want a string store characters: '1', '0'.
1

When your input is an even number like 12, then the first digit to be stored is 0 which actually means NULL as the array defined is a character array.

That is why nothing gets printed when the input is an even number.

Here is what you can do:

#include<stdio.h>
int main(){
int n,i=0,c1,c2;
char a[1000];
scanf("%d",&n);

while(n!=0)
{
    a[i]='0' + n%2; //note here
  //  printf("%d",a[i]); // This prints Values Correctly
    n=n/2;
    i++;

}    
a[i]='\0';
for(i=0;a[i]!='\0';i++)
printf("%c",a[i]);    
}

1 Comment

Thanks for the help
1

First thing you used a[i]=n%2 and n is an integer value so what happens is say n=65(for A) then 65%2=1 (now a[0]=1) 65/2=32 now, for the next iteration 32%2=0 so basically you stored a null value at the first or second iteration depending on the value of n.

I edited your code a little bit for better understanding and debugging.

#include<stdio.h>
int main(){
int n,i=0,c1,c2;
char a[1000];
scanf("%d",&n);

while(n!=0)
{    
a[i]=n%2;
printf("%d\t%d\t%c\n",a[i],i,a[i]); // This prints Values Correctly
n=n/2;
i++;
} 
printf("%d\n",i);   
a[i]='\0';
printf("%d\t%d\n",a[i],i);
for(i=0;a[i]!='\0';i++)
printf("%d\t%d\n",a[i],i);    //This prints only the first element of the         array
}

SAMPLE RUN:-

Untitled.png

I also recommend that before posting for such silly errors try debugging using printf within loops and perform dry runs before you come to any conclusions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.