pattha
asked on
printing integer varaible through %s format
What is wrong with following code
int a = 102;
printf("%s",(char *)(&a));
above is printing nothing.
int a = 102;
printf("%s",(char *)(&a));
above is printing nothing.
ASKER
Hi sunnycode,
I agree with you.
But if i put a = 2pow(31) -1 which is maximum value i can put in a integer, then i think memory structure of a will be
0Xff ff ff ff.
But even in this case above code is printing nothing.
one more thing, can you tell me that why below code is wrong.
char *p = (char *)malloc(100);
*p = strlen("hello");
strcpy(p+1,"hello");
printf("%s",p);
I agree with you.
But if i put a = 2pow(31) -1 which is maximum value i can put in a integer, then i think memory structure of a will be
0Xff ff ff ff.
But even in this case above code is printing nothing.
one more thing, can you tell me that why below code is wrong.
char *p = (char *)malloc(100);
*p = strlen("hello");
strcpy(p+1,"hello");
printf("%s",p);
But if i put a = 2pow(31) -1 which is maximum value i can put in a integer, then i think memory structure of a will be
0Xff ff ff ff.
If my memory serves me right, result of RHS is a float and not an int!!!
If your data types and format specifiers do not match, results are unpredictable
I cant see why you are mixing ints and chars and pointers around
0Xff ff ff ff.
If my memory serves me right, result of RHS is a float and not an int!!!
If your data types and format specifiers do not match, results are unpredictable
I cant see why you are mixing ints and chars and pointers around
What are you trying to do? Perhaps that info would help us guide you.
In general printing a binary integer as a string is not going to work very well.
Your example with char * p = slrlen("Hello"); suggests you think strings have a leading length byte.
they do in some old Pascal systems, but in C generally strings are zero-terminated, no length byte.
In general printing a binary integer as a string is not going to work very well.
Your example with char * p = slrlen("Hello"); suggests you think strings have a leading length byte.
they do in some old Pascal systems, but in C generally strings are zero-terminated, no length byte.
ASKER
Hi grg99,
I want to simulate pascal string in C.
i know that in C, string are '\0' terminated.
So i thought that in first byte of aray , i will put length of string and from next byte onwards, i will put actualu string.
so i coded like:
char *p = (char *)malloc(100);
*p = strlen("hello");
strcpy(p+1,"hello");
printf("%s",p);
but above code's output is
hello.
I am not able to understande why p is not storing length of string in 1st byte.
I want to simulate pascal string in C.
i know that in C, string are '\0' terminated.
So i thought that in first byte of aray , i will put length of string and from next byte onwards, i will put actualu string.
so i coded like:
char *p = (char *)malloc(100);
*p = strlen("hello");
strcpy(p+1,"hello");
printf("%s",p);
but above code's output is
hello.
I am not able to understande why p is not storing length of string in 1st byte.
ASKER
Hi sunny,
as i told above that i am trying to simulate pascal string in C.
while tryinig to do this, i came to just thinking of storing integer in char string and so i tried following code.
int a = 2pow(31) -1;
printf("%s",(char *)(&a));
one more thing in above assignment RHS is not a float , it is integer only.
i am woring on
SunOS au1646s 5.8 Generic_117000-01 sun4u sparc SUNW,Sun-Fire-15000
and in this platform, int is 4 byte long and above assignment assign
a= 2147483647
and if i print a with %d format, it is working fine.
I know that through above code, i am not achieving anything, but my question is why it is not working.
Can you provide a link of document, where i can get information about how computer access variable's memory area in printf command.
as i told above that i am trying to simulate pascal string in C.
while tryinig to do this, i came to just thinking of storing integer in char string and so i tried following code.
int a = 2pow(31) -1;
printf("%s",(char *)(&a));
one more thing in above assignment RHS is not a float , it is integer only.
i am woring on
SunOS au1646s 5.8 Generic_117000-01 sun4u sparc SUNW,Sun-Fire-15000
and in this platform, int is 4 byte long and above assignment assign
a= 2147483647
and if i print a with %d format, it is working fine.
I know that through above code, i am not achieving anything, but my question is why it is not working.
Can you provide a link of document, where i can get information about how computer access variable's memory area in printf command.
>I want to simulate pascal string in C.
But WHY do you want to form a Pascal string in C? All the C string routines expect a zero-terminated string. You can't use strcpy, strcat, or "%s" format to handle these strings. So why make one at all?
>i know that in C, string are '\0' terminated.
>So i thought that in first byte of aray , i will put length of string and from next byte onwards, i will put actualu string.
>so i coded like:
char *p = (char *)malloc(100);
*p = strlen("hello");
strcpy(p+1,"hello");
printf("%s",p);
>but above code's output is
> hello.
>I am not able to understande why p is not storing length of string in 1st byte.
It is putting the string length in the first byte, but the %s format thinks that first byte is jsut the first character of the string, so it tries to print it. But chr(5) in ASCII is a non-printing character, so you don't see it. printf merrily goes along and keeps printing characters until it hits a zero byte. Which it did.
But WHY do you want to form a Pascal string in C? All the C string routines expect a zero-terminated string. You can't use strcpy, strcat, or "%s" format to handle these strings. So why make one at all?
>i know that in C, string are '\0' terminated.
>So i thought that in first byte of aray , i will put length of string and from next byte onwards, i will put actualu string.
>so i coded like:
char *p = (char *)malloc(100);
*p = strlen("hello");
strcpy(p+1,"hello");
printf("%s",p);
>but above code's output is
> hello.
>I am not able to understande why p is not storing length of string in 1st byte.
It is putting the string length in the first byte, but the %s format thinks that first byte is jsut the first character of the string, so it tries to print it. But chr(5) in ASCII is a non-printing character, so you don't see it. printf merrily goes along and keeps printing characters until it hits a zero byte. Which it did.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi pattha,
>int a = 2pow(31) -1;
>printf("%s",(char *)(&a));
>one more thing in above assignment RHS is not a float , it is integer only.
pow returns a double ... double - int is double and storing it in an int will give you not so reliable results .. be careful!!!
If you wish to implement Pascal strings on your own, do them cleanly ... declare a struct
struct string_struct
{
int length;
char * str;
};
typedef it to your string data type and use this to perform all manipulations .... mixing around chars, ints and pointers is never a good idea ...
Hi Sid,
Long time no see ... where are you now-a-days
Cheers!
sunnycoder
>int a = 2pow(31) -1;
>printf("%s",(char *)(&a));
>one more thing in above assignment RHS is not a float , it is integer only.
pow returns a double ... double - int is double and storing it in an int will give you not so reliable results .. be careful!!!
If you wish to implement Pascal strings on your own, do them cleanly ... declare a struct
struct string_struct
{
int length;
char * str;
};
typedef it to your string data type and use this to perform all manipulations .... mixing around chars, ints and pointers is never a good idea ...
Hi Sid,
Long time no see ... where are you now-a-days
Cheers!
sunnycoder
ASKER
Hi sunny,
i think i have sent some wrong information due to my wrong way of writing.
actually assignment in my code is
int a = 2147483647;
printf("%s",(char *)(&a));
Earlier i written 2pow(31) -1 , just to show you clearly.
so problem came to back where it was.
and thanks for idea of declaring structure for pascal string.its really good, but it will be much helpful, if you can clear my idea
about why
printf("%s",(char *)(&a));
is not printing when memory structure of a should be 0x ff ff ff ff.
i think i have sent some wrong information due to my wrong way of writing.
actually assignment in my code is
int a = 2147483647;
printf("%s",(char *)(&a));
Earlier i written 2pow(31) -1 , just to show you clearly.
so problem came to back where it was.
and thanks for idea of declaring structure for pascal string.its really good, but it will be much helpful, if you can clear my idea
about why
printf("%s",(char *)(&a));
is not printing when memory structure of a should be 0x ff ff ff ff.
is ff a printable character? ... I dont think so ..
try this
char a = 0xff;
printf ("%c",a);
try this
char a = 0xff;
printf ("%c",a);
0xFF is not a printable character. It is a blank character in every ASCII set I've ever seen.
Hi pattha,
> printf("%s",(char *)(&a));
> is not printing when memory structure of a should be 0x ff ff ff ff.
I don't see where this journey should lead to. You shouldn't cast around pointer types. Even if by mere coincidence there are zeros after a's address (that's likely), it's completely arbitrary.
Cheers!
Stefan
> printf("%s",(char *)(&a));
> is not printing when memory structure of a should be 0x ff ff ff ff.
I don't see where this journey should lead to. You shouldn't cast around pointer types. Even if by mere coincidence there are zeros after a's address (that's likely), it's completely arbitrary.
Cheers!
Stefan
sunnycoder...
long time no see... mail me at yahoo:<E-mail id removed by sunnycoder, Page Editor>
Regards,
Sid
long time no see... mail me at yahoo:<E-mail id removed by sunnycoder, Page Editor>
Regards,
Sid
ints are generally 32 bits ... so a would be 0x00 00 00 XX
You cast it to char * an d try to print it ... %s would stop interpretation on first 00 ... remaining 3 bytes would be left in the stack ...
You should not be mixing data types around unless you are absolutely sure of what you are doing..
Cheers!
sunnycoder