- #include<stdio.h>
-
int main()
-
{
-
char arr[3]={0,2,3,4,5,6,7};
-
printf("%d ",sizeof(arr));
-
printf("%c ", arr[0]);
-
printf("%c ", arr[1]);
-
printf("%c ", arr[2]);
-
printf("%c ", arr[3]);
-
return 0;
-
}
In this code 2 and 3 would be acting like ASCII values so then these are 4 byte integer values so when we will extract 1 byte from it since its a character array so then how come it is not printing null character for each array index value ?
sorry, I don't understand I tried this, and output seemed correct (and logical) to /me -
~/tmp> cat test.c
-
#include <stdio.h>
-
-
void main()
-
{
-
printf("%c %c %c", 2, 3, 4);
-
}
-
~/tmp> make test
-
cc test.c -o test
-
~/tmp> ./test | hexdump -C
-
00000000 02 20 03 20 04 |. . .|
-
00000005
-
~/tmp>
-
10 1686 Luuk 1,047
Expert 1GB
First thing you should do is get the errors out - cc lala.c -o lala
-
lala.c: In function ‘main’:
-
lala.c:5:2: warning: excess elements in array initializer [enabled by default]
-
char arr[3]={0,2,3,4,5,6,7};
-
^
-
lala.c:5:2: warning: (near initialization for ‘arr’) [enabled by default]
-
lala.c:5:2: warning: excess elements in array initializer [enabled by default]
-
lala.c:5:2: warning: (near initialization for ‘arr’) [enabled by default]
-
lala.c:5:2: warning: excess elements in array initializer [enabled by default]
-
lala.c:5:2: warning: (near initialization for ‘arr’) [enabled by default]
-
lala.c:5:2: warning: excess elements in array initializer [enabled by default]
-
lala.c:5:2: warning: (near initialization for ‘arr’) [enabled by default]
-
There is no such thing as promotion to a char.
Promotion means to make a smaller type into a bigger type. Like promotion a char (1 byte) to an int (4 bytes).
The %c and %d are printf format specifiers where you request the data to be displayed as a char or as an int.
In this case the int of the array is typecast by printf to a char. Most likely the least significant byte (LSB) of the int becomes the char. The remaining bytes of the int are lost.
So that's what I am trying to say that 0 , 2,3 these numbers are 4 byte value , hence these would be getting converted to 1 byte starting from LSB , so for arr[1]= 2 , op must be some character corresponding to ascii value 2 , but it is not printing anything , although it prints some character for ascii value 3 and 4 , so then ?
Remember, an int value of 2 is not an ASCII value of 2. A char '2' would be an int 50.
Luuk 1,047
Expert 1GB
A char '2' will only be an int 50 if it's specified in decimal notation.
A '2' is always 50.
The %c tells printf you have an ASCII value so the 50 will be displayed as 2. - char x = 50;
-
printf("%c, %d\n", x, x);
this will display 2,50.
If I do like
printf("%c %c" , 3, 4);
Then I am getting the character value corresponding to int 3 and 4 , and that is quite obvious also since we are chosing LSB's so why not for 2 I am getting this output , that's the confusion .
Luuk 1,047
Expert 1GB
sorry, I don't understand I tried this, and output seemed correct (and logical) to /me -
~/tmp> cat test.c
-
#include <stdio.h>
-
-
void main()
-
{
-
printf("%c %c %c", 2, 3, 4);
-
}
-
~/tmp> make test
-
cc test.c -o test
-
~/tmp> ./test | hexdump -C
-
00000000 02 20 03 20 04 |. . .|
-
00000005
-
~/tmp>
-
Forget LSB, byte alignment, conversion algorithms, etc...
%c tells printf to display the ASCII value (what is called the SYMBOL of the argument) if the value is between 32 and 127. Values outside this range are not printable ASCII values and are displayed as is and usually appear as weird characters.
It does not matter what the type of the argument is as long as it is an integer. char and int are both integers. Both behave the same with %c.
Thankss a lot , that was a great explanation now which actually cleared my confusion , I understood the concept now , thankss once again .
Sign in to post your reply or Sign up for a free account.
Similar topics
by: tvn007 |
last post by:
Hi,
I tried to convert string data type to char type so
that I can use strtok.
I have used c_str(). However, still getting error message
when compile .
Any help would be appreciate.
|
by: lovecreatesbeauty |
last post by:
There is a warning/(error? I remember it is an error for line 10 on
some compilers before. At least on g++, it is an error.) for line 10. I
first read a similar example from `Expert C Programming...
|
by: Jim Langston |
last post by:
I'm using a function like this:
char TextBuffer;
jGet_DropDown_Selected_Text( cc.ddSex, TextBuffer);
Where the function is filling in the text buffer. I don't have access to
the actual...
|
by: pallav |
last post by:
I'm using an old sparse matrix C library in my C++ code and I'd like
to know how to downcast a boost::shared_ptr to char *. The sparse
matrix data structure is like this:
struct...
|
by: kenneth6 |
last post by:
I am writing sth about data in string type in console mode.
now, I transform it to Windows Form Application, i know managed type should be used here instead of the unmanaged type. should I change...
|
by: kenneth6 |
last post by:
i am now having:
string* book=new string; // string array
in Windows form application (managed code), i am unable to user it !
how can change it to char type with the same effect and...
|
by: Anna |
last post by:
I try to put 8 int bit for example 10100010 into one character of type
char(1 octet) with no hope . Could anyone propose a simple way to do
it? Thank you very much.
|
by: prasad8619 |
last post by:
please help me out in this regard as this is urgent to me because i stuck in the middle of a program.......
here I have a program like this......
char name="rajesh";
char...
|
by: DomoChan |
last post by:
the code below will compile in visual c++ 2003, but im not sure its
valid.
unsigned char myString = "";
after this line executes, all the bytes within myString are indeed set
to '0's' but is...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |