Skip to content

Inconsistent syntax highlighting for C funcitons in the same stdio.h header #2973

@drankinatty

Description

@drankinatty

Inconsistent Syntax Highlight for C functions from stdio.h

On StackOverflow when answering questions, highlightjs fails to apply consistent syntax highlighting to many functions, but this is particularly acute when it applies (or fails to apply) consistent syntax highlighting to functions from the same standard header stdio.h For example:

The functions fputs() and puts() are highlighted, but fopen(), fgetc() and fclose() all have no highlight applied at all. The is confusing to new programmers wondering why some functions are one color and some are not.

This applies to C, but is applicable to C++ as well

Here is the link to the answer: Strncat causes exception

Are you using highlight or highlightAuto?

Sample Code to Reproduce

#include <stdio.h>

#define MAXC 100        /* if you need a constant, #defien one (or more) */

int main (int argc, char* argv[]) {
    
    if (argc < 2) { /* validate one argument give for filename */
        fputs ("error: too few arguments.\n", stderr);
        return 1;
    }
    char buf[MAXC] = "", c;
    size_t n = 0;
    FILE* fp = fopen (argv[1], "r");

    if (fp == NULL) {                   /* validate file open for reading */
        perror ("fopen-argv[1]");       /* on failure, perror() tells why */
        return 1;
    }

    /* while buf not full (saving 1-char for \0), read char */
    while (n + 1 < MAXC && (c = fgetc(fp)) != EOF)
        buf[n++] = c;                   /* assign char to next element in buf */

    buf[n] = 0;                         /* nul-terminate buf */
    
    puts (buf);                         /* output result */
    
    fclose(fp);
}

Expected behavior

All functions, especially from the same standard header stdio.h should have consistent highlight applied.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions