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 ?? 8 3838
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
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
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. 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.
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"
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.
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).
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Vishal Ladha |
last post by:
Hi !
I have been experimenting with char * for a while now. I have two pieces of
code :
Code1 :
=====
char *ptr = "hello";
|
by: sieg1974 |
last post by:
Hi,
how can I compare one character of a string to a char?
I have tried this to compare the first character of directory to '/',
but when I run this program I got a segmentation fault.
void...
|
by: LaBird |
last post by:
Dear all,
I'd like to ask for the following C code segment:
#include <stdio.h>
int main()
{
char *a = "abc"; /* Line 1 */
printf("%s", a); /* Line 2 */
a = '1'; /* Line...
|
by: yogeshmk |
last post by:
I need to break a longer string (with strtok()) and store the smaller
strings in an array. since the number of small strings is not
fixed/known, I cannot use a declaration like char *format, so I...
|
by: ziroi |
last post by:
Hello!
I am new here to the forums and still learning C. I understand your policies on coursework, but I have an error that I've run around and around and can't figure out where my problem is...
|
by: dtschoepe |
last post by:
Hi,
I have a homework project I am working on, so be forwarned, I'm new to
C programming. But anyway, having some trouble with a memory
allocation issue related to a char * that is a variable...
|
by: brenda24 |
last post by:
Hi,
I have programmed in other languages too much, so i might be missing out something simple in C++. I'm using a library function that takes a (char*) parameter as its input. The problem is that...
|
by: brenda24 |
last post by:
...Seems to be impossible... Any solutions?
I have programmed in other languages too much, so i might be missing out something simple in C++. I'm using a library function that takes a (char*)...
|
by: rtillmore |
last post by:
Hello,
I did a quick google search and nothing that was returned is quite
what I am looking for. I have a 200 character hexadecimal string that
I need to convert into a 100 character string.
...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: erikbower65 |
last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal.
2. Connect to...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |