Thread: Int vs Char

  1. #1
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110

    Int vs Char

    Learning C


    What is the difference between:

    Int t[80]; & char t[80];

    and when do I need to use int t[80];

    Thanks........

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    int's are for holding numbers, and char's are for holding characters. It does get a little more complicated, but in essence, that's it.

    The brackets [] represent an array (in your case, size 80).

    I presume you're following a course or reading a tutorial? Either way, this type of thing is covered pretty early on... Try a web search for c data types if you're having problems.... or post a specific question here.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    Thanks Hammer..........


    But what confuses me is how come you can store integers mixed with characters in a char array???

  4. #4
    Unregistered
    Guest
    The reason you can store both types in a char array is because every char is actually an int, and the int specifies exactly what type of char it is. If you've ever heard of ASCII codes, then you know what I mean. Each character symbol has a corresponding int that represents it.

  5. #5
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    That make perfect sense....so why do we need to use int array???

  6. #6
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by Max
    That make perfect sense....so why do we need to use int array???
    char is not exactly a type of int , they are both integral types , a char typically holds less values ,
    in my complier , the range of char is from 0 -255 while that of int is from -2147483647-1 to 2147483647 , you can have a look at the file limits.h to get an idea in your implementation
    The one who says it cannot be done should never interrupt the one who is doing it.

  7. #7
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    Thank You all....that was enlightning

  8. #8
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by Max
    That make perfect sense....so why do we need to use int array???
    char is not exactly a type of int , they are both integral types , a char typically holds less values ,
    in my complier , the range of char is from 0 -255 while that of int is from -2147483647-1 to 2147483647 , you can have a look at the file limits.h to get an idea in your implementation
    The one who says it cannot be done should never interrupt the one who is doing it.

  9. #9
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    deja vu?

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Here is what it boils down to. A char is an 8 bit number, on windows an int is 32 bit. A long int is 32 bits and a short is 16 bits. Here is one way of looking at it (using unsigned versions of each), if you want to know the limitations of that storage type just do (2^(number of bits/3))-1. Like an unsigned char can hold up to 255, an unsigned short can hold up to 65536 and on unsigned long can hold up to 4294967295. On a side note, floats are 32bit and doubles are 64.

  11. #11
    Unregistered
    Guest

    Question

    how to count upper and lower case?

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Unregistered
    how to count upper and lower case?
    What has this got to do with this thread??!!

    Anyways,
    >#include <ctype.h>
    Then use isupper() and islower().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by ygfperson
    deja vu?
    Screwed up browser !!!

  14. #14
    Registered User
    Join Date
    Dec 2001
    Posts
    40
    An int is for storing a number, which you can subsequently use in calculations if you want to. A char is for holding characters or alphanumerics. You cannot do calulations on numbers stored as a char without doing a type conversion (also referred to as casting or type casting). Why would you want to do this? You may want to store someones phone number for example. This consists of all numbers, but here in the UK all phone numbers commence with zero. If you stored this as an integer you would lose the leading zero, so you must store it as a string of characters. And the way that C implements a string is as an array of characters eg char[11].

  15. #15
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by clancyPC
    An int is for storing a number, which you can subsequently use in calculations if you want to. A char is for holding characters or alphanumerics. You cannot do calulations on numbers stored as a char without doing a type conversion (also referred to as casting or type casting). Why would you want to do this? You may want to store someones phone number for example. This consists of all numbers, but here in the UK all phone numbers commence with zero. If you stored this as an integer you would lose the leading zero, so you must store it as a string of characters. And the way that C implements a string is as an array of characters eg char[11].
    You are wrong , you can add,subtract two characters ,in fact you can do all things you can do with integers , characters are like integers with a different range of values .
    The one who says it cannot be done should never interrupt the one who is doing it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03: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, 08:36 PM
  3. Replies: 14
    Last Post: 06-28-2006, 01: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, 01:27 PM