calling other functions

R

Richard Bos

Dilbert said:
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
 
D

Dilbert

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
 
A

Andreas Kahari

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;
}
 
A

Allan Bruce

Dilbert said:
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
 
F

Florian Weingarten

Dilbert said:
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
 
F

Florian Weingarten

Irrwahn Grausewitz said:

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
 
A

Arthur J. O'Dwyer

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
 
S

Sheldon Simms

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]$
 
I

Irrwahn Grausewitz

Florian Weingarten said:
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
 
I

Irrwahn Grausewitz

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

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

Regards
 
F

Florian Weingarten

Irrwahn Grausewitz said:
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.


Flo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top