What does this c-line of code do? (const VAR = "string";)

Stumbled upon this line of c code, but was not sure if it really was or not. What does it do? What type will the variable have?

const VARNAME = "String of text";
+5
source share
3 answers

Curiously, I did not expect this to compile, but it is. However, the compiler does not really like this:

..\main.c:4:7: warning: type defaults to 'int' in declaration of 'VARNAME'
..\main.c:4:17: warning: initialization makes integer from pointer without a cast

Thus, it accepts the int type by default, and therefore, the VARNAME value has a pointer value, since the string is a pointer (which may later be distinguished as char *).

This works fine (on Intel IA32 machine):

#include<stdio.h>

const VARNAME = "String of text";

int main()
{
    printf("%s\n", (char*)VARNAME);
    return 0;
}

But I personally would not use such implicit typing. As explained below:

this is even dangerous since sizeof (int) may be smaller than SizeOf (char *)

+6

GCC :

  • VARNAME , int;
  • int .

, , int - 32 , - 64 .

a.c:1: error: initializer element is not computable at load time
+2

Find the definition of "VARNAME" and you will see. I would say something like "char *".

0
source

All Articles