2

When running the dc utility, if I input a very large number, say:

999999999999999999999999999999999999999999999999999999999999999999999999

When I print it out using p, I get:

999999999999999999999999999999999999999999999999999999999999999999999\
999

Why does it split the output after 69 digits?

4

At least for the GNU implementation of dc, there is a hard-coded DEFAULT_LINE_MAX of 70 characters - although that may be overridden by setting a DC_LINE_LENGTH environment variable. From dc/numeric.c:

559 static int out_col = 0;
560 static int line_max = -1;       /* negative means "need to check environment" */
561 #define DEFAULT_LINE_MAX 70
562 
563 static void
564 set_line_max_from_environment(void)
565 {
566         const char *env_line_len = getenv("DC_LINE_LENGTH");
567         line_max = DEFAULT_LINE_MAX;
568         errno = 0;
569         if (env_line_len) {
570                 char *endptr;
571                 long proposed_line_len = strtol(env_line_len, &endptr, 0);
572                 line_max = (int)proposed_line_len;
573 
574                 /* silently enforce sanity */
575                 while (isspace(*endptr))
576                         ++endptr;
577                 if (*endptr || errno || line_max != proposed_line_len
578                                         || line_max < 0 || line_max == 1)
579                         line_max = DEFAULT_LINE_MAX;
580         }
581 }
582 

So

$ dc
999999999999999999999999999999999999999999999999999999999999999999999999
p
999999999999999999999999999999999999999999999999999999999999999999999\
999
q

but

$ DC_LINE_LENGTH=0 dc
999999999999999999999999999999999999999999999999999999999999999999999999
p
999999999999999999999999999999999999999999999999999999999999999999999999
q
$ 
5

Terminals are traditionally 80x24 (or so) and in such a playing field long lines that wrap may be problematical, which probably accounts for the unix tradition of wrapping long lines and marking such wrapped lines with a \.

This is a very old limit, dc(1) in the Version 6 AT&T UNIX source contains:

dc1.s:ll:   70.         /line length

(Someone on retro computing might be able to tell you in detail why long lines are a problem; one might suspect that line printers back then may not have had a magical wrap-long-lines feature, so for something to print properly it would really need to fit on the page...)

  • The name for it is an automatic right margin and someone on this site can tell you about it. Thomas Dickey and I, just for starters, have discussed it. See answers like unix.stackexchange.com/questions/147609 , unix.stackexchange.com/questions/283983 , and so forth. It's also discussed at other places like superuser.com/questions/710019 . – JdeBP Feb 8 '18 at 7:42
  • More generally, dc being an arbitrary precision calculator, it can give you numbers of arbitrary length, and many tools (including most historical text processing ones), communication channels (think SMTP, or ttys on input) etc. have limits on the maximum line length. So while the 70 one most probably comes from early tty limitations or user convenience, even if at a different length, you'd still want to wrap long lines. Note that POSIX requires that 70 wrapping for bc (which was historically a wrapper around dc) – Stéphane Chazelas Feb 8 '18 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.