8 Replies - 574 Views - Last Post: 09 August 2012 - 10:15 AM Rate Topic: -----

#1 m*pasha  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 17-July 12

what is difference between int and intN_t?

Posted 08 August 2012 - 12:35 PM

hi i am trying to write a program that use flip and add rule(23+32=55, a palindrome no.) to find palindrome number of a number input by user.the following code doesn't work even if we use 'long' or 'long long' .But it works with a fixed width data type like 'int64_t',moreover even with 64bits code will be unable to calculate palindrome of '196' and '295' as these numbers doesn't give palindromes even after millions of iterations.I am really confused with fixed width data types,can someone please explain it.
please suggest how this program can be written correctly in c.Below is my code.

#include<stdio.h> /*program finds a palindrome number using number input by user*/
#include<inttypes.h> 
int64_t reverse(int64_t);
int main(){
int64_t num = 0,sum = 0,rever = 0,i = 0;
printf("enter the number to get a palindrome number\n");
scanf("%"SCNd64"",&num);
while(num!=rever){
i+=1;
num = num + rever;
printf("...");
rever = reverse(num);
}
i--;
printf("\nnumber gets palindromic:% "PRId64" in %"PRId64" rounds",num,i);
getchar();
return 0;
}
int64_t reverse(int64_t num){
int64_t rev = 0;
while(num>0){
rev = (rev*10)+(num%10);
num = num/10;
}
return rev;
}


Is This A Good Question/Topic? 0
  • +

Replies To: what is difference between int and intN_t?

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3449
  • View blog
  • Posts: 10,643
  • Joined: 05-May 12

Re: what is difference between int and intN_t?

Posted 08 August 2012 - 01:08 PM

Those integer types with the numbers indicate the number of bits within the integer. The more bits the bigger the range of values the numbers can store.

I think that you are running into an integer overflow situation when using the plain old integer, as most modern compilers default to 32 bits. When you used the 64 bit integer, you managed to get a bit further, but still eventually overflow an integer.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 3987
  • View blog
  • Posts: 12,298
  • Joined: 25-December 09

Re: what is difference between int and intN_t?

Posted 08 August 2012 - 01:13 PM

You also need to desperately find an indentation style you like and use it consistently. The way your code is now formatted makes reading your code a chore. By using proper indentation you will make reading and trouble shooting your code much easier.

What compiler are you using?

Jim
Was This Post Helpful? 0
  • +
  • -

#4 m*pasha  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 17-July 12

Re: what is difference between int and intN_t?

Posted 08 August 2012 - 08:46 PM

I forgot to mention that program works with 'long long' ,which should beat least 64 bits as per c99 standardswide,but eventually integer overflow leads to infinite loop.But when we use int64_t program doesn't
run in to an infinfite loop,but gives some incorrect palindrome number.So what change does fixed width(64bit)
make? You can consider this as my real question.

View Postjimblumberg, on 08 August 2012 - 01:13 PM, said:

You also need to desperately find an indentation style you like and use it consistently. The way your code is now formatted makes reading your code a chore. By using proper indentation you will make reading and trouble shooting your code much easier.

What compiler are you using?

Jim

I am using GCC
Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1302
  • View blog
  • Posts: 4,484
  • Joined: 19-February 09

Re: what is difference between int and intN_t?

Posted 08 August 2012 - 10:46 PM

All the integers are fixed width.

They are going to overflow and repeat. I don't know why there is a difference with int64_t.



#ifdef __STDC__

#include <stdio.h>
#include <limits.h>

#else

#include <iostream>
#include <climits>
using namespace std;

#endif


#define myint       short
#define MYINTMAX    SHRT_MAX
#define MYINTSIZE   sizeof(short)
#define PRINTFORMAT "%d \n"

#define myuint       unsigned short
#define MYUINTMAX    USHRT_MAX
#define MYUINTSIZE   sizeof(unsigned short)
#define UPRINTFORMAT "%u \n"

#define INCREMENT    5000


int main()
{
  myint number = 0;
  printf("size=%d\n", MYINTSIZE);

  while(number != 500)
  {
    number += INCREMENT;
    printf(PRINTFORMAT, number);

    if(number == MYINTMAX) {
      printf("equal MAX \n");
      break;
    }
    if(number < INCREMENT) {
      printf("less than zero/initial \n");
      break;
    }
  }

  printf("----- \n");

  myuint unumber = 0;
  printf("size=%d\n", MYUINTSIZE);

  while(unumber != 500)
  {
    unumber += INCREMENT;
    printf(UPRINTFORMAT, unumber);

    if(unumber == MYUINTMAX) {
      printf("equal MAX \n");
      break;
    }
    if(unumber < INCREMENT) {
      printf("less than zero/initial \n");
      break;
    }
  }


  //cin.get();
  return(0);
}

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3449
  • View blog
  • Posts: 10,643
  • Joined: 05-May 12

Re: what is difference between int and intN_t?

Posted 09 August 2012 - 01:44 AM

