how does C compiles "senthil""kumar" in a printf statement? [duplicate] - c

This question already has answers here:
two strings separated by blank being concatenated automatically
(3 answers)
Closed 8 years ago.
I've doubt regarding the basics of C language while using printf statement.
well this is how my code looks like.
#include<stdio.h>
int main(){
printf("%s %s",("senthil""kumar"),("hello""world"),("stack""overflow");
return 0;}
I've got an output like,
senthilkumar helloworld
but i don't how does this code works.
could u pls help me to figure out how it works...
thanx in advance.

Two consecutive string literals are merged by the compiler.
I.e. the following examples are equivalent:
// 1:
"foo" "bar"
// 2:
"foobar"
// 3:
#define FOO "foo"
FOO "bar"
You do not need all those inner parentheses (also, there's a ) missing at the end).

Related

is there any array[a,b] syntax in c? [duplicate]

This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 24 days ago.
while reading about the c preprocessor I got something like array[x=y,x+1]. I haven't seen this kind of syntax in c before and after searching for many hours I didn't find anything useful.
#include <stdio.h>
int main() {
int arr[] = {5,10,15};
printf("%d %d %d",arr[0,1]);
return 0;
}
outputs:
10 1762365112 1769491896
Can someone elaborate on this?
Because of how the comma operator (,) works in C, the effect of the (questionable and woefully hard to read) array[x=y,x+1] is to first copy the value of y into x and then use x+1 as the normal (single dimensional, non-ranged, or whatever you were expecting) index in to the array.
(I do not see the need to discuss your experimental code.)
See the link to documentation of comma operator, as kindly provided in comments by Pignotto:
https://en.cppreference.com/w/c/language/operator_other#Comma_operator

How to append literal C-string to literal C-string by a macro? [duplicate]

This question already has answers here:
How do I concatenate const/literal strings in C?
(17 answers)
Closed 1 year ago.
Considering that macros in C are processed by the preprocessor before compiling the project, as well as what is in macros with variables, etc. work as with text, is it possible in some way to add another to one line, but in such a way that it is performed at the level of complementing the source code, and not at runtime?
Example:
#define VERSION_CODE "1.0"
#define ADD_VERSION(base) (base!" !"VERSION_CODE)
int main() {
printf(ADD_VERSION("MyProgram"));
return 0;
}
Here, the !" symbol denotes the removal of the quote to the right of the argument and to the left of the existing line.
I would not like to perform this action at runtime, since all the constituent strings are already known.
You don't need to remove the quotes to do this - the compiler will remove them by itself. One of the features of the C-compiler is that it itself does the concatenation of literal strings. This works in both C and C ++.
You can write like this:
#define VERSION_CODE "1.0"
#define ADD_VERSION(base) (base VERSION_CODE)
int main() {
printf(ADD_VERSION("MyProgram"));
return 0;
}
As a result, you will receive the following output:
MyProgram1.0

Use of '??=', '??<' and '??>' in c [duplicate]

This question already has answers here:
What is the meaning of these strange question marks?
(1 answer)
"??*" C++ escape sequence [duplicate]
(1 answer)
Closed 2 years ago.
The community is reviewing whether to reopen this question as of 29 days ago.
I was going through few interview questions and I came across example as below. I tried the example for simple input/output and also for some logic and it works without any problems.
??=include <stdio.h>
int main(void)
??<
printf("Hello");
// Other code lines here
return 0;
??>
To my surprise, this worked without any compilation issue and output was as required.
What is the significance of '??=', '??<' and '??>' here ?
What is the significance of '??=', '??<' and '??>' here ?
??= will be replaced with #,
??< will be replaced with {,
??> will be replaced with },
by the preprocessor. These are called trigraphs. There are 9 trigraphs in total; the others are:
??( will be replaced with [,
??) will be replaced with ],
??/ will be replaced with \,
??' will be replaced with ^,
??! will be replaced with |,
??- will be replaced with ~.
Trigraphs are processed very early in the translation process, before the source code is tokenized. They can affect comments and strings and character literals.

What is happening in this C code? (From interview) [duplicate]

This question already has answers here:
Square of a number being defined using #define
(11 answers)
Closed 5 years ago.
I was given a variation of this C code in an interview recently, and asked what would be returned by the function.
#define fun(a) a*a
int main() {
return(fun(4+5));
}
I've run it with a printf("%d"...) in place of the return and it prints 29. No other types than "%d" showed a result. Can someone explain what is going on here?
a is expanded, not evaluated as a. So you get:
4+5*4+5
and with operator priorities; you get 4 + 20 + 5 => 29
better way (with extra outside parenthesis for protecting against outside operators too thanks to Thomas comment):
#define fun(a) ((a)*(a))
but all parenthesis of the world still don't protect against i++, function calls (with side effects, preferably)... so argument reusing in macros is always a problem (there's no syntax to declare & assign a temp variable either). Prefer inlined functions in that case.

Why i am not getting the expected output in the following c programme? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does “#define STR(a) #a” do?
Macros evaluation in c programming language
#include <stdio.h>
#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)
int main()
{
printf("%s\n",h(f(1,2)));
printf("%s\n",g(f(1,2)));
return 0;
}
I was expecting the output to be same for both the printf. But what I am getting is different(given below)
12
f(1,2)
can someone explain what is the reason and why is it happening in detail?
I extended your program with an additional line
printf("%d\n",f(1,2));
which, in turn, results into
printf("%d\n",12);
(called with gcc -E).
Your two lines result into
printf("%s\n","12");
printf("%s\n","f(1,2)");
What happens here?
f(1,2) is clear - 1 and 2 just get sticked together.
g(something) just reproduces something as a string, without treating it specially -> "f(1,2)".
h(something), in turn, lets the result of g(something) expand.
C standard states that macro arguments aren't expanded if they are stringified or concatenated. That is why g(YOUR_MACRO) YOUR_MACRO isn't expanded. However in h(YOUR_MACRO) case - h() does stringification indirectly and so it complies with C macro arguments expansion rules and is expanded further.
First one:
printf("%s\n",h(f(1,2)));
becomes:
g(12)
which in turn becomes
"12"
Second one:
printf("%s\n",g(f(1,2)));
becomes
"f(1,2)"
since # converts the argument to a string parameter.

Resources