473,408 Members | 1,904 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,408 software developers and data experts.

Is it possible to initialize char value into int value?

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int main()
  3. {
  4.     int j=printf("Hello.");
  5.     return 0;
  6.     exit(10);
  7. }
In the above program is it possible to initialize int j with char value "Hello."?
Oct 13 '12 #1
6 4875
weaknessforcats
9,208 Expert Mod 8TB
Yes it is. But there are a few considerations. One is that "Hello" is not an integer value. Instead it is an array of char. You would have to construct your intger value using your own code. If you use "12345" you could call atoi() in ctypes.h. This works because each character is an ASCII integer which is convertible to a binary interger. You just add up the converted values and there you are.

Remember a char is an integer just like an int. It's just smaller in size. The only reason it is called "char" is that it is supposed to be large enough to hold one character of the implementation set. In our case this is ASCII so your char is 8 bits.

Further, the library functions thst use char or char* arguments assume ASCII characters or a C-style string od ASCII characters.
Oct 13 '12 #2
whodgson
542 512MB
In C "Hello" is string.
You could visualize it as an array which ends with NULL ('\0')
int array[6]={H,e,l,l,o,\0};
If you cycled through that array with a for loop, you could develop a running total of the values of each character (excluding NULL) with something like:
Expand|Select|Wrap|Line Numbers
  1. int total=0,j=0,i=0;
  2. for(i=0;i<5;i++)
  3.    total=total+array[i];
  4. j=total;
Nov 10 '12 #3
donbock
2,426 Expert 2GB
Please clarify your intent.
As mentioned earlier, "Hello" is a string -- it does not have a char value. However, each individual character in the string (including the terminating null) has a character value.
Do you want j to be:
  1. The char value of the first letter of the string ('H')?
  2. The sum of the char values of all letters in the string (with or without the terminating null)?
  3. A count of the number of characters in the string (with or without the terminating null)?
  4. Something else?

By the way, the C Standard does not mandate any particular character encoding. That is, you cannot be completely certain what numeric value your compiler will use to encode a particular character. That said, ASCII encoding is very common.
Nov 12 '12 #4
weaknessforcats
9,208 Expert Mod 8TB
A char is just an integer.

The C standard says it has to be large enough to hold one symbol of the implementation character set. For the United States implementaton of ANSI C, the encoding is ASCII.

There are implementations of C based on other encodings.

So while you can add up the char values in a C string, the result is just some number.
Nov 12 '12 #5
int j = "HELLO" is forbidden since you are assigning string value to an integer type.

but
int j = 'a' is valid.

Since character has integral values associated with them, hence 'j' will have ascii value corresponding to 'a' which is 97.

For your knowledge... you can assign integral values to character type as well

For example: char a = 97 is legal.
After this 'a' will have the character whose ascii value is 97.
Nov 13 '12 #6
weaknessforcats
9,208 Expert Mod 8TB
@vikramjit:

int j = "HELLO" is forbidden since you are assigning string value to an integer type.


Not quite true. The name of an array is always the address of element 0. So the above is forbidden because you cannot assign an address (the address of the H) to an int.

To get the value of the H you would need to code:

Expand|Select|Wrap|Line Numbers
  1. int j = *"Hello";
Nov 13 '12 #7

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

Similar topics

4
by: Don | last post by:
Using PHP, I'm creating an <input> tag. How do you delimit the text string for the value parm of an <input> tag, when the string contains the delimit char? Example: <input...value="This text...
5
by: Aeon Twilight | last post by:
REFERENCE: http://us4.php.net/manual/en/function.include.php Example 16-7. include() through HTTP Example 16-10. include() and the return() statement -------------------- return.php...
4
by: rasika | last post by:
I have a structure typedef struct m { char *name, char **alais; }mystruct; The way I have initialised name field of the struct i want to intialise the alais field to { "geeta", NULL } char...
10
by: Yang Lee | last post by:
hi is this correct char teststr="\0"; or is there another method to initialise; thanks lee
2
by: msnews.microsoft.com | last post by:
Hi! In C++ you could easily intialize a char-variable with an integer value: char ch = 123; How can we accomplish this in C#? You can get the integer representation of a char value easily,...
2
by: Gidi | last post by:
Hi, I'm writing a C# win application program, and i need to transfer my hebrew letters from unicode to ascii, now if i use the ascii encoding it writes me ??? instead of the hebrew letter i've...
1
by: veblen.lee | last post by:
for an example: 'a' value 0x61 '1' value 0x31.
0
by: coolvivek33 | last post by:
hi i am facing a strange problem i am trying to build report using sql server 2005 and microsoft visual studio2005 now it is in requirement that i should seperate date from time.I have done that...
9
by: ashokd001 | last post by:
Hi How do i reinitialize a char array in one line if i want to use the same array variable every time. Ex- char name; -Ashok
1
by: prashk | last post by:
My page is using the Master page . I am using TabContainer ajax control in my aspx page and have a TabPanel that contains a RadioButtonList control and a text box. I want to show/hide populate...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.