String literal

This is a description of a string literal that expresses string in the source code. String literals are represented by enclosing them in double quotation "" ".

// String literal
"Hello"

This is a sample that outputs a string literal. String literals can be assigned to variables in char *. I think that string literals are mostly treated as constants, so it is recommended to assign them to variables declared with "const char *". You can print a string in the "%s" format of the printf function.

#include <stdio.h>

int main (void) {
  const char * message = "Hello";
  
  printf("%s\n", message);
}

This is the output result.

Hello

Escape sequence of string literals

Escape sequence of string literals. You can express tabs and line breaks with "\ t" and "\ n".

\ t Tab
\n Line feed (LF)

This is a sample escape sequence for string literals.

#include <stdio.h>

int main (void) {

  const char * message = "He \ tllo\n";
  
  // Escape sequence of string literals
  printf("%s", message);
}

This is the output result.

He llo

Arbitrary string of ASCII code

You can use the escape sequence "\ x hexadecimal ASCII code" to represent any string of ASCII code. The following represents the ASCII code "a" in hexadecimal.

\ x61

This is a sample that uses an arbitrary string of ASCII code in the escape sequence of a string literal.

#include <stdio.h>

int main (void) {

  const char * message = "He \ x61 llo";
  
  // Escape sequence of string literals
  printf("%s\n", message);
}

This is the output result. "\ X61" outputs a.

He a llo

Escape sequence list

Symbol Meaning
\ a Bell
\ b Backspace
\ f Page feed (clear)
\n Line break (line break)
\ r Page breaks
\ t tab
\ v Vertical tab
\\ \
\? ?
\' Single Quotation (')
\ " Double quotation (")
\ 0 Null character
\ N Octal constant
\ xN Hexadecimal constant

Associated Information