473,499 Members | 1,551 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

calling other functions

Hi there, was just wondering whats the best way to call a function from
within another function.

say ive got
int main (int argc, char * argv[]) {
return 0;
}

and another function such as:
void getInput(char name) {
printf("Hello");
.....................
}

and i wanted it, so as soon as the program starts it goes straight to the
getInput function..

i tried; in the main function:
getInput(char name);
void getInput(char name);
getInput;
getInput();

none seemed to work

Any ideas?
Thank

Nov 13 '05 #1
11 1503
In article <Ts****************@newsfep4-glfd.server.ntli.net>, Dilbert wrote:
Hi there, was just wondering whats the best way to call a function from
within another function.

say ive got
int main (int argc, char * argv[]) {
return 0;
}

and another function such as:
void getInput(char name) {
printf("Hello");
.....................
}

and i wanted it, so as soon as the program starts it goes straight to the
getInput function..


Your getInput() function expects a character as its argument.
Call it with a character.

#include <stdio.h>

void getInput(char name)
{
printf("Hello, got '%c'\n", name);
}

int main(void)
{
getInput('Q');
return 0;
}
--
Andreas Kähäri
Nov 13 '05 #2
"Dilbert" <Ha************@memail.com> wrote:
i tried; in the main function:
getInput(char name);
void getInput(char name);
getInput;
getInput();

none seemed to work

Any ideas?


Yes. Get a C book. I'd recommend K&R. This really is a question at such
a basic level that you should acquaint yourself a bit more (read: at
all) with the language before trying things like newsgroups and web
searches.

Hint: function(actual argument), _not_ function(type) or
function(declaration).

Richard
Nov 13 '05 #3

"Dilbert" <Ha************@memail.com> wrote in message
news:Ts****************@newsfep4-glfd.server.ntli.net...
Hi there, was just wondering whats the best way to call a function from
within another function.

say ive got
int main (int argc, char * argv[]) {
return 0;
}

and another function such as:
void getInput(char name) {
printf("Hello");
this printf() is a function, so thats a function within a function
.....................
}

and i wanted it, so as soon as the program starts it goes straight to the
getInput function..

i tried; in the main function:
getInput(char name);
void getInput(char name);
getInput;
getInput();

none seemed to work
getInput requires a char, now there are three things wrong here,
1) a char can only hold one character, so this should accept a char pointer
or a char array to hold a name
2) the function name does not hint what the function does, its called
getInput but doesnt actually get any input.
3) You pass a paramater (char name) and never actually use it

All in all, I suggest you get yourself a book in c (I dont recommend K&R as
your first book, but you should read it eventually), and study up a bit
more.
HTH
Allan


Any ideas?
Thank

Nov 13 '05 #4
Dilbert <Ha************@memail.com> wrote:
void getInput(char name) {
printf("Hello");
Ouch! Dont use printf here. puts() would be better.
.....................
} i tried; in the main function:
getInput(char name);
void getInput(char name);
getInput;
getInput();
How about getInput('x'); or something like this? Your function requires a
char as parameter!
Any ideas?


