char to int

This is a discussion on char to int within the C Programming forums, part of the General Programming Boards category; How is it possible to convert a letter in char to int...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    29

    char to int

    How is it possible to convert a letter in char to int

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    cast it - and you get the ascii decimal value for the character:

    Code:
    #include <stdio.h>
    
    int main ()
    {
    	int letter = 'a';
    
    	printf ("The ascii value of %c is %d\n", letter, (int)letter);
    	getchar ();
    	return 0;
    }
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    thanks but the thing i want to do is to take a charactor out of a char and convert it to int so i can multply it eg.

    char test[20] = "12345678";

    now i want to multiply 8 with 5 and place it into a variable called

    int lastnumber

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    ah, different story, you were on about arrays... there are ways to
    convert between ascii numeral values and actual ints. your compiler may have a function called atoi - i cant honestly
    remember if it is standard code or not, but here's a link about it:

    FAQ atoi does it work like this (C)
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  5. #5
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    The header <stdlib.h> declares functions for number conversion, storage allocation, and similar tasks.
    double atof(const char *s)
    atof converts s to double; it is equivalent to strtod(s, (char**)NULL).

    int atoi(const char *s)
    converts s to int; it is equivalent to (int)strtol(s, (char**)NULL, 10).

    long atol(const char *s)
    converts s to long; it is equivalent to strtol(s, (char**)NULL, 10).

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    niceee.. it worked very well.. thank u very much.. u guys are the best

  7. #7
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    If your goal is to just convert a single digit, then an easier more portable method would be to subtract '0' from it, for example:

    Code:
    char test[20] = "12345678"
    int lastdigit = test[7]-'0';
    int firstdigit = test[0]-'0';
    ..etc

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    14

    help!

    in my case i need to convert an input string to a #..
    for example input a month..
    Code:
    printf("input month");
    scanf("%s",month");
    if (month==january)
    month=1;
    ive try dis 1 but its an error..
    hope you can help me..
    thanks

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Use strcmp to compare strings. Visit the FAQ. Reread your reference on basic syntax.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 04:45 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 09:36 PM
  3. Replies: 14
    Last Post: 06-28-2006, 02:58 AM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21