468,457 Members | 1,620 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

char format specifier and segmentation fault

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 ??
Jan 12 '08 #1
8 3573
asit said:
plz
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.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jan 12 '08 #2
On Jan 12, 7:26*am, asit <lipu...@gmail.comwrote:
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
Jan 12 '08 #3
On Jan 12, 2:37 pm, suresh shenoy <msureshshe...@gmail.comwrote:
On Jan 12, 7:26 am, asit <lipu...@gmail.comwrote:
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.
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.
Jan 12 '08 #4
vi******@gmail.com wrote:
On Jan 12, 2:37 pm, suresh shenoy <msureshshe...@gmail.comwrote:
>On Jan 12, 7:26 am, asit <lipu...@gmail.comwrote:
>>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.
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.
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).

More important is IMHO to replace 8 with CHAR_BIT and #include <limits.h>
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.

Jan 12 '08 #5
asit <li*****@gmail.comwrites:
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.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jan 12 '08 #6
asit wrote:
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.

Jan 12 '08 #7
On Jan 12, 8:26*pm, asit <lipu...@gmail.comwrote:
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).
Jan 13 '08 #8
arsir walker wrote:
On Jan 12, 8:26 pm, asit <lipu...@gmail.comwrote:
[...]
> char ch;
[...]
"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.

Jan 13 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Vishal Ladha | last post: by
10 posts views Thread by yogeshmk | last post: by
17 posts views Thread by dtschoepe | last post: by
1 post views Thread by brenda24 | last post: by
14 posts views Thread by rtillmore | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.