With an explicit cast, for example (in Java):
int i = 0;
char c;
c = (char) i;
Please note that data may be lost in such a conversion; the explicit cast basically tells the compiler "go ahead; I know what I am doing". Without an explicit cast, the compiler won't accept the conversion.
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.
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! }
pick one: int main (void); int main (int argc, char **argv); int main (int argc, char **argv, char **envp);
minimalist: int main (void); standard: int main (int argc, char **argv); unix-only: int main (int argc, char **argv, char **envp);
in stdio.h:extern int printf (const char *fmt, ...);
For a 'C' program, the main routine must be on of these:int main (void)int main (int argc, char ** argv)int main (int argc, char ** argv, char **envp) /* on some platforms */
int main (void) or int main(int a, char **p)
Old: function (par1, par2) int par1; char *par2; {...} New: int function (int par1, char *par2) {...}
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.
Because we usually don't call it from the program, but if we do, you should have a prototype: int main (int argc, char **argv); int foobar (const char *progname) { char *param[2]; ... param[0]= progname; param[1]= "help"; main (2, param); ... } int main (int argc, char **argv) { ... foobar (argv[0]); ... }
int x;float x;char x;double x;long x;long long x;short x;unsigned int x;unsigned float x;unsigned double x;signed char x;unsigned long x;unsigned long long x;int *x;float *x;char *x;double *x;long *x;long long *x;int x[100];typedef struct rect {int left;int top;int right;int bottom;};int main(int argv, char* argc[]) {return 0;}That enough for you?Well, these are definitions, declarations are like these:extern char x;int main (int argc, char **argv);
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.