Risusdan
Risusdan

Reputation: 91

Is there a special meaning of a "$" mark at the end of a string?

Please refer to the code that I saw in a certain c program:

#define _BUILD_DATE     "2010/05/03$" 
#define _BUILD_TIME     "10:46:42$"
#define _BUILD_GUEST    "Intel$"
#define _BUILD_BOARD    "B0$"
#define _BUILD_CODEVER  "2.00$"

const unsigned char SIGN_DATE[] = {_BUILD_DATE};
const unsigned char SIGN_TIME[] = {_BUILD_TIME};
const unsigned char SIGN_GUST[] = {_BUILD_GUEST};
const unsigned char SIGN_PCBV[] = {_BUILD_BOARD};
const unsigned char SIGN_CODEVR[] = {_BUILD_CODEVER};

I'm curious about why there's always a "$" mark at the end of each string. First I thought maybe I should follow this rule once I declared a string with "{" and "}",but the test below shows me that it still works fine.

#include <stdio.h>
unsigned char A[] = {"ABC$"};
//unsigned char A[] = {"ABC"};
unsigned char B[] = "123";


int main()
{
    int i,j;
    
    for(i=0;i<sizeof(A);i++)
    {
        if(A[i] == '\0')
            printf("null\n");
        else
            printf("%c\n",A[i]);
    }
    printf("\n");
    for(j=0;j<sizeof(B);j++)
    {
        if(B[j] == '\0')
            printf("null\n");
        else
            printf("%c\n",B[j]);
    }
    printf("size of A is %d\n",(int)sizeof(A));
    printf("size of B is %d\n",(int)sizeof(B));

    return 0;
}

So I'm not sure if there is any special meaning of "$" in some situation, or it is just a meaningless mark.

Thanks for your time!

Upvotes: 2

Views: 125

Answers (2)

klutt
klutt

Reputation: 31409

So I'm not sure if there is any special meaning of "$" in some situation, or it is just a meaningless mark.

Short answer

It has no special meaning in C. Not in the core language and not by convention in any library functions. The library functions instead treat a zero as a terminator.

Long answer

I cannot say if it means anything in that particular code. It's quite possible. But in general, it has no special meaning at all. A wild guess is that the strings are used as regular expressions somewhere. Then it has a meaning.

But a better guess is that it has to do with the fact that DOS used dollar terminated strings. In DOS, you could print a $ terminated string with interrupt 9. Your program is likely to have a print function that relies on that. Or maybe there is some tool that can analyze the executable file that relies on this.

Here is a Hello World in x86 assembly using DOS interrupt.

; hello-DOS.asm - single-segment, 16-bit "hello world" program
;
; assemble with "nasm -f bin -o hi.com hello-DOS.asm"

    org  0x100        ; .com files always start 256 bytes into the segment

    ; int 21h is going to want...

    mov  dx, msg      ; the address of or message in dx
    mov  ah, 9        ; ah=9 - "print string" sub-function
    int  0x21         ; call dos services

    mov  ah, 0x4c     ; "terminate program" sub-function
    int  0x21         ; call dos services

    msg  db 'Hello, World!', 0x0d, 0x0a, '$'   ; $-terminated message

Note that 0x0d, 0x0a is just to print a line break. On DOS (and also windows) you need a Carriage return character (0d) before the Newline character (0a).

I found the code here https://montcs.bloomu.edu/Information/LowLevel/Assembly/hello-asm.html

In C, what ends a string is instead the zero terminator.

Upvotes: 5

Paul Yang
Paul Yang

Reputation: 346

'$' has no special meaning on string operations of C programing.

Upvotes: 0

Related Questions