In “Stranger Than Fiction,” a nondescript IRS auditor discovers that he is actually a character in a novel and that the author plans to kill him off. Dismayed by the prospect of his demise, he sets out to track down the author of his story so he can convince her to let him live.

Zach Helm’s screenplay has been praised as a whimsical fantasy, and a fantasy it is, of course. Yet writers may feel a tingle of recognition as Will Ferrell’s Harold Crick simply refuses to go quietly into that good night.

Writers often find their characters not just taking on a life of their own, but acting up, acting out — and occasionally, like Crick, fighting for their lives.

Guillermo del Toro, writer-director of “Pan’s Labyrinth,” lost that fight to one of his characters. He’d planned for a resistance fighter in the film to be tortured to death onscreen. “Literally, there was a point where I could not kill her,” says del Toro. “It was not just a matter of dramaturgy or structure, she simply was so strong that she almost willed herself to live. I can’t put it any other way.”

On the other hand, Helm had nearly the opposite experience while penning “Stranger Than Fiction.” He’d intended for the literature professor Hilbert (played by Dustin Hoffman) to read the manuscript in which Crick was to die and find a way for the doomed protagonist to live.

Instead, says Helm, “I found myself writing Hilbert saying, ‘No, you have to die.’ I just found this character saying that, and I began thinking, ‘Well wait a second, I don’t want him to die, that’s not where I want this to go.’ But when he said, ‘It’s her masterpiece,’ I realized that that’s what that character would do.”

That left Helm in a plot bind, but he still finds those moments when the characters strike out on their own to be “very enlightening.

“Obviously there’s something that I’m missing if I have a character that won’t follow the path. I haven’t looked at it from all the points of viewing. To ask a character to do something unnatural, in the same way as asking an actor to do something unnatural, is only going to wind up with negative results, so I try to learn from those moments as much as I can.”

On “Stranger Than Fiction,” Hilbert’s little rebellion told him that some viewers would want Crick to live and others would want him to die for the sake of the novel. He also realized “that there was going to have to be a sacrifice. And at that point I realized that the entire movie was gonna be about the sacrifices, and that was a huge thematic moment for me. It was a tremendous moment to realize that and if I hadn’t allowed that moment to happen, if I hadn’t allowed those characters to sort of defy the plot, then I wouldn’t have found it.”

Many writers, it turns out, share the feeling that they do not exactly create the characters they write or exercise godlike powers over the worlds they create.

“The first to talk to the character is the writer,” says del Toro, “then you pass it along to the director and the actor. It’s like automatic writing. Writing characters is channeling them.”

Helm, too, uses “channeling” to describe his experience.

A character can even send an author’s entire story spinning in an unexpected direction. It happened to Tom Perrotta in the course of writing the novel “Little Children,” which he later helped adapt for the screen.

Perrotta had planned to write a light story of love and adultery in the suburbs. Then he had his hero get a ride from one part of town to another. “My intention was simply to get him from the library where he’s watching the skateboarders to the football game. But he got in the car with this guy who’s got the posters about this pedophile, who’s obsessed with this guy, and it was like this guy drove in with this subplot that I hadn’t planned and wasn’t sure I wanted.

“It’s one of those cases where your subconscious is generating material and the novel is telling you what it wants to become. I had meant to write this comic story, and then this dark and threatening subplot drove into the novel.”

These moments can strip a writer of a structure he’s worked very hard to build. “You go into a writing project with your structure and beat sheet and 3×5 cards, and you’re very organized,” says del Toro, “but as you’re writing, it does start to dialogue with you.”

Perrotta says writers commonly find a character “coming in and hijacking the story and sometimes taking you down a very time-consuming dead end. And sometimes it can be hard to get back where you were.” In “Little Children,” he spent six months wondering if this dark turn would destroy the book. In the end, though, it worked.

Anthony Minghella, writer-director of “Breaking and Entering,” doesn’t go quite as far as some of the others, but he says, “I think you surrender to whatever state you’re in when you’re making things up, which is half-dream, half-awake, half-organizing, half-astonishing.”

In that state, Minghella, too, found his characters sneaking up on him. The Juliette Binoche character in “Breaking and Entering” snaps a photo of Jude Law, then uses it to blackmail him. Her snapping the shot, he says, “was not at all in my mind, and yet it just seems suddenly inexorable. It sounds absurd because it could only come from my imagination, but it was quite unexpected.”

Minghella concurs that “If you get to the point of focus, the writing can surprise you. You arrive at an observation you haven’t bargained for.”

But he also adds, “There’s got to be some adventure, hasn’t there?”