Skip to main content

Command Palette

Search for a command to run...

String Literal Indexing

Updated
1 min readView as Markdown
String Literal Indexing
#include <stdio.h>

int main()
{
    printf("%c\t","Hello"[0]);
    printf("%c\t","Hello"[1]);
    printf("%c\t","Hello"[2]);
    printf("%c\t","Hello"[3]);
    printf("%c\t","Hello"[4]);
    printf("%c\t","Hello"[5]);

    printf("\n\nIn ASCII:\n\n");
    printf("%d\t","Hello"[0]);
    printf("%d\t","Hello"[1]);
    printf("%d\t","Hello"[2]);
    printf("%d\t","Hello"[3]);
    printf("%d\t","Hello"[4]);
    printf("%d\t","Hello"[5]);

    printf("\n\n ( a[b] = b[a] ):\n\n");
    printf("%c\t",0["Hello"]);
    printf("%c\t",1["Hello"]);
    printf("%c\t",2["Hello"]);
    printf("%c\t",3["Hello"]);
    printf("%c\t",4["Hello"]);
    printf("%c\t",5["Hello"]);

    return 0;
}

Output:

Notes

  1. The string literal also is like a character array.

  2. %d makes printf print the ASCII value of a character.

  3. Array manipulation using pointers: a[i] = *(a+i) = *(i+a) = i[a]

30 views

More from this blog

P

Programming Tutorials

89 posts