LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-18-2018, 09:32 AM   #16
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,623
Blog Entries: 19

Rep: Reputation: 4462Reputation: 4462Reputation: 4462Reputation: 4462Reputation: 4462Reputation: 4462Reputation: 4462Reputation: 4462Reputation: 4462Reputation: 4462Reputation: 4462

Quote:
Originally Posted by shlomo.hovir View Post
my language is c
can u tell me why would someone use macros in c for substiuate a variable with a value
One reason is to make the program run faster. When you have a function running, there's quite a lot of overhead. The function's variables have to be loaded onto the stack when the function is called. Then they have to be unloaded again when the function exits.

For a big, multi-line function, that overhead is acceptable. For a one-line function it wouldn't be, so you use a macro instead. Macros are created with #DEFINE (like named constants), so they are handled by the C preprocessor. Basically it's just a global edit; the macro call is replaced throughout by the line of code that it represents. Result: when the program runs, nothing has to be called, but you still have the convenience of typing in a name with some arguments rather than a line of complex code.
 
Old 10-18-2018, 12:54 PM   #17
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,242

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
Quote:
still didn't get it completly

Code:
circumference = radius * 2 * pi;
The macro is called "circumference"

So, whenever you call the macro "circumference" the word is replaced with the whole contents of the text.

Exactly the same as in any text editor with macros.

why would anyone substitue a variable with a value???
Quote:
Originally Posted by shlomo.hovir View Post
my language is c
can u tell me why would someone use macros in c for substiuate a variable with a value
The circumference macro you're thinking of would look more like this:

Code:
#define circumference radius * 2 * pi
And why would anyone do that? The answer, obviously, is because they're idiots.

Related:

What is the worst real-world macros/pre-processor abuse you've ever come across?

Last edited by dugan; 10-18-2018 at 01:49 PM.
 
Old 10-18-2018, 01:47 PM   #18
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
On the other hand, non-idiots use macros, too, e.g.
Code:
#define circumference(radius) ((radius) * 2 * pi)
 
Old 11-11-2018, 09:30 PM   #19
shlomo.hovir
Member
 
Registered: Oct 2018
Posts: 66

Original Poster
Rep: Reputation: Disabled
Macros for conditonal compiling

Can somebody show me an exams about how to write macros that are used for conditional compiling like Unix

I wanna learn writing them to implement the. Further in my software

Ps : must be macros that used for conditional compiling
 
Old 11-12-2018, 02:20 AM   #20
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,949

Rep: Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325
I'm sorry, but I do not really understand.
At first you need to specify the language you use. (I assume now it is C or C++). But in C, C++ all the macros are used in the same way. So probably would be better to give us more details or show some examples what do you mean.
 
Old 11-12-2018, 02:32 AM   #21
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
Quote:
Originally Posted by shlomo.hovir View Post
Can somebody show me an exams about how to write macros that are used for conditional compiling like Unix

I wanna learn writing them to implement the. Further in my software

Ps : must be macros that used for conditional compiling
This is the fourth thread that you have opened about macro programming in the past few weeks. Macros for conditional compilation is not a significantly unique use case to begin a new thread without having shown that you have learned from the others, so I am merging this post into your most recent thread on the subject.

Please do not continue this behavior in the future, and again, please review the Site FAQ to which you have been previously referred for acceptable use of this forum.
 
Old 11-27-2018, 05:15 PM   #22
bkelly
Member
 
Registered: Jan 2008
Distribution: Centos 7-4
Posts: 205

Rep: Reputation: 13
I don't see much about C and C++ and nothing on the lines I am thinking of so here goes my maybe first answer to another question.
In C and C++ you can start a line with:
Code:
#define THIS  THAT
and every time the compiler files THIS it replaces it with THAT. The THAT part can be quite lengthy and can have variables such that you have a macro with arguments. That topic is broad and deep and I stop here.

However, I do wish to note: The Joint Strike Fighter (JSF) program writes code for aircraft in C++ and has published their programming guidelines. Its easy to find with a search. Those guidelines declare that macros are not allowed. They can make the code extremely difficult to understand. You may have to force the compiler to save the preprocessor output so you can see what those macros have done to your code.
So,..., if you are at the point where you need to ask about macros, then the best advice is: Don't use them.

Part 2
In Microsoft Excel, Word, Access, and others, you can write VBA (Visual Basic for Application) code to do things. There is specialized grammar/syntax for the various Office applications. Such code is frequently, sometime invariably, called a macro. It is not, but it is called that anyway. If you want to go further this will be enough to get you started.

Last edited by bkelly; 11-27-2018 at 05:18 PM. Reason: typo
 
Old 11-27-2018, 05:32 PM   #23
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Writing C/C++ Macros: Rules, Tricks and Hints http://www.ebyte.it/library/codesnip...CppMacros.html

It's actually a really good read.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming in M4 macro; quotation issue BenJZ Programming 1 05-27-2014 05:02 AM
explain eof in c programming. batman4 Programming 7 02-07-2013 09:20 AM
Could someone please explain the concept of associative arrays in AWK programming? AJAY E Linux - Newbie 6 05-27-2012 07:01 PM
C: programming macro recorder and pixel reader ganyks Programming 4 12-02-2009 08:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration