# what is difference between int and intN_t?

Page 1 of 1

## 8 Replies - 1088 Views - Last Post: 09 August 2012 - 10:15 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=288323&amp;s=997ecb13f74f7b8d705a1d61b53a8e3c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 m*pasha

Reputation: -1
• 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

• Code herder

Reputation: 6111
• Posts: 21,033
• 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.

### #3 jimblumberg

Reputation: 5444
• Posts: 16,968
• 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

### #4 m*pasha

Reputation: -1
• 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.

jimblumberg, 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

### #5 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• 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);
}
```

### #6 Skydiver

• Code herder

Reputation: 6111
• Posts: 21,033
• 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

### #7 m*pasha

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

## Re: what is difference between int and intN_t?

Posted 09 August 2012 - 08:40 AM

#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;

}

```

### #8 jimblumberg

Reputation: 5444
• Posts: 16,968
• 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 deﬁne 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

### #9 Skydiver

• Code herder

Reputation: 6111
• Posts: 21,033
• 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