char format specifier and segmentation fault

A

asit

plz fix the bug...

#include <stdio.h>

int main()
{
char ch;
char i;
printf("Enter an unsigned char : ");
scanf("%d",&ch);
printf("You have entered : %ud\n",ch);
printf("binary representation...\n");
for(i=sizeof(char)*8-1;i>=0;i--)
((1<<i)&ch) ? printf("1 "): printf("0 ");
printf("\n");
return 0;
}

when i run it, if 130 is entered..it should show 130...but its not
showig ???
and also why the error segmentation fault comes ??
 
R

Richard Heathfield

asit said:

I don't know what "plz" means. I *guess* it means "please", but it's a good
idea to be precise when composing Usenet articles.
fix the bug...

I will fix *one* bug. Beware - there may be more.
#include <stdio.h>

int main()
{
char ch;
char i;
printf("Enter an unsigned char : ");
scanf("%d",&ch);

Here, you have told scanf to expect a pointer to an int, but you have
provided a pointer to a char. Use %c rather than %d, and check the return
value that scanf provides. It should be 1 (because you asked for 1 field
to be converted). If it is anything else, deal with it appropriately.
 
S

suresh shenoy

plz fix the bug...

#include <stdio.h>

int main()
{
        char ch;
        char i;
        printf("Enter an unsigned char : ");
        scanf("%d",&ch);
        printf("You have entered : %ud\n",ch);
        printf("binary representation...\n");
        for(i=sizeof(char)*8-1;i>=0;i--)
                ((1<<i)&ch) ? printf("1 "): printf("0 ");
        printf("\n");
        return 0;

}

when i run it, if 130 is entered..it should show 130...but its not
showig ???
and also why the error segmentation fault comes ??

Use the %c specifier if you are reading in a character. Read 'Format
Specifiers' in google for C. That should give you some idea.
Also you should know the return type of sizeof(), it is unsigned int.
You are using char 'i' and unsigned int from sizeof() in the 'for'
expression.

Suresh Shenoy
 
V

vippstar

Use the %c specifier if you are reading in a character. Read 'Format
Specifiers' in google for C. That should give you some idea.
You can also read about them in man 3 printf, your C book or the C99
stardard.
You can read a signed char with '%hhd' too, but it *has* to be signed.
plain char or unsigned won't do.
Also you should know the return type of sizeof(), it is unsigned int.
You are using char 'i' and unsigned int from sizeof() in the 'for'
expression.
sizeof evaluates to size_t.
The proper way to print it on a C99 compliant code is with '%zu'.
On pre-C99 code, (C89,C90,C95) you could cast it to (unsigned long)
and use '%lu'.
It might produce incorrect results (however, no UB), but I've never
heard of a system that has C89 implemented, and SIZE_MAX > ULONG_MAX.
 
J

Joachim Schmitz

You can also read about them in man 3 printf, your C book or the C99
stardard.
You can read a signed char with '%hhd' too, but it *has* to be signed.
plain char or unsigned won't do.
sizeof evaluates to size_t.
Doesn't matter too much as a) the sizeof is superfluos (and therfor should
get dropped or replaced with sizeof(ch) to ease code changes) as sizeof char
by definition is 1 and b) because sizeof very likely (and sizeof char surly)
returns a value that pretty easily fits an unsigned char (unless called for
a large array or struct, neither is the case here).
 
K

Keith Thompson

asit said:
plz fix the bug...

#include <stdio.h>

int main()
{
char ch;
char i;
printf("Enter an unsigned char : ");
scanf("%d",&ch);
printf("You have entered : %ud\n",ch);
printf("binary representation...\n");
for(i=sizeof(char)*8-1;i>=0;i--)
((1<<i)&ch) ? printf("1 "): printf("0 ");
printf("\n");
return 0;
}

when i run it, if 130 is entered..it should show 130...but its not
showig ???
and also why the error segmentation fault comes ??

One of the many things you need to do is decide what input the program
expects. It prompts the user to "Enter an unsigned char"; if I'm
running the program, what am I supposed to type? If I want the
character value '$', should I type '$' or should I type the decimal
value of '$' (36 for ASCII-based character sets)? Your call
``scanf("%d",&ch)'' is wrong in either case. Even if you declared ch
as an unsigned char (which is what the program asks for), it would
still be wrong.

Decide what you're trying to do; then we can help you do it.
 
M

Martin Ambuhl

asit said:
plz fix the bug...

#include <stdio.h>

int main()
{
char ch;
char i;
printf("Enter an unsigned char : ");

Without a 'fflush(stdout);' you have no expectation that your prompts on
stdout will be properly synchronized with your input on stdin.
scanf("%d",&ch);

ch is a char (and might be unsigned); "%d" is the specifier for reading
signed ints. The scanf line above is just gibberish.
printf("You have entered : %ud\n",ch);

ch is a char (and might be signed); "%u" is the specifier for unsigned
ints. While ch will be promoted to the right width, its signedness may
or may not be right. The "d" after "%u" is just a plain text 'd' and
has nothing to do with printing the value of ch.
printf("binary representation...\n");
for(i=sizeof(char)*8-1;i>=0;i--)

sizeof(char) is 1 by definition, so is just typing exercise.
The multiplier '8' is a magic number that need not have anything to do
with the representation of ch in binary. You may have meant something like
for (i = CHAR_BIT-1; i >= o; i--)
but if char is unsigned, i >= 0 will always be true. You could always use
for (i = CHAR_BIT; i > 0; i--)
and changed the next line
((1<<i)&ch) ? printf("1 "): printf("0 ");
to
putchar ( ( (1 << (i - 1)) & ch) ? '1' ? '0');
printf("\n");
return 0;
}

when i run it, if 130 is entered..it should show 130...but its not
showig ???
and also why the error segmentation fault comes ??

Because you are trying to store an int into a char. Unless sizeof(int)
== 1, this is an obvious error.
 
A

arsir walker

plz fix the bug...

#include <stdio.h>

int main()
{
        char ch;
        char i;
        printf("Enter an unsigned char : ");
        scanf("%d",&ch);
        printf("You have entered : %ud\n",ch);
        printf("binary representation...\n");
        for(i=sizeof(char)*8-1;i>=0;i--)
                ((1<<i)&ch) ? printf("1 "): printf("0 ");
        printf("\n");
        return 0;

}

when i run it, if 130 is entered..it should show 130...but its not
showig ???
and also why the error segmentation fault comes ??

"printf("You have entered : %ud\n",ch);" is not right.beause ch is a
signed character,it is prompted to signed int,then to unsigned int.So
it shows 0Xffffff82(130=0X82).
 
M

Martin Ambuhl

arsir said:
[...]
"printf("You have entered : %ud\n",ch);" is not right.beause ch is a
signed character,it is prompted to signed int,then to unsigned int.So
it shows 0Xffffff82(130=0X82).

That is not right. ch is a char, not a character, and it _may_ be
signed, but could as easily be unsigned.
If you are going to use a char to store a value treated as an integer
(rather than as an encoding of a character) you should always declare it
explictly as signed or unsigned. You should never rely on the
signedness of a plain char. The original poster, by the way, in his for
loop
>> char i; [...]
>> for(i=sizeof(char)*8-1;i>=0;i--)
depeneds on a char being a signed char, and in his printf statement
depends on its being an unsigned char. One of those assumptions is right
on any particular implementation, but not both, and we can't tell which
one is right.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top