c++ - printf uses %s to output a character array of length 4, but the actual number of output digits is not 4
某草草
某草草 2017-06-12 09:24:55
0
1
1241

As shown in the figure, it is originally a character array with a length of 4. Using %c to manually output one by one is displayed normally. However, when using %s to directly output the character array, the number of output digits is not 4. What is this? The reason? Is there no 0 at the end of this character array? Is there any easy way to quickly output a string in this situation? (What if there is a character array with a length of several hundred to be output? Do I have to manually splice the character array members one by one?)

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

typedef struct WAV_header {
    char ChunkID[4];   /* "RIFF" */
    uint32_t ChunkSize; /* 36 + Subchunk2Size */
    char Format[4];    /* "WAVE" */

    /* sub-chunk "fmt" */
    char Subchunk1ID[4];   /* "fmt " */
    uint32_t Subchunk1Size; /* 16 for PCM */
    uint16_t AudioFormat;   /* PCM = 1*/
    uint16_t NumChannels;   /* Mono = 1, Stereo = 2, etc. */
    uint32_t SampleRate;    /* 8000, 44100, etc. */
    uint32_t ByteRate;  /* = SampleRate * NumChannels * BitsPerSample/8 */
    uint16_t BlockAlign;    /* = NumChannels * BitsPerSample/8 */
    uint16_t BitsPerSample; /* 8bits, 16bits, etc. */

    /* sub-chunk "data" */
    char Subchunk2ID[4];   /* "data" */
    uint32_t Subchunk2Size; /* data size */
} Wav_header_t;

typedef struct WAV_block_header {
    char blockID[4];   /* "data" */
    uint32_t blockSize; /* data size */
} Wav_block_header_t;


int main()
{
    FILE *fp = NULL;
    Wav_header_t header;
    Wav_block_header_t block_header;

    fp = fopen("test.wav", "rb");
    if (!fp) {
        printf("can't open audio file\n");
        exit(1);
    }

    fread(&header, 1, sizeof(Wav_header_t), fp);

//    printf("length:%d(10),0x%x, \n\n", sizeof(struct WAV_Format), sizeof(struct WAV_Format));  // 44
    printf("ChunkID \t%c%c%c%c\n", header.ChunkID[0], header.ChunkID[1], header.ChunkID[2], header.ChunkID[3]);
    printf("ChunkSize \t%d\n", header.ChunkSize);
    printf("Format \t%s\n", header.Format);
    printf("Subchunk1ID \t%x\n", header.Subchunk1ID);
    printf("Subchunk1Size \t%d\n", header.Subchunk1Size);
    printf("AudioFormat \t%d\n", header.AudioFormat);
    printf("NumChannels \t%d\n", header.NumChannels);
    printf("SampleRate \t%d\n", header.SampleRate);
    printf("ByteRate \t%d\n", header.ByteRate);
    printf("BlockAlign \t%d\n", header.BlockAlign);
    printf("BitsPerSample \t%d\n", header.BitsPerSample);
    printf("Subchunk2ID \t%s\n", header.Subchunk2ID);
    printf("Subchunk2Size \t%d\n", header.Subchunk2Size);

    printf("\n");
    
    fread(&block_header, 1, sizeof(Wav_block_header_t), fp);
    printf("blockID \t%s\n", block_header.blockID);
    printf("blockSize \t%d\n", block_header.blockSize);
}
某草草
某草草

reply all(1)
学霸

C language string ends with '

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template