-3

I just started programming in c and i know some basics of python so when i set a very big int value to a variable in python say '58944651132156484651' and print the variable everything will be fine but when i do the same with c the answer will be 247..... after searching this I found that this is the max possible value in c and i understood everything but can anyone tell me that what was the need of this thing everything was working fine without this max value and this maximum value will arise so many problems like I can't set my phone number as variable and a lot of things like that so please help me to figure this out. Remember i was a python user and now I am learning c.

/*code in c*/
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num = 456465465456456465465456;
    printf("%d \n", num);
    return 0;
}


#code in python
num = 54454564564848431284132116483211
print(num)
#that's better than the c's big print :(
Håken Lid
  • 22,318
  • 9
  • 52
  • 67
  • To find out max value of int, take look at the `INT_MAX` macro in `limits.h` header file. – JUSHJUSH Aug 17 '19 at 08:27
  • This question is too broad for SO. You should first start out by looking at how numbers are stored in the computers registers. And just a PSA: don't store phone numbers as an actual number. Store it as a string – rdas Aug 17 '19 at 08:29
  • Good place to start - https://www.tutorialspoint.com/cprogramming/c_data_types.htm – thisisbenmanley Aug 17 '19 at 08:29
  • 5
    Please repeat after me: **A phone number is not an integer**! My phone number in international format starts either with `+358` or `00358`, neither of which you can code as an integer. – Antti Haapala -- Слава Україні Aug 17 '19 at 08:31
  • You means that we can only use big numbers like 283737282827737346 as strong that is like printf ("283737377373634") – Tushar Sharma Aug 17 '19 at 08:34
  • There is usually no need to use such big numbers. – JUSHJUSH Aug 17 '19 at 08:35
  • But in Python there is infinite limit then what was the need of limiting the value in c – Tushar Sharma Aug 17 '19 at 08:36
  • C is low level language. You'll miss **A LOT** functionalities from python. Python and C have different goals. – JUSHJUSH Aug 17 '19 at 08:36
  • @JUSHJUSH ok got it but if assume I have to store a very big number say 277363737337 as variable coz I'll change it's value again and again and it's a number which is repeated multiple time in the complete code then what will I do as I can't assign that big value in c – Tushar Sharma Aug 17 '19 at 08:38
  • @TusharSharma, you can store 9223372036854775807 in long long variable. Once again, storing such a big number is not usual case in real word (low-level)programming. – JUSHJUSH Aug 17 '19 at 08:46
  • 1
  • @JUSHJUSH what does 64 bit long variable means and how to use it in c? – Tushar Sharma Aug 17 '19 at 08:47
  • 4
    @TusharSharma please consider getting yourself [a C text book](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and reading it. – Antti Haapala -- Слава Україні Aug 17 '19 at 08:47
  • @AnttiHaapala actually j already have one and asking only the doubts which confuse sorry for disturbing I'll not reply now – Tushar Sharma Aug 17 '19 at 08:48
  • You really did *not* ask a question. There is no "one size fits all" standard-compliant solution for the problem of storing exact integer numbers greater than 2⁶³. – Antti Haapala -- Слава Україні Aug 17 '19 at 09:04
  • beside `+` phone numbers can also contain #, \* and pause (for extensions). Regarding precision then most languages don't support [arbitrary-precision integers](https://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic) because it's much slower and need special support from libraries (since no CPU supports that) – phuclv Aug 17 '19 at 10:10
  • Possible duplicate of [Store and work with Big numbers in C](https://stackoverflow.com/questions/2640625/store-and-work-with-big-numbers-in-c) – phuclv Aug 17 '19 at 10:18
  • [How do you store an arbitrarily large integer value in memory?](https://stackoverflow.com/q/2252896/995714) – phuclv Aug 17 '19 at 10:18

1 Answers1

5

For Python to support integers without fixed limits, it must include a lot of software that manages the data. Because there are no fixed sizes, Python must support variable lengths for its integer objects. That means it must track those lengths, it must allocate and free variable amounts of memory for them, and it must have routines that perform compound arithmetic operations on them. This is slow and relatively expensive.

C is largely designed to give more direct access to the computer. It provides elementary operations, leaving it to the programmer to either build their own more complicated functions or to limit their operations to the natural bounds of the computer. By and large, adding or multiplying two numbers in C is effected with a simple processor instruction, whereas Python may require dozens even for the same values.

This means the maximum value of an int in C largely arises out of what the computer processor provides in its operations. You ask what was the “need” of limiting the size: The need is simply that the maximum value of an int is the maximum value that can be supported without using additional instructions or resources to represent greater values.

You should also be aware that C does not specify the maximum value of an int is 2,147,483,647. C is designed to be flexible and portable, and the maximum value of an int varies from C implementation to C implementation. It is at least 32,767. 2,147,483,647 is common these days, but other values are allowed.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312