Best way to declare a constant string in C [duplicate] - c

This question already has answers here:
How do you declare string constants in C?
(5 answers)
"static const" vs "#define" vs "enum"
(17 answers)
Closed 5 years ago.
I have a string: "The world is a beautiful place to live in.".
I have two source files and a global header file. I can access the string in two easy ways:-
Declaring and defining a constant variable as shown: I can use it using extern
const char *str = "The world is a beautiful place to live in.";
Using #define STR "The world is a beautiful place to live in."
Out of the above two options, which one is preferred in C?

It doesn't matter (doesn't matter if you only consider of getting a string literal) how you do but in case you want to get the string constant to be traced under debugger you may want to use the const char*str = ...
With macro definition you will get all the problems of macro
expansion- the side effects of the expanson, the pollution of the name
space, the deviation from debugger help etc

Related

What is the differences between storage classes [duplicate]

This question already has answers here:
C Storage-class differences?
(2 answers)
Closed 4 years ago.
Could you explain easily all differences between the four storage classes with examples and usages.
I have found some information but I could not understand well.
I only know 2 things:
1) When we use the keyword 'static' in a function the variable still remains after the function ends. but what about outside of the function is it necessary?
2) When we use extern for a variable we can use it from anywhere.
but I know when we declare (int variablename) on top lines it still can be used from anywhere.
am I right about the 2 things or the only things that I know are incorrect?
and another question:are the 4 stroge classes are special for c or are they same in any other languages
C uses storage classes to let you control a variable's scope and lifetime. "Variable scope" and "variable lifetime" are concepts that almost all languages have. "Scope" is basically "what code can see/use this variable" and "lifetime" is "when is this variable created and destroyed".
"extern" doesn't create a variable or function, it just declares that somewhere else in the program that variable or function exists.
Hopefully this will give you enough information to find the rest of the answers you want.

What is the meaning of 'static void *' in C? [duplicate]

This question already has answers here:
What does "static" mean in C?
(20 answers)
Closed 6 years ago.
I'm new to C and recently when i'm learning zeromq (work related) I'm kinda confused with the static void *:
...
static void *
worker_task(void *args)
{
...
What is the exact the meaning of that line? I tried looking for answer, I thought that it was a pointer but It is kinda odd, given that pointer usually have a variable name after the '*'.
Thank you very much, I hope It's not rude of me for asking this seemingly "novice" question. :)
The function worker_task returns a void *.
The static keyword in front of the function definition means that the function is only viewable inside of the current compilation unit, i.e. a given object file, typically built from one source file with several include files. The function is not visible from other object files that may be linked with with one containing this function.

How to find function name and print it in which in the variable is declared in c [duplicate]

This question already has answers here:
Programmatic way to get variable name in C?
(10 answers)
Closed 7 years ago.
int findme()
{
int input[120];
int make00;
}
I want to find make00 in the program and after finding i want to print the function name which contain this variable. How do I do that PS:I am noob in c.(linux)
Have you considered good old bison + flex? The C/C++ grammar could be very simplified for your needs... I think this is your best option here.

Using "#define" symbolic constants in ANSI C [duplicate]

This question already has answers here:
"static const" vs "#define" vs "enum"
(17 answers)
Why do most C developers use define instead of const? [duplicate]
(9 answers)
What Is The Point of Symbolic Constants?
(5 answers)
Closed 9 years ago.
After coming across symbolic constants via K&R's The C Programming Language I fail to see the usage/importance of them. I'd rather use a 'real' constant, say an integer, than using a #define statement. I can see why you would use say...
#define PI 3.14
as opposed to just typing "3.14" in your program but why would you use a #define statement if
I could just use...
static const double PI = 3.14;
Are symbolic constants used today or is it just a feature of the language that was more useful 'back in the day'?
What do you recommend I should use for constants?
#define is old, and simply textual replacement processed by preprocessor. For example when you do #define PI 3.14, preprocessor simply replaces all occurance of PI by the number literal 3.14
const is newer, and compiler dependent. It could be evaluated at compile time, or at runtime. It also provides scope.
I personally use #define because it is guarrentee to be evaluated at compile time, and it has other flexible uses.

Can I use asm function with a c - string variable instead of a string literal as an argument? [duplicate]

This question already has answers here:
How can use asm() with a const char* instead of a string literal?
(1 answer)
C/C++ inline assembler with instructions in string variables
(2 answers)
Closed 1 year ago.
I did something like this :
char command [] = "Some assmebly code";
asm volatile(command);
It didnt work. Specifically, I get the following error :
Expected string literal before command.
So, can I only use string literals with C inline assembly ? I really want to be able to use string variables like above. How can I achieve this effect ?
As far as I know asm code fragment are inlined during compilation phase, not run-time. Which means you can't do what you are doing.
As pmg suggested, you could do it by calling external commands, but it would mean user would need to have all the tools installed.

Resources