473,322 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

How does an int gets type promoted to a char ?

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int main()
  3. {
  4. char arr[3]={0,2,3,4,5,6,7};
  5. printf("%d  ",sizeof(arr));
  6. printf("%c ", arr[0]);
  7. printf("%c ", arr[1]);
  8. printf("%c ", arr[2]);
  9. printf("%c ", arr[3]);
  10. return 0;
  11. }
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 ?
Sep 13 '15 #1

✓ answered by Luuk

sorry, I don't understand I tried this, and output seemed correct (and logical) to /me

Expand|Select|Wrap|Line Numbers
  1. ~/tmp> cat test.c
  2. #include <stdio.h>
  3.  
  4. void main()
  5. {
  6.         printf("%c %c %c", 2, 3, 4);
  7. }
  8. ~/tmp> make test
  9. cc     test.c   -o test
  10. ~/tmp> ./test | hexdump -C
  11. 00000000  02 20 03 20 04                                    |. . .|
  12. 00000005
  13. ~/tmp>
  14.  

10 1686
Luuk
1,047 Expert 1GB
First thing you should do is get the errors out
Expand|Select|Wrap|Line Numbers
  1. cc     lala.c   -o lala
  2. lala.c: In function ‘main’:
  3. lala.c:5:2: warning: excess elements in array initializer [enabled by default]
  4.   char arr[3]={0,2,3,4,5,6,7};
  5.   ^
  6. lala.c:5:2: warning: (near initialization for ‘arr’) [enabled by default]
  7. lala.c:5:2: warning: excess elements in array initializer [enabled by default]
  8. lala.c:5:2: warning: (near initialization for ‘arr’) [enabled by default]
  9. lala.c:5:2: warning: excess elements in array initializer [enabled by default]
  10. lala.c:5:2: warning: (near initialization for ‘arr’) [enabled by default]
  11. lala.c:5:2: warning: excess elements in array initializer [enabled by default]
  12. lala.c:5:2: warning: (near initialization for ‘arr’) [enabled by default]
  13.  
Sep 13 '15 #2
weaknessforcats
9,208 Expert Mod 8TB
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.
Sep 14 '15 #3
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 ?
Sep 14 '15 #4
weaknessforcats
9,208 Expert Mod 8TB
Remember, an int value of 2 is not an ASCII value of 2. A char '2' would be an int 50.
Sep 15 '15 #5
Luuk
1,047 Expert 1GB
A char '2' will only be an int 50 if it's specified in decimal notation.
Sep 15 '15 #6
weaknessforcats
9,208 Expert Mod 8TB
A '2' is always 50.

The %c tells printf you have an ASCII value so the 50 will be displayed as 2.

Expand|Select|Wrap|Line Numbers
  1. char x = 50;
  2. printf("%c, %d\n", x, x);
this will display 2,50.
Sep 15 '15 #7
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 .
Sep 15 '15 #8
Luuk
1,047 Expert 1GB
sorry, I don't understand I tried this, and output seemed correct (and logical) to /me

Expand|Select|Wrap|Line Numbers
  1. ~/tmp> cat test.c
  2. #include <stdio.h>
  3.  
  4. void main()
  5. {
  6.         printf("%c %c %c", 2, 3, 4);
  7. }
  8. ~/tmp> make test
  9. cc     test.c   -o test
  10. ~/tmp> ./test | hexdump -C
  11. 00000000  02 20 03 20 04                                    |. . .|
  12. 00000005
  13. ~/tmp>
  14.  
Sep 15 '15 #9
weaknessforcats
9,208 Expert Mod 8TB
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.
Sep 15 '15 #10
Thankss a lot , that was a great explanation now which actually cleared my confusion , I understood the concept now , thankss once again .
Sep 16 '15 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

6
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.
1
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...
4
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...
2
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...
1
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...
2
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...
14
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.
4
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...
24
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...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
1
isladogs
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...
0
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...
1
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
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...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.