answersLogoWhite

0


Best Answer

Yes, but the results may not be what you expect, depending on the relative sizes of an int and a char (in bytes), whether they are signed or unsigned, and whether they use big-endian notation or not.

By way of an example, in C++ an int is typically 4 bytes long while a char is 1 byte long. Both are signed by default. If you were to loop an integer from 0x80000000 to 0x7fffffff (-2147483648 to +2147483647) using big-endian notation and assign the value to a signed char, then the char would repeatedly cycle through the values 0 to 127, then -128 to 0. This is because the char takes on the value of the least-significant byte of the integer in big-endien notation (the bytes are effectively reversed in memory). However, if you cycle the int from 0xffffff80 to 0x7f (-128 to +127), then you get the expected behaviour (the char cycles from -128 to +127). However, if the char were unsigned, then the first loop would repeatedly cycle from 0 to 255 while the second would cycle from 128 to 255, and then from 0 to 127.

Thus to get the expected behaviour, both the int and char must both be signed or both must be unsigned, and the range of values must be within the range of the smaller of the two types (which will typically be the char), and the system must either use big-endian notation (reverse notation) or must otherwise compensate for little-endian notation.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

Not directly. But assuming the String is made up of digits, you can use methods to convert a String to an int, for example, Integer.parseInt().


Not directly. But assuming the String is made up of digits, you can use methods to convert a String to an int, for example, Integer.parseInt().


Not directly. But assuming the String is made up of digits, you can use methods to convert a String to an int, for example, Integer.parseInt().


Not directly. But assuming the String is made up of digits, you can use methods to convert a String to an int, for example, Integer.parseInt().

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

To do this, a number has to be converted to a sring representation. Some languages allow this to be done automatically, some require an explicit conversion. In Java, for example, it is enough to concate a number with a string:

int someNumber = 5;

String myString = "" + someNumber;

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

In C when assigning an integer variable to String variable a compilation error occurs as Incompatible Type Conversion.
So that integers cannot get assigned to String variables.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Not directly. But assuming the String is made up of digits, you can use methods to convert a String to an int, for example, Integer.parseInt().

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can int value be assigned to char variable?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Can you assigned value of int to a ordinary pointer?

Yes, with type-cast (but I don't see why you should): char *ptr = (char *)300;


Can an short value be assigned to int variable?

Yes, but you may cause truncation error because the short variable does not necessarily have the same range as an int variable. Most modern compilers will flag this as a warning. If you know that the value of the int variable will not exceed the range of a short variable, you can explicitly prevent the warning with a typecast, i.e. someShort = (short) someInt; and the compiler will assume that you know what you are doing.


What is the difference between initialisation and definition in C programming?

Variable-declaration is: extern int x; extern double y; extern char a; Variable-definition is: int x; static double y; auto char a; Variable-definition with initialization is: int x = 1; static double y= 2.3; auto char a = 'w';


How do you calculate ascii value in java?

char a = 'A'; System.out.println((int)a);


How character data type accept int?

An int and a char are both integral types such that a char is always guaranteed to be within the range of an int, because an int is at least as long as a short which is at least as long as a char (in bits). Converting the other way, from int to char, is not guaranteed to work, but we can guard against this by testing the int value is within the required range prior to conversion.The reason we use an int as opposed to a char in certain cases is because an int can represent values that a char cannot. This is useful in functions which would normally return a char value, but where we also need to cater for other values. Those other values could be used to indicate an error condition, for instance.

Related questions

Can you assigned value of int to a ordinary pointer?

Yes, with type-cast (but I don't see why you should): char *ptr = (char *)300;


What is the types of data?

int, but can be assigned to a short, long or char as well


Can you make an integer pointer variable to char data?

Of course. But why? int *p = (int *)"string";


What is the data type of 5?

int, but can be assigned to a short, long or char as well


How do you convert a char to an int in java?

Assuming - char mychar ; and int myint have been properly declared, myint = (int) mychar; // converts This is a feature of Java= to change types, put the type you want to convert into in parenthes before the variable that stores the converted value. ^Will convert your char into an int, but it will be the ascii value. For example, mychar = 3; myint = (int) mychar; //returns 51 To make an accurate one that will not return an error, you can use a try-catch statement Example char mychar = '3'; try { int myint = (int) mychar; }catch(NumberFormatException e) { //Do whatever you want with it! }


What will happen if a char is counted as an int in C plus plus?

The main difference is that int values are treated as being integers whereas char values are treated as being character codes. Thus if you output a char with value 65 you will get the symbol 'A', but if you output an int with the value 65 you will get the value 65 instead. In order to output the symbol 'A' you would have to cast the int to a char.


Can an short value be assigned to int variable?

Yes, but you may cause truncation error because the short variable does not necessarily have the same range as an int variable. Most modern compilers will flag this as a warning. If you know that the value of the int variable will not exceed the range of a short variable, you can explicitly prevent the warning with a typecast, i.e. someShort = (short) someInt; and the compiler will assume that you know what you are doing.


What is the difference between initialisation and definition in C programming?

Variable-declaration is: extern int x; extern double y; extern char a; Variable-definition is: int x; static double y; auto char a; Variable-definition with initialization is: int x = 1; static double y= 2.3; auto char a = 'w';


How do you calculate ascii value in java?

char a = 'A'; System.out.println((int)a);


Why only three types of pointer variable in c language?

I have no idea what you mean by that... Some examples for pointers of different types: int *intptr; char *charptr; void *generic_ptr; FILE *stdin; int (*funptr)(int, char **);


How character data type accept int?

An int and a char are both integral types such that a char is always guaranteed to be within the range of an int, because an int is at least as long as a short which is at least as long as a char (in bits). Converting the other way, from int to char, is not guaranteed to work, but we can guard against this by testing the int value is within the required range prior to conversion.The reason we use an int as opposed to a char in certain cases is because an int can represent values that a char cannot. This is useful in functions which would normally return a char value, but where we also need to cater for other values. Those other values could be used to indicate an error condition, for instance.


How do you convert a char to and int in c plus plus but keep the value of the char so if the char was 3 the int would also be 3?

Formally, you cannot do this. A char containing a character value has a different bit configuration than an int. Attempting to convert between the two is non-portable. However, in the ASCII character set, the character '0' has the value 48, or 0x30. If you subtract 48 from the char you will get the int value, but only if the char was one of the digits '0' through '9'. It is better to use the library routines to convert, such as atoi and sscanf, because they will give you predictable, portable results.