omerjerk
omerjerk

Reputation: 313

How to send ctrl+z in C

I'm working with Arduino.

I want to send Ctrl+z after a string in C. I tried truncating ^Z but that didn't work. So how to do that ?

Upvotes: 15

Views: 74975

Answers (4)

shirish patki
shirish patki

Reputation: 1

The following should work: whatever you are trying to write append \032 at the end For example:

strcpy(InputCommand,"hi\032");
GetSerialData(InputCommand,......); //this is my own function which uses serialPuts() 

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 881613

I assume by "truncating" you actually meant appending.

In ASCII, CTRL+z is code point 26 so you can simply append that as a character, something like:

#define CTRL_Z 26
char buffer[100];
sprintf (buffer, "This is my message%c", CTRL_Z);

The sprintf method is only one of the ways of doing this but they all basically depend on you putting a single byte at the end with the value 26.

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 754060

Ctrl+Z = 26 = '\032' = '\x1A'. Either of the backslash escape sequences can be written in a string literal (but be careful with the hex escape as if it is followed by a digit or A-F or a-f, that will also be counted as part of the hex escape, which is not what you want).

However, if you are simulating terminal input on a Windows machine (so you want the character to be treated as an EOF indication), you need to think again. That isn't how it works.

It may or may not do what you want with Arduino, either; in part, it depends on what you think it is going to do. It also depends on whether the input string will be treated as if it came from a terminal.

Upvotes: 21

Goblinhack
Goblinhack

Reputation: 3088

I hacked this up as I needed similar

#include <stdio.h>
#define CTRL(x) (#x[0]-'a'+1)
int main (void)
{
    printf("hello");
    printf("%c", CTRL(n));
    printf("%c", CTRL(z));
}

hope it helps 8)

Upvotes: 6

Related Questions