News for hackers. Stuff that matters.
Re:What does the ??!??! operator do in C? by susam on Wednesday October 05, 2022 @06:55PM (#33102248)
I learnt C, more than 20 years ago, from the book The C Programming Language written by Brian W. Kernighan and Dennis M. Ritchie, also known as K&R. I read the book almost cover to cover all the way from the preface at the beginning to its three appendices at the end while solving all the exercises that each chapter presented. As someone who knew very little about programming languages back then, this book was formative in my journey of becoming a programmer.

Appendix A (Reference Manual) of the book broadened my outlook on programming languages by providing me a glimpse of what goes into formally specifying a programming language. Section A.12 (Preprocessing) of this appendix specifies trigraph sequences. Quoting from the section:

> Preprocessing itself takes place in several logically successive phases that may, in a particular implementation, be condensed.

> 1. First, trigraph sequences as described in Par.A.12.1 are replaced by their equivalents. Should the operating system environment require it, newline characters are introduced between the lines of the source file.

Then section A.12.1 (Trigraph Sequences) further elaborates trigraph sequences in more detail. Quoting this section below:

> The character set of C source programs is contained within seven-bit ASCII, but is a superset of the ISO 646-1983 Invariant Code Set. In order to enable programs to be represented in the reduced set, all occurrences of the following trigraph sequences are replaced by the corresponding single character. This replacement occurs before any other processing.

  ??=  #
  ??/  \
  ??'  ^
  ??(  [
  ??)  ]
  ??!  |
  ??<  {
  ??>  }
  ??-  ~
> No other such replacements occur.

> Trigraph sequences are new with the ANSI standard.

Re:What does the ??!??! operator do in C? by vonwoodson on Wednesday October 05, 2022 @07:44PM (#33102637)
To be fair, and definitely a part of its appeal, the K&R is only 312 pages long. It covers the language and most of the standard library you’ll need.

As opposed to say, “Learn You a Haskell for Great Good! A Beginner's Guide” which is 881 pages and doesn’t even moderately cover the prelude.

Anyway, C is an amazing language and I keep a K&R on my phone as a pdf

Re:What does the ??!??! operator do in C? by 1-more on Wednesday October 05, 2022 @08:19PM (#33102872)
My copy of Learn You a Haskell ends on page 360 and the index is another 16 pages. Is this some weirdly shaped PDF of it with tiny phone-sized pages?
Re:What does the ??!??! operator do in C? by sli on Wednesday October 05, 2022 @08:34PM (#33102955)
According to its listing on the No Starch website[0], the PDF is (currently) 400 pages. I'm not a fan of that particular Haskell book but it's in no way even close to 881 pages.

[0]: https://nostarch.com/lyah.htm

Re:What does the ??!??! operator do in C? by bbarnett on Thursday October 06, 2022 @03:33AM (#33105516)
That's 800 pages double sided! Simply because the back side is blank, is no reason to nitpick.
Re:What does the ??!??! operator do in C? by tremon on Thursday October 06, 2022 @04:56AM (#33106095)
I don't get it. Wouldn't 800 pages double-sided require a PDF containing 1600 pages?
Re:What does the ??!??! operator do in C? by bbarnett on Thursday October 06, 2022 @08:14AM (#33107238)
Sorry, been looking at new cars, and salespeople have been explaining new car, extended warranties to me.
Re:What does the ??!??! operator do in C? by stonemetal12 on Thursday October 06, 2022 @09:33AM (#33107961)
400 single sided printed pages. If you are trying to inflate the page count that is 800 pages, but every other page is blank.
Re:What does the ??!??! operator do in C? by Kalq on Thursday October 06, 2022 @04:53PM (#33113958)
Why don't you like that book and which Haskell book would you recommend then?
Re:What does the ??!??! operator do in C? by vonwoodson on Wednesday October 05, 2022 @11:05PM (#33103874)
Could be, now that you mention it. Which, now, makes me wonder if my K&R is even shorter.
Re:What does the ??!??! operator do in C? by DonHopkins on Thursday October 06, 2022 @05:53AM (#33106382)
There's one thing that's usually true about every well used copy of K&R, and that's that it always opens up easily and by default to the page with the huge operator precedence order and associativity table, because that's the page everyone needs to refer to the most often, which is usually bookmarked, but in an old copy doesn't even need to be.

That says something about the C programming language design, that I as a deeply stack based FORTH programmer and explicitly parenthetical LISP programmer find horrible.

Re:What does the ??!??! operator do in C? by dahfizz on Thursday October 06, 2022 @08:31AM (#33107414)
Just use parentheses if you're doing many operations on one line? I've been programming in C for years and never had an issue with operator precedence.
Re:What does the ??!??! operator do in C? by falcrist on Thursday October 06, 2022 @09:54AM (#33108173)
There's a lot of exaggeration that goes on with certain fans of postfix systems when they talk about infix systems.

For example: I really like HP calculators, so I'm in several facebook groups for fans of RPN/RPL and HP specifically. Sometimes a few of them go way too far out of their way to try to demonstrate how inferior algebraic systems must be.

For the record, my copy of K&R wants to open into either section 7.6 or appendix B. No idea what this says about me, though.

Re:What does the ??!??! operator do in C? by michaelcampbell on Saturday October 08, 2022 @09:39AM (#33132191)
Indeed, different strokes for different uses. I "grokked" RPN pretty early in my career and use it for all my calculator stuff... only. infix for programming languages is equally comfortable for me. I still have to think and study quite hard the prefix LISP notation. I get it, it's just not internalized like the other 2.
Re:What does the ??!??! operator do in C? by DonHopkins on Thursday October 06, 2022 @09:57AM (#33108208)
It's not that YOU have any issues with it -- because you're perfect.

https://www.youtube.com/watch?v=fKHaNIEa6kA

It's about the poor people who read your code that relies on both you and them having perfectly memorized every single little detail of operator precedence and associativity, instead of simply and consistently using parenthesis.

Quick without looking: can you tell me what the precedence and associativity of the ternary ?: operator is?

The designer of PHP got it wrong (which isn't surprising given his proudly self proclaimed contempt towards computer science and incompetence at parser writing), but then millions of PHP programmers also learned it the wrong way.

https://en.wikiquote.org/wiki/Rasmus_Lerdorf

Do you really want any of those people who were corrupted by PHP messing around with your code, if you relied on it being one way, and they assume it works the other way?

It's not that you can't tell what it actually does, it's that you can't tell what the person who wrote it actually meant, which is more important than what it actually does, especially when it has bugs.

Don't do many operations on one line, AND do use parenthesis, AND do use indentation, with no exceptions except for very simple expressions. Take every opportunity to use vertical alignment to make symmetry and repetition obvious, like:

    sqrt((x * x) +
         (y * y))
Redundant parens, plus breaking expressions into multiple lines and indenting according to depth, unambiguously express programmer INTENT, so the reader doesn't need to wonder if the person who wrote it had a clue or was just showboating.

Just use parenthesis, and put a comment on it, sailor.

https://wellcomecollection.org/works/m33njwx3/items

My copy of The Little Schemer won't open to page 13 because of the jelly stains.

https://vpb.smallyu.net/[Type]%20books/The%20Little%20Scheme...

Re:What does the ??!??! operator do in C? by dahfizz on Thursday October 06, 2022 @11:05AM (#33109147)
What is with the gay sailor condom ad? You're being completely ridiculous.

I'm glad you enjoy Forth so much, I guess. I'm sure postfix will catch on any day now.

Re:What does the ??!??! operator do in C? by coldpie on Thursday October 06, 2022 @10:09AM (#33108346)
I agree, but I'm still cracking open that page when I'm reading someone else's code. I guess you work solo most of the time?
Re:What does the ??!??! operator do in C? by dahfizz on Thursday October 06, 2022 @11:07AM (#33109184)
Nope, I work on a team. I guess we all have similar instincts about what is readable.
Re:What does the ??!??! operator do in C? by mrguyorama on Thursday October 06, 2022 @11:56AM (#33109902)
I don't know what it is with C programmers and programming "terseness". It hasn't been the 60s since the 60s, and you have gobs of memory available, your source code can have syntactic sugar for the purpose of readability and the world won't end.
Re:What does the ??!??! operator do in C? by dahfizz on Thursday October 06, 2022 @12:54PM (#33110797)
Can you give an example of syntactic sugar in a modern language that makes order of operations a non-issue?
Re:What does the ??!??! operator do in C? by rdlw on Thursday October 06, 2022 @05:49PM (#33114533)
Parentheses
Re:What does the ??!??! operator do in C? by dahfizz on Thursday October 06, 2022 @05:55PM (#33114597)
Yeah, that's why I said

> Just use parentheses

Not sure what that has to do with memory usage..?

Re:What does the ??!??! operator do in C? by rdlw on Thursday October 06, 2022 @06:15PM (#33114815)
Just to clarify, I'm not the original person you replied to, and we all agree about using parentheses.

mrguyorama just implied that the only reason you would check the operator precedence chart would be to shave a few bytes off the size of your source code, which has not been a reasonable reason to do anything for many decades, and yet C programmers seem to like to do it anyway.

Re:What does the ??!??! operator do in C? by 1-more on Friday October 07, 2022 @01:18PM (#33124264)
These aren't syntactic sugar, but formatters. I rather like Ormolu for Haskell and elm-format for Elm. I occasionally type a bunch of parenthesis so that I'll for sure have the right order of operations. Then the format on save removes the redundant ones. It's delightful. The typecheckers and tendency to wrap primitives in a semantically significant constructor help with that.
Re:What does the ??!??! operator do in C? by belter on Thursday October 06, 2022 @07:59AM (#33107104)
Can I up vote for first paragraph, and down vote the second? :-)
Re:What does the ??!??! operator do in C? by benj111 on Thursday October 06, 2022 @08:14AM (#33107239)
Isn't that what brackets are for?

Well that's what I do. If you're having to look it up to write it, you're going to have to look it up to read it again down the line.

Re:What does the ??!??! operator do in C? by zh3 on Thursday October 06, 2022 @01:05AM (#33104572)
Just checked my first edition K&R (copyright date 1978, last page number is 228 (end of index) after which there is a single tearout page for other "High Quality C and Unix system titles" from Prentice Hall. There's also a front section that has about 10 pages in roman numerals (2 pages of prefix starting with 'ix') so about 240 pages total.

Page 1 starts: "Chapter 0: Introduction".

Re:What does the ??!??! operator do in C? by checkyoursudo on Thursday October 06, 2022 @05:27AM (#33106262)
Page 1? What, it's not zero indexed?
Re:What does the ??!??! operator do in C? by DonHopkins on Thursday October 06, 2022 @07:44AM (#33106994)
It looks like it's 1-indexed, but it core dumps when you get to the last page.
Re:What does the ??!??! operator do in C? by pjmlp on Thursday October 06, 2022 @04:10AM (#33105771)
Which is why most workloads bring POSIX for the ride as means to make anything actually usefull.
Re:What does the ??!??! operator do in C? by dahfizz on Thursday October 06, 2022 @08:34AM (#33107455)
POSIX is an OS API. You're complaining that C interacts with the operating system to do useful work? What language do you use that can do useful work without interacting with the OS?
Re:What does the ??!??! operator do in C? by pjmlp on Thursday October 06, 2022 @09:18AM (#33107828)
POSIX is the part of the C standard library in UNIX, that should have been part of ISO C as well.

It wasn't, so any C application that is more than a toy hello world with stdio, pings back into POSIX for any kind of meaningful work, that wants to stay cross platform.

Basically it the the C runtime library, that wasn't part of ISO.

I use JVM, .NET, Web and C++, not caring if the runtimes are bare metal or running on top of an OS, type 1 hypervisor, or whatever.

Re:What does the ??!??! operator do in C? by salawat on Thursday October 06, 2022 @09:38AM (#33108008)
>I use JVM, .NET, Web and C++, not caring if the runtimes are bare metal or running on top of an OS, type 1 hypervisor, or whatever.

If you're downloading a JVM binary, you're missing out on the build step. It's C dependent, friend. How do you think that VM interfaces with the OS? Go on. Try it. ldd the java executable.

It's libc all the way down. C itself is a sort of "VM" specification utilized to create the tools to run the tools to build the tools that make other high level languages possible.

Unless you create something entirely custom in platform specific assembly, you're running on C at some level.

Re:What does the ??!??! operator do in C? by dahfizz on Thursday October 06, 2022 @10:42AM (#33108814)
I don't think you really understand what POSIX is.

POSIX is an IEEE standard (example [1]). POSIX defines the Operating System API. You can see the C implementation of this API here[2].

> so any C application that is more than a toy hello world with stdio, pings back into POSIX for any kind of meaningful work

Simply calling printf relies on writing to a file descriptor. A "Hello world" application on linux uses posix. ANY hello world application uses posix. Even your Java Hello world App will call into the posix APIs. `System.out.println` isn't magic. It calls into the C posix implementation.

If you want to do anything in any language (write to files, create threads, allocate memory, network communication), you need to go through the OS. POSIX is what defines that OS interface.

> I use JVM, .NET, Web and C++, not caring if the runtimes are bare metal or running on top of an OS, type 1 hypervisor, or whatever.

So you use POSIX, you just don't think about it.

[1] https://standards.ieee.org/ieee/1003.1/7700/

[2] https://en.wikipedia.org/wiki/C_POSIX_library

Re:What does the ??!??! operator do in C? by arinlen on Friday October 07, 2022 @03:00AM (#33118245)
> POSIX is the part of the C standard library in UNIX, that should have been part of ISO C as well.

I'm not sure what you're trying to say. The Portable Operating System Interface (POSIX) is specified in an ISO standard, and basically specifies what a UNIX operating system's programmable interfaces are.

https://en.wikipedia.org/wiki/POSIX

POSIX also specifies stuff like "awk must be made available". Is that what you think the C programming language specifies?

Re:What does the ??!??! operator do in C? by metafunctor on Thursday October 06, 2022 @04:16AM (#33105815)
My Second Edition K&R (purchased in the mid-90s) is only 272 pages, including the index.
Re:What does the ??!??! operator do in C? by wiseowise on Thursday October 06, 2022 @12:47AM (#33104486)
Comparing C to Haskell is like comparing razor to laser scalpel.
Re:What does the ??!??! operator do in C? by bryanrasmussen on Thursday October 06, 2022 @01:55AM (#33104854)
And not just any razor, a really useful one!
Re:What does the ??!??! operator do in C? by pjmlp on Thursday October 06, 2022 @06:44AM (#33106672)
One of those that always cuts the user no matter how carefully they try to get hold of it.
Re:What does the ??!??! operator do in C? by benj111 on Thursday October 06, 2022 @08:19AM (#33107300)
No it's a tool.

I suppose you could compare it to a table saw. C is one without a guard or any other safety measures, so you need to be careful not to cut your fingers off. More modern languages have the guard and break etc.

For general use you probably do want all the safety bits, but occasionally it is useful to be able to take it off to do a weird cut on a weird bit of wood.

None of that necessarily means you will cut your fingers off though.

Re:What does the ??!??! operator do in C? by unwind on Thursday October 06, 2022 @01:09AM (#33104594)
I learnt it a few years before that, and I remember how reading K&R once I got it felt like having someone turn UP the lights, open the blinds, wash the windows and basically TURN UP THE SUN compared to things I read before. So much clarity.

Number of times I've seen trigraphs in "real code": still zero. I hope it's the same for you.

Re:What does the ??!??! operator do in C? by moomin on Thursday October 06, 2022 @04:07AM (#33105743)
I read it at a similar time, and I remember that feeling well. However, if you revisit it with a critical eye, you find a hundred places where a result isn’t checked, bounds aren’t checked, memory is leaked and so on.

All of this was pretty much fine in the context in which it was written, but these days bullet-proofing things is pretty much mandatory and K&R’s elegance disappears in the face of such challenges.

Re:What does the ??!??! operator do in C? by unwind on Friday October 07, 2022 @07:04AM (#33119685)
Well yeah, but there is a difference between teaching the mechanics of a language, and teaching how to write safe, correct and secure code in that language.

Perhaps that mindset is part of what made C survive for so long and in such diverse roles.

Re:What does the ??!??! operator do in C? by usr1106 on Thursday October 06, 2022 @02:42AM (#33105161)
In 1990 IBM donated a 9370 computer to our university. The default code page for German EBCDIC did not support square brackets.

I don't remember whether trigraphs were not supported by the compiler at the time or whether we just wanted to avoid completely unreadable code. Not experienced in VM/370 administration we spent weeks to modify the system to use some international EBCDIC codepage.

The system never saw much use, everybody preferred Unix workstations where programming in C was a natural thing.

Re:What does the ??!??! operator do in C? by Taniwha on Wednesday October 05, 2022 @07:14PM (#33102399)
This is because IBM 029 card punches don't support these characters right?
Re:What does the ??!??! operator do in C? by aidenn0 on Wednesday October 05, 2022 @07:54PM (#33102711)
I thought it was because of international character sets that lacked the punctuation of EBCDIC CP37 or ASCII.

[edit]

For example ISO/IEC 646 is ascii with punctuation replaced by other characters.

Re:What does the ??!??! operator do in C? by Taniwha on Wednesday October 05, 2022 @09:25PM (#33103254)
EBCDIC is very much IBM - on our old Burroughs machine we used to have to use cent signs (and something else that I forget) for square brackets and a 3-hole multipunch for ';'
Re:What does the ??!??! operator do in C? by sargstuff on Wednesday October 05, 2022 @08:09PM (#33102806)
stills leaves the issue of how to print out something that doesn't exist as a physical old style physical type face character. (way pre-dot matrix / laster printer stuff). -- aka substituting 3 characters way more informative than blank space.
Re:What does the ??!??! operator do in C? by aidenn0 on Wednesday October 05, 2022 @10:36PM (#33103669)
My (possibly wrong) understanding is that trigraphs were a late addition to the ANSI standard, which would place it in late 80s, well into the CRT terminal and dot-matrix era.
Re:What does the ??!??! operator do in C? by mananaysiempre on Friday October 07, 2022 @04:30AM (#33118706)
I don’t think national variants of (7-bit) 646 have been relevant for a decade, and on a computer for two or perhaps more, except for that one Japanese thing with ¥ instead of \ (which Windows carried forward all the way from Japanese versions of DOS). The “International Reference Version” in 646 has changed several times (generally to increase the overlap with US-ASCII, most recently to a perfect one), but I don’t think anyone ever cared about that any more than they did about X3.4 itself specifying that '<BS>e (or maybe e<BS>' ?) denotes é. I’ve seen ¤ in some Soviet manuals, but even there it was just a weird glyph printed instead of $, not an actual currency symbol. (Apparently some European layouts still have it. Is it actually used for anything?)

The need for trigraphs looks to be well and truly limited to the weird twisted world of IBM lock-in.

Re:What does the ??!??! operator do in C? by dragonwriter on Wednesday October 05, 2022 @09:17PM (#33103216)
I think the ISO/IEC 646 invariant character set is more the issue.
Re:What does the ??!??! operator do in C? by randomswede on Friday October 07, 2022 @03:31AM (#33118395)
In the "Swedish 7-bit ASCII", the C code "a || b" would look like "a öö b". The same character mappings were used in Finland, and that's why IRC count the characters {|}[\] as letters (that would typically have been displayed as "äöåÄÖÅ").

On the Compis II computer (a CP/M machine built on the 80186 CPU), there were places for {|}[\] in the character set, but they were in the top half of the 8-bit characters and not generally useful for programming.

Re:What does the ??!??! operator do in C? by wglb on Wednesday October 05, 2022 @09:00PM (#33103106)
Or an ASR33
Re:What does the ??!??! operator do in C? by kjs3 on Wednesday October 05, 2022 @08:38PM (#33102990)
I, too, learned C by reading K&R cover to cover and solving all the exercises (in front of a Sun 3/160 running SunOS 3.5-ish). Even then back in those ancient days, it was obvious trigraphs were evil and should have been abolished to a special place in hell.
Re:What does the ??!??! operator do in C? by dijonman2 on Thursday October 06, 2022 @01:43AM (#33104776)
Fantastic book. I used Learn C in 21 days and that is what started everything for me. I had a second book on Linux administration and installed Slackware from 1.44mb disks, ultimately setting up pppd and using Mosaic.

Great memories!