Click here to Skip to main content
15,941,973 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Explain me the second line of code. What will be the output?

What I have tried:

C
#include <stdio.h>
#define scanf "%s me "
int main()
{
  printf(scanf, scanf);
  getchar();
  return 0;
}
Posted
Updated 22-Nov-21 20:43pm
v2

#define is part of the C text preprocessor: it does a text replace of the first part with the second part. You can do more with it than that, but that's the general idea - all it ever does is replace text: C Language: #define Directive (macro definition)[^]

So everywhere in your code you write scanf in your code it will be replaced with "%s me "

#define is there to make code more readable:
C
#define true (1==1)
#define false (1==0)
#define strDbConnect "Server=[SqlDev];Database=[Accounts];password=[NONE];user = [ME]"
So you can write stuff like this:
C
while (true)
   {
   OpenSql(strDBConnect);
   ...
   }
And make it easy to read (and safer, because the DB string is only entered once in the whole program).
I agree with KarstenK - the code you show is just stupid nonsense.
 
Share this answer
 
The output would be
%s me  me 
Because the second line is telling the preprocessor to replace every occurrence of scanf with "%s me", so that the compiler will actually 'see':
C
int main()
{
  printf("%s me ", "%s me");
  getchar();
  return 0;
}

That said, such line, as pointed out by KarstenK, hides the declaration of the C standard I/O function scanf, fscanf, sscanf, scanf_s, fscanf_s, sscanf_s - cppreference.com[^].
No need to say it is something you should avoid (C programs maybe criptic on their own, no need to make them awkward).
 
Share this answer
 
v2
The second line is dangerous nonense. At first it is wrong and second it substitutes the scanf documentaion and example code.

Best is to visit some Learn C tutorial to learn understand and use the language.

Tip: install some IDE, like Visual Studio.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900