Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple c Character manipulation question

Status
Not open for further replies.

halfbarrel

Programmer
Jun 12, 2002
44
US
Hello,
I was wondering if someone could kind of tell me what the second line bellow would be outputting:

x=(char) *myinputval++; /* get input char */
x=x * x;

The first line I know pulls just a character for example 'P', but I don't understand multiplying a character by itself. What would this output. Is it hex or something? Thanks for any help I am new to the c world and have been trying to print the value out with printf("%c", x) to see what is there, catting the output to a file, and hexdumping it, but I think it would be alot of help if someone would just explain this one to me.
Thanks again,

Chris.
 
That will be squaring the ASCII value for the character. I don't see any practical use unless this is used in some kind of encryption scheme.

For example, a capital 'A' has a decimal ASCII value of 65. Squaring that gets a value that has no ASCII equivalent (decimal 4225).

Hope this helps.
 
A char type in C is an integral type, i.e. integer (very short;). It's not a special character type with special operators. The only subtle point: char value may be signed or unsigned (it's implementation-dependent issue). For example:
Code:
char ch = '\xFF';
int chval = ch;
printf("%d\n",chval);
May print -1 or 255.
So be careful...
 
I think the first line stops the compile so the
second line outputs nothing

tomcruz.net
 
the values for that line are different
depending upon the type of "x".

Code:
    char aaa [] = "321";
    char *myinputval  = aaa;
    int x;
    x=(char) *myinputval++;   /* get input char */    
    x=x*x;

Code:
    char aaa [] = "321";
    char *myinputval  = aaa;
    char x;
    x=(char) *myinputval++;   /* get input char */    
    x=x*x;

now what are we doing ;-)

tomcruz.net
 
if x is a char and x = 'A' (what is 65 in integer)

we will get:

x = 65 * 65 = 4225 = 0x1081 (hex) = 0x81 (char in hex) = 129 (unsigned char) = -127 (char) = some special extended ASCII code.

When you do it, do it right.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top