On Windows, with both MSVC and g++ that comes with CodeBlocks, I'm getting consistent behavior between long long and int64_t:
int (32 bits)
89: Error: Integer overflow after 16 steps.
98: Error: Integer overflow after 16 steps.
177: Error: Integer overflow after 15 steps.
187: Error: Integer overflow after 15 steps.
196: Error: Integer overflow after 18 steps.
unsigned int (32 bits)
89: Error: Integer overflow after 17 steps.
98: Error: Integer overflow after 17 steps.
177: Error: Integer overflow after 15 steps.
187: Error: Integer overflow after 16 steps.
196: Error: Integer overflow after 18 steps.
long (32 bits)
89: Error: Integer overflow after 16 steps.
98: Error: Integer overflow after 16 steps.
177: Error: Integer overflow after 15 steps.
187: Error: Integer overflow after 15 steps.
196: Error: Integer overflow after 18 steps.
unsigned long (32 bits)
89: Error: Integer overflow after 17 steps.
98: Error: Integer overflow after 17 steps.
177: Error: Integer overflow after 15 steps.
187: Error: Integer overflow after 16 steps.
196: Error: Integer overflow after 18 steps.
long long (64 bits)
196: Error: Integer overflow after 40 steps.
unsigned long long (64 bits)
196: Error: Integer overflow after 41 steps.
int64_t (64 bits)
196: Error: Integer overflow after 40 steps.
uint64_t (64 bits)
196: Error: Integer overflow after 41 steps.
BigNum (300 digits)
196: Error: Integer overflow after 702 steps.



Here is the code I wrote to test things out. (Please excuse the inefficiencies in the BigNum class. It was something I just put together as something quick and dirty.)
Spoiler

This post has been edited by Skydiver: 09 August 2012 - 02:40 AM

Was This Post Helpful? 0
  • +
  • -

#7 m*pasha  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 17-July 12

Re: what is difference between int and intN_t?

Posted 09 August 2012 - 08:40 AM

View Post#define, on 08 August 2012 - 10:46 PM, said:

All the integers are fixed width.

They are going to overflow and repeat. I don't know why there is a difference with int64_t.



#ifdef __STDC__

#include <stdio.h>
#include <limits.h>

#else

#include <iostream>
#include <climits>
using namespace std;

#endif


#define myint       short
#define MYINTMAX    SHRT_MAX
#define MYINTSIZE   sizeof(short)
#define PRINTFORMAT "%d \n"

#define myuint       unsigned short
#define MYUINTMAX    USHRT_MAX
#define MYUINTSIZE   sizeof(unsigned short)
#define UPRINTFORMAT "%u \n"

#define INCREMENT    5000


int main()
{
  myint number = 0;
  printf("size=%d\n", MYINTSIZE);

  while(number != 500)
  {
    number += INCREMENT;
    printf(PRINTFORMAT, number);

    if(number == MYINTMAX) {
      printf("equal MAX \n");
      break;
    }
    if(number < INCREMENT) {
      printf("less than zero/initial \n");
      break;
    }
  }

  printf("----- \n");

  myuint unumber = 0;
  printf("size=%d\n", MYUINTSIZE);

  while(unumber != 500)
  {
    unumber += INCREMENT;
    printf(UPRINTFORMAT, unumber);

    if(unumber == MYUINTMAX) {
      printf("equal MAX \n");
      break;
    }
    if(unumber < INCREMENT) {
      printf("less than zero/initial \n");
      break;
    }
  }


  //cin.get();
  return(0);
}


Wikipediasays that int should be at least 16 bits and long at least 32 bits but of course it's implementation defined,and on latest systems int is 16 bits or 4 byte.But intN_t type allows integers to be stored in 8,16,32,64 bits it's not possible with int . Below is the code of what i got so far.
#include<stdio.h> 
/*integer stored in 1 byte of course it will overflow */
#include<stdint.h>

#include<inttypes.h>

#define INCREMENT 2

#define SIZE sizeof(number)

#define INTTYPE  uint8_t

int main()

{

INTTYPE number = 0;

while(number<127)

{

printf("number:%"PRId8",size:%d\n",number,SIZE);

number+=INCREMENT;

}

getchar();

return 0;

}


Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 3987
  • View blog
  • Posts: 12,298
  • Joined: 25-December 09

Re: what is difference between int and intN_t?

Posted 09 August 2012 - 09:07 AM

Quote

Wikipediasays that int should be at least 16 bits and long at least 32 bits but of course it's implementation defined,and on latest systems int is 16 bits

Actually on most systems today an int is 32 bit. The 16 bit int was standard on 16 bit DOS but has been 32 bit on Windows for quite some time. Also, depending on operating system on 64 bit systems the int may be 64 bit as well.

Quote

But intN_t type allows integers to be stored in 8,16,32,64 bits

But remember these fixed types are not always available on all systems.
From the C11 Standard (last draft)

Quote

7.20.1.1 Exact-width integer types
1
The typedef name intN_t designates a signed integer type with width N , no padding
bits, and a two’s complement representation. Thus, int8_t denotes such a signed
integer type with a width of exactly 8 bits.
2
The typedef name uintN_t designates an unsigned integer type with width N and no
padding bits. Thus, uint24_t denotes such an unsigned integer type with a width of
exactly 24 bits.
3
These types are optional. However, if an implementation provides integer types with
widths of 8, 16, 32, or 64 bits, no padding bits, and (for the signed types) that have a
two’s complement representation, it shall define the corresponding typedef names.

Also be aware that since gcc uses the Microsoft C runtime on Windows you may not have proper support for the current C standards. The Microsoft C runtime only supports C89.

Jim

This post has been edited by jimblumberg: 09 August 2012 - 09:08 AM

Was This Post Helpful? 1
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3449
  • View blog
  • Posts: 10,643
  • Joined: 05-May 12

Re: what is difference between int and intN_t?

Posted 09 August 2012 - 10:15 AM

Quick follow up on Post #6: I've got the same consistent results between long long and int64_t on Ubuntu 12.04 LTS (64-bit).

This post has been edited by Skydiver: 09 August 2012 - 10:16 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1