Results 1 to 6 of 6

Thread: Why to use integers as loop counter variables rather than char or short?

  1. #1
    Join Date
    Aug 2009
    Posts
    56

    Why to use integers as loop counter variables rather than char or short?

    Hello to all,
    I am new to this forum. I recently started learning "c" language. While reading c-book I have read somewhere that it is better to use integers as loop counter variables rather than char or short. I don't know how. Can anyone tell me Why to use integers as loop counter variables rather than char or short? Please help me.
    Thank you.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Why to use integers as loop counter variables rather than char or short?

    Hey we use integers as loop counter variables rather than char or short because of fast access. Generally what happened that when you use integer type , c-compiler will convert int value into proper size for putting into one of your CPU's general purpose registers. When you use character c-compiler will convert char value into big size and it leads to slow execution.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Why to use integers as loop counter variables rather than char or short?

    Mostly what happened that in 32-bit architecture various operations related to int variables are usually perform faster. This is because speed of operation is totally depend on size of registers and memory alignment. But in 64 bit architecture the situation is different. In 64 bit architecture the int variable is automatically made 64-bit integer.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Why to use integers as loop counter variables rather than char or short?

    As per my information it is always better to use a type that's the same width as the architecture. It means if you are using 32 bit system then it is better to use int32 instead of other.
    For example if you use a char i.e.

    for(char k=0;k<max;++k)
    Make sure that keep size of "max" as low as possible.

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: Why to use integers as loop counter variables rather than char or short?

    When you use int type on your system then there are more chances of faster implementation. Most of the system use int type for fast implementation as default choice. Normally an integer typically reflecting the natural size of integers on the host machine and that's why speed is fast. The speed you get using int variable is not able to get using char or string variable.

  6. #6
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Why to use integers as loop counter variables rather than char or short?

    We use integers as loop counter variables rather than char or short for fast speed, because int type provide more speed than char and string. It also depends on what your loop is doing. Just try to understand following example. It very simple one.


    Code:
    typedefs structs _SomeDatas
    {
    
    } SomeDatas;
    
    void SomeFunctions (SomeDatas *array_of_datas, int number_of_elementss)
    {
      int k;
    
      for (k = 0 ; k < number_of_elements ; ++k)
      {
         DoSomethingWithDatas (&array_of_datas [k]);
      }
    }

Similar Threads

  1. 20" Apple Cinema Display blinking "short-long-short" power light
    By Alln.Do in forum Monitor & Video Cards
    Replies: 8
    Last Post: 22-12-2011, 10:13 PM
  2. How to use a loop function to reverse a counter string?
    By LostIt in forum Software Development
    Replies: 3
    Last Post: 08-08-2009, 07:54 PM
  3. Reading integers from a file into a character array
    By afidelino in forum Software Development
    Replies: 3
    Last Post: 08-08-2009, 11:32 AM
  4. JAVA- divide an array of integers into 2 parts
    By machok in forum Software Development
    Replies: 3
    Last Post: 18-03-2009, 02:20 PM
  5. Problem printing like integers in C# ARRAYS
    By Lambard in forum Software Development
    Replies: 4
    Last Post: 26-01-2009, 04:55 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,909,262.96747 seconds with 17 queries