C programming

ChatIndia

Community Advocate
Community Support
Messages
1,408
Reaction score
30
Points
48
Code:
#include <stdio.h>
main()
{
int r,s;
printf("Enter Roll Number\n");
scanf("%d",&r);
printf("Enter Serial Number\n");
scanf("%d",&s);
system("clear");
printf("%d\n",r);
printf("%d\n",s);
}

When I enter Roll No = 3332810074 and Serial Number = 14 the output roll number is wrong and the output serial number is right. Can you help me to detect the error.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
if int ---> unsigned int ---> max value 2147483647 ...
 

ChatIndia

Community Advocate
Community Support
Messages
1,408
Reaction score
30
Points
48
so what should i use for a number up to 4000000000
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Assuming you are only going to have positive numbers, unsigned int should work.
 

LostHorizon

Member
Messages
43
Reaction score
2
Points
8
For your information:

short, unsigned short -- 2 bytes
int, unsigned int -- 4 bytes
long, unsigned long -- 4 bytes

(http://msdn.microsoft.com/en-us/library/9c3yd98k(v=VS.71).aspx)


NOTES:

  1. The maximum value of any UNSIGNED integer type = (2 ^ number of storage bits) - 1. Hence:

    the maximum value of unsigned short = (2 ^ 16) - 1 = 65,535 (0xFFFF),
    the maximum value of unsigned int & unsigned long = (2 ^ 32) - 1 = 4,294,967,295 (0xFFFFFFFF).

  2. Also, the maximum value of any SIGNED integer type = (2 ^ (number of storage bits - 1)) - 1. Hence:

    the maximum value of signed short = (2 ^ 15) - 1 = 32,767,
    the maximum value of signed int & signed long = (2 ^ 31) - 1 = 2,147,483,647.
Hope this helps.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
long, unsigned long -- 4 bytes

That's only to be expected on a 32-bit machine, or when using a Microsoft compiler. For example, a long on a 64-bit machine running Linux in a program compiled in 64 bit mode will use 8 bytes. To get the minimum and maximum integer values on a system, consult limits.h.
 
Last edited:
Top