What Does "#Define Str(A) #A" Do

What does #define STR(a) #a do?

In the first definition, #a means to print the macro argument as a string. This will turn, e.g. STR(foo) into "foo", but it won't do macro-expansion on its arguments.

The second definition doesn't add anything to the first, but by passing its argument to another macro, it forces full macro expansion of its argument. So XSTR(expr) creates a string of expr with all macros fully expanded.

Meaning of a function - str

It specifies the return value. It has nothing to do with the code at all and is just for documentation purposes.

What are the specifics of the definition of a string in C?

c1 is mostly [1] equivalent to &c1[0], which is holding one string, "CS".

There's a second string lurking in there, "324", starting at &c1[3] -- but as long as you access c1 as c1, the string "CS" is all the functions strcpy() et al. would see.


[1]: c1 is an array, &c1[0] is a pointer.

How many bytes does a #define string (string literal) take?

There is a trailing '\0' at the end.

Is it possible to put compiler -D define string value in variable?

You should try this common trick usually called stringification:

#define STR_IMPL(x) #x
#define STR(x) STR_IMPL(x)

#ifdef COMP_DEF
static const char * x = STR(COMP_DEF);
#else
static const char * x = "NULL";
#endif

# followed by argument name in macro expands to string literal containing passed argument. If you do just

#define STR(x) #x

this would make STR(COMP_DEF) expand to "COMP_DEF". To avoid this you need another level of macro expansion.

What is the meaning of a for/in loop on a string?

What is the meaning of a for/in loop on a string?

If you think of a string as an array of characters, the purpose of looping over a string would be to iterate through each of the characters.

In JavaScript for..in loops will return the keys in an object or array, so what you're seeing are the array indices. A more useful format would be:

var str,
item;
str = "abc";
for (item in str) {
console.log(str[item]);
}

Which will output 'a', 'b', and 'c'.

Is this code even valid JavaScript?

It is now, but there were issues in the past.

Note that for older browsers, array indices weren't supported on strings, so a backwards compatible way to iterate over the characters in a string is:

var str,
i;
str = "abc";
for (i = 0; i < str.length; i++) {
console.log(str.charAt(i));
}

What does - mean in Python function definitions?

It's a function annotation.

In more detail, Python 2.x has docstrings, which allow you to attach a metadata string to various types of object. This is amazingly handy, so Python 3 extends the feature by allowing you to attach metadata to functions describing their parameters and return values.

There's no preconceived use case, but the PEP suggests several. One very handy one is to allow you to annotate parameters with their expected types; it would then be easy to write a decorator that verifies the annotations or coerces the arguments to the right type. Another is to allow parameter-specific documentation instead of encoding it into the docstring.



Related Topics



Leave a reply



Submit