473,804 Members | 3,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function & header files

how to convert a program to a function/macro and put it in a header
file? is there any shortcut method for this?
thanks.

Jun 12 '07 #1
34 1983
Umesh said:
how to convert a program to a function/macro and put it in a header
file?
Don't even try. Instead, find out how to use libraries.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 12 '07 #2
On 12 Jun, 14:05, Richard Heathfield <r...@see.sig.i nvalidwrote:
Umesh said:
how to convert a program to a function/macro and put it in a header
file?

Don't even try. Instead, find out how to use libraries.
Now you've done it. If he picks up on this (he may not, as he
generally wants to be spoonfed a direct answer to his question), he'll
now ask "how to use libraries?" and we'll have a heap of "off-topic"
responses...

Jun 12 '07 #3
How can I convert a function to a macro in general? e.g.

void factorial(int n)
{
long int x=1;
for (int i=1;i<=n;i++)
x*=i;
printf("Factori al of %d = %ld\n",n,x);
}

Jun 12 '07 #4
Umesh wrote:

| How can I convert a function to a macro in general? e.g.
|
| void factorial(int n)
| {
| long int x=1;
| for (int i=1;i<=n;i++)
| x*=i;
| printf("Factori al of %d = %ld\n",n,x);
| }

Sorry - there is no "general solution". Please re-read Richard's
article.

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
Jun 12 '07 #5
Umesh said:
How can I convert a function to a macro in general? e.g.

void factorial(int n)
{
long int x=1;
for (int i=1;i<=n;i++)
x*=i;
printf("Factori al of %d = %ld\n",n,x);
}
You'd be better off asking "how can I avoid risking the generation of
incorrect results?"

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 12 '07 #6
Richard Heathfield <rj*@see.sig.in validwrites:
Umesh said:
>How can I convert a function to a macro in general? e.g.

void factorial(int n)
{
long int x=1;
for (int i=1;i<=n;i++)
x*=i;
printf("Factori al of %d = %ld\n",n,x);
}

You'd be better off asking "how can I avoid risking the generation of
incorrect results?"
He'd be *far* better off asking "How do I use this Usenet thing
without looking like a parasite?".

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 12 '07 #7
Umesh wrote:
>
How can I convert a function to a macro in general? e.g.

void factorial(int n)
{
long int x=1;
for (int i=1;i<=n;i++)
x*=i;
printf("Factori al of %d = %ld\n",n,x);
}
A function that declares local variables,
is a poor candidate for conversion to a macro.

/* BEGIN new.c */

#include <stdio.h>

#define factorial(N) \
do { \
n = N; \
x = i = 1; \
while (n >= i) { \
x *= i++; \
} \
printf("Factori al of %d = %ld\n", n, x); \
} while (0)

void (factorial)(int n);

int main(void)
{
long int x = 1, i = 1, n;

factorial(5);
(*factorial)(5) ;
return 0;
}

void (factorial)(int n)
{
long int x = 1, i = 1;

factorial(n);
}

/* END new.c */
--
pete
Jun 12 '07 #8
// factorial by macro

#include<stdio. h>
#define factorial(n) \
long int x=1; \
for(int i=1;i<=n;i++) \
x*=i; \
printf("Factori al of %d = %ld\n",n,x);\

main()
{
int n;
printf("Enter No.- ");
scanf("%d",&n);
factorial(n);
return 0;
}

Jun 13 '07 #9
Umesh said:
// factorial by macro

#include<stdio. h>
#define factorial(n) \
long int x=1; \
for(int i=1;i<=n;i++) \
x*=i; \
printf("Factori al of %d = %ld\n",n,x);\

main()
{
int n;
printf("Enter No.- ");
scanf("%d",&n);
factorial(n);
return 0;
}
foo.c:9: warning: return-type defaults to `int'
foo.c:9: warning: function declaration isn't a prototype
foo.c: In function `main':
foo.c:13: parse error before `long'
foo.c:13: parse error before `int'
foo.c:13: `i' undeclared (first use in this function)
foo.c:13: (Each undeclared identifier is reported only once
foo.c:13: for each function it appears in.)
foo.c:13: warning: statement with no effect
foo.c:13: parse error before `)'
foo.c:13: `x' undeclared (first use in this function)
make: *** [foo.o] Error 1

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 13 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
4967
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or anything...it just returns a "1" (whereas it should return a "0") as far as I can tell. I have read the PHP...
4
6073
by: webdev | last post by:
lo all, some of the questions i'll ask below have most certainly been discussed already, i just hope someone's kind enough to answer them again to help me out.. so i started a python 2.3 script that grabs some web pages from the web, regex parse the data and stores it localy to xml file for further use.. at first i had no problem using python minidom and everything concerning
0
1480
by: Daron S. Lowell | last post by:
I need to be able to obtain the dimensions of some graphics files. Most of what I have found in this group seems to indicate loading the files into a form, then obtaining the dimensions form a picture object. I have over 17K files that I would like to get the dimensions for, and this seems like it would be a bit time consuming. I found the following code. could this be modified and used in a form? I am using Access98. (from...
19
2519
by: Deniz Bahar | last post by:
Hi, I would like to call one of my functions the exact name as an existing C library function (for example K&R2 exercises asks me to make an atof function). If I don't include the header with the declaration for the C library function (stdlib.h in this case) then define/declare my own function with the same name, am I safe? It seems to work on my compiler, but I wonder if this is portable or even acceptable? thx
11
2380
by: bill | last post by:
I recently worked with a piece of code where dereferencing the pointer was too slow, and I was able to achieve a nearly 2x speed-up by replacing a local array of size 8 with 8 local variables. (*x requires 2 fetches, x requires 1, so it's easy to explain it, I was just surprised that I actually encountered a situation where it makes sense to do this optimization.) Now, I want to test a similar situation, but the array that will be...
4
13226
by: thinktwice | last post by:
i have just made a test project :(win32 console) //file : func.h #ifndef _FUNC_H_ #define _FUNC_H_ void func1() { return; };
2
2548
by: Jack | last post by:
I have a chunk of code that loads a few dozen function pointers into global variables. I'm concerned with unused memory consumption. What if the client only needs to use one or two functions? Then there's quite a few function pointers consuming memory and going to waste. Here's little example: // mycode.cpp or mycode.c typedef int (*PFN) (); PFN g_pfn;
18
2648
by: mdh | last post by:
May I ask the following. By K&R's own admission, the example used to describe function pointers is complex ( on P119). In addition, the use of casts has been stated by some on this group as being, again, a poor/bad example of it's use. For the moment, accepting these criticisms, I would still like to get some insight into why/how some things work as even poor code is enlightening, to me at least. The example uses K&R's version of...
16
3291
by: Xiaoxiao | last post by:
Hi, I got a C library, is there a way to view the public function names in this library so that I can use in my C program? Thanks.
0
9704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9569
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10318
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9130
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2975
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.