carraysslash

C programming-In the below program what does '\' means in t[5\]


Consider:

#include <stdio.h>

int main(void) {
    int i,t[5\];
    for(i = 0; i < 5; i++)
        t[i\] = 2 * i;
    i = 0;
    for(i = 0; i < 5; i++)
        i += t[i\];
    printf("%d",i);
    return 0;
}

What does \ in array mean?

I found this program in CLA (C Programming Language Certified Associate) sample paper.


Solution

  • The program is incorrect, \] is a syntax error.

    A possible explanation is the author had to escape some of the C operators (such as [) to typeset code fragments and he also escaped ] which the word processing software seems to leave alone...

    Ignore these \ or replace all occurrences of \] with ].

    Note that this is a tricky question, and the programmer who wrote this code should be fired.

    Note also that the document in question has other errors. For example Question 11 reads:

    What happens if you try to compile and run this program?

    #include <stdio.h>
      int main(void) {
          int t[2\][3\] = { { 3, 2, 1 }, { 1, 2, 3} };
          printf("%d", sizeof(t) / sizeof(t[1\][1\]));
          return 0;
      }
    

    A. the program outputs 6
    B. the program outputs 3
    C. the program outputs 2
    D. the program outputs 4

    Ignoring the \], the program has potential undefined behavior as %d expects an int but the value passed to printf() has type size_t.

    Questions 13, 14, 15, 17 have a similar problem.