-7

I have two questions:

  1. If we want to declare one million variable names all having the same length then what should be the smallest length possible to use?

  2. If we want to declare one million variable names of any possible valid length then what is the maximum size of the variable name required?

Zahid
  • 13
  • 4
  • 3
    1 million variables? You might want to reconsider your design. – Maroun Feb 10 '14 at 11:38
  • By 'length' do you mean length of the variable name? – MrKWatkins Feb 10 '14 at 11:43
  • If all the variables are in separate scopes they can all be named `x`, so the minimum required length would be 1. – eerorika Feb 10 '14 at 11:44
  • @ᴍaroun ᴍaroun: Maybe it's a stress test for a pre-processor, or whatever. – TonyK Feb 10 '14 at 11:44
  • My guess is that what he actually wants is to have a set of X strings, all of the same length Y. (e.g. "AA", "AB", "BA" "BB"). What is the the shortest length needed to allow a million permutations? He doesn't specify what characters can be used, though, so we still can't answer. – benjymous Feb 10 '14 at 11:46
  • @benjymous it is `C++`. – Ari Feb 10 '14 at 11:50
  • For 1.000.000 variables, five characters from a-z would be enough. If you have 32 character, than your letter variable names are sufficient. If you manage to find 100 different charactes, you could do it with 3 letter variable names. – tgmath Feb 10 '14 at 11:50
  • @Zahid, I don't know why everybody seems to find your question so difficult to understand! It seems clear enough to me. – TonyK Feb 10 '14 at 11:58
  • @Ari And? Is he using names that only contain A..Z, or A..Z,a..z or 0..9, or unicode?, etc? The question is unanswerable without that bit of information. – benjymous Feb 10 '14 at 11:59
  • @TonyK It's clearer now that it was edited to mention that it asks about variable names. – eerorika Feb 10 '14 at 11:59
  • @user2079303: Yes, that's true. – TonyK Feb 10 '14 at 12:02
  • [Answers in this question](http://stackoverflow.com/questions/6007568/what-is-max-length-for-an-c-c-identifier-on-common-build-systems) may be relevant to the second part of the question. I don't see how having a million variables affects the maximum required length. Note that the limits are for [mangled](http://en.wikipedia.org/wiki/Name_mangling) names. – eerorika Feb 10 '14 at 12:15

4 Answers4

1

If I have understood you correctly then C++ has special template class std::numeric_limits that allows to get maximum and minimum values for any arithmetic type. To use it you need to include header <limits>

For example that to get the maximum and minimum values for type int you could write

#include <iostream>
#include <limits>

int main()
{
   std::cout << "The maximum value of int is " << std::numeric_limits<int>::max() << std::endl;
   std::cout << "The minimum value of int is " << std::numeric_limits<int>::min() << std::endl;
}

If you are asking about the minimum length for all one million identifiers having the same length then you should calculate the number of permutations such that n! will be not less than one million

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

You have at least 53 chars which could be used as first char of variable name ('a'-'z', 'A'-'Z', and '_'). Then you can also use digits, so you have 63 possible chars. 53*63*63*63 > 1 000 000, so you don't need names longer than 4 chars.

How long variable name could be depends only on compiler.

Ari
  • 3,101
  • 2
  • 27
  • 49
1

This is not programming question, but math question. To name variable we have to use letters a-z, numbers 0-9 and few special characters (-, _). So we can use appr. 39 different characters. This is our alphabet. So to store 1000000 names we have to use names with minimum length:

log(base39)(1000000) = ~3.8

So to name million variables (to get 1000000 unique names), you have to use at least 4 characters in name. (I know about not allowed "023abc" variable names, but if we exclude them, result will not change a lot).

Answer: 4 chars. This is minimum character count. Maximum name length is limited by your compilator.

Oleg Olivson
  • 489
  • 2
  • 5
1

The first character in an identifier must be a-z, A-Z, or underscore, for 53 possibilities. Subsequent characters can be digits 0-9 too, for 63 possibilities. So n characters give 53 * (63**(n-1)) legal identifiers:

Length Names
1      53
2      3339
3      210357    // much less than 1000000
4      13252491  // much more than 1000000

Some of these are reserved words (int, return etc.), which we are not allowed to use. Others begin with an underscore, which the C++ standard frowns on. But these small considerations don't change the answer, which is 4 (to both your questions).

TonyK
  • 16,761
  • 4
  • 37
  • 72
  • 1
    There are lots of non alpha-numeric characters that are valid in variable names, too - e.g unicode. – benjymous Feb 10 '14 at 12:00
  • In addition to those beginning with an underscore, identifiers that contain adjancent underscores are reserved as well. – eerorika Feb 10 '14 at 12:08