The C Programming Language
by Brian Kerningham and Dennis Ritchie
Flo
--
Give me about 10 seconds to think for a minute.
Florian Weingarten / Use PGP! (0x65C91285)
Nov 13 '05 #5
Florian Weingarten <fw@go.cc> wrote:
Dilbert <Ha************@memail.com> wrote:
void getInput(char name) {
printf("Hello");


Ouch! Dont use printf here. puts() would be better.


Why?
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #6
Irrwahn Grausewitz <ir*******@freenet.de> wrote:
> printf("Hello");


Ouch! Dont use printf here. puts() would be better.


Why?


Because printf() should only be used for outputting formated strings. puts()
is for printing strings which do not need to be formated and as far as I
know, puts() is much smaller and faster, so puts() should always be used
instead of printf() if possible IMHO.
Flo
--
Give me about 10 seconds to think for a minute.
Florian Weingarten / Use PGP! (0x65C91285)
Nov 13 '05 #7

On Fri, 14 Nov 2003, Florian Weingarten wrote:

Irrwahn Grausewitz <ir*******@freenet.de> wrote:
> printf("Hello");

Ouch! Dont use printf here. puts() would be better.


Why?


Because printf() should only be used for outputting formated strings.
puts() is for printing strings which do not need to be formated and
as far as I know, puts() is much smaller and faster, so puts() should
always be used instead of printf() if possible IMHO.


One caveat:

% cat test.c

#include <stdio.h>
int main(void)
{
printf("Hello");
puts("Hello");
printf("World");
puts("World");
return 0;
}

% gcc -o test test.c
% ./test
HelloHello
WorldWorld
%

puts(foo) actually prints foo to stdout, and then *adds a newline*!
printf(foo) will not add that newline, and neither will fputs(foo,
stdout). puts() is special that way.
MMV, anyway -- I prefer to use printf() consistently, except in
temporary debugging output where puts() is faster to type and I
don't have to manually insert the "\n".
Oh, and one printf() caveat: Remember that the '%' character is
special in printf(), but not in puts().

puts("100%");
/* is equivalent to */
fputs("100%\n", stdout);
/* is equivalent to */
printf("100%%\n");

-Arthur
Nov 13 '05 #8
On Fri, 14 Nov 2003 21:48:53 +0100, Florian Weingarten wrote:
Irrwahn Grausewitz <ir*******@freenet.de> wrote:
> printf("Hello");

Ouch! Dont use printf here. puts() would be better.


Why?


Because printf() should only be used for outputting formated strings. puts()
is for printing strings which do not need to be formated and as far as I
know, puts() is much smaller and faster, so puts() should always be used
instead of printf() if possible IMHO.


Tell us why we should care if puts() returns 10 nanoseconds faster than
printf().

And take a look at this:

[sheldon@wsxyz mcc]$ cat test.c
#include <stdio.h>
int main (void)
{
printf("Hello World!\n");
return 1;
}
[sheldon@wsxyz mcc]$ gcc -Wall -W -O2 -S test.c
[sheldon@wsxyz mcc]$ cat test.s
.file "test.c"
.section .rodata.str1.1,"aMS",@progbits,1
..LC0:
.string "Hello World!"
.text
.p2align 4,,15
..globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $.LC0, (%esp)
call puts <-------------- Good GCC!
movl %ebp, %esp
movl $1, %eax
popl %ebp
ret
.size main, .-main
.ident "GCC: (GNU) 3.3.1"
[sheldon@wsxyz mcc]$
Nov 13 '05 #9
Florian Weingarten <fw@go.cc> wrote:
Irrwahn Grausewitz <ir*******@freenet.de> wrote:
> printf("Hello");

Ouch! Dont use printf here. puts() would be better.


Why?


Because printf() should only be used for outputting formated strings. puts()
is for printing strings which do not need to be formated and as far as I
know, puts() is much smaller and faster, so puts() should always be used
instead of printf() if possible IMHO.


Note that printf("Hello") and puts("Hello") are not equivalent,
as puts appends a newline to the output. fputs("Hello", stdout);
would have been equivalent. However, if you printf a constant string
ending in a newline, any decent optimizing compiler will probably
insert a call to puts in the code.

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #10
Sheldon Simms <sh**********@yahoo.com> wrote:

[...]
printf("Hello World!\n"); [...] call puts <-------------- Good GCC!

[...]

Just as I thought. Thanks for the sample proving my
assumption elsethread.

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #11
Irrwahn Grausewitz <ir*******@freenet.de> wrote:
Note that printf("Hello") and puts("Hello") are not equivalent,
Yeah. Sure. My fault.
as puts appends a newline to the output. fputs("Hello", stdout);
would have been equivalent. However, if you printf a constant string
ending in a newline, any decent optimizing compiler will probably
insert a call to puts in the code.
Ok, thank you for explanation.
Regards

Flo
--
Give me about 10 seconds to think for a minute.
Florian Weingarten / Use PGP! (0x65C91285)
Nov 13 '05 #12

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

Similar topics

2
3209
by: pieter.breed | last post by:
Hi All, Is it possible to export a c# method into a dll in such a way that your "normal" C application can then call this method? To be clear: I am not asking how to use "DllImport" or...
19
4218
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
5
2204
by: Dave | last post by:
does calling a regular function cost any cpu time? In other words, is it faster to write the code of two functions into main(), or is it the exact same thing as calling two functions. I know its...
1
1775
by: Mark Jerde | last post by:
Yesterday I posted the message below to microsoft.public.dotnet.languages.vb and microsoft.public.vc.language. The two replies are also posted. I need to write some ISO C++ functions, more...
1
2880
by: Jesse McGrew | last post by:
Hi all, I'm trying to make a plugin DLL for a third-party application, using VC++ .NET 2003. This DLL acts as a bridge between the C++ plugin API of the application, and my actual plugin code...
1
2579
by: H.B. | last post by:
Hi, I need to make a function that can display data on my Managed C++ app and be called by an unmanaged C++ DLL. Something like : void Form1::Form1_Load(System::Object * sender,...
2
2803
by: Daniel Lidström | last post by:
I'm using a library called fyba. This library reads and writes files in a format called sosi. fyba uses the following code to determine if the calling process has own methods to handle errors,...
18
4310
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
4
4775
by: Edwin Gomez | last post by:
I'm a C# developer and I'm new to Python. I would like to know if the concept of Asynchronous call-backs exists in Python. Basically what I mean is that I dispatch a thread and when the thread...
10
3233
by: sulekhasweety | last post by:
Hi, the following is the definition for calling convention ,which I have seen in a text book, can anyone give a more detailed explanation in terms of ANSI - C "the requirements that a...
0
7130
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,...
0
7007
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...
0
7220
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...
1
6893
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...
0
7386
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...
0
5468
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,...
1
4918
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...
0
4599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
295
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...

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.