A C-Program Question

K

Kotni

Write a function of type void fn (void)
so that the o/p of the program should be anything other than 20.
(Dont use #define fn()..... )

main() {
int i=20;
fn();
printf("%d \n",i);
}
 
J

Joona I Palaste

Kotni said:
Write a function of type void fn (void)
so that the o/p of the program should be anything other than 20.
(Dont use #define fn()..... )
main() {
int i=20;
fn();
printf("%d \n",i);
}

void fn(void) {
}

The output of the program will be "20 ", i.e. 20 followed by two
spaces.
 
A

Al Bowers

Kotni said:
Write a function of type void fn (void)
so that the o/p of the program should be anything other than 20.
(Dont use #define fn()..... )

main() {
int i=20;
fn();
printf("%d \n",i);
}

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


void fn(void)
{
int i;

srand((unsigned)time(NULL));
while((i = rand()) == 20);
printf("%d \n",i);
exit(EXIT_SUCCESS);
}

int main() {
int i=20;
fn();
printf("%d \n",i);
return 0;
}
 
T

Thomas Matthews

Kotni said:
Write a function of type void fn (void)
so that the o/p of the program should be anything other than 20.
(Dont use #define fn()..... )

main() {
int i=20;
fn();
printf("%d \n",i);
}

#include <stdio.h>
#include <stdlib.h>

void fn (void)
{
puts("This program recurses too much.\n");
return fn();
}

int main(void)
{
int i = 20;
fn();
printf("%d \n", i);
return EXIT_SUCCESS;
}


Nobody said it had to end successfully.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
E

Eric Sosman

Thomas said:
#include <stdio.h>
#include <stdlib.h>

void fn (void)
{
puts("This program recurses too much.\n");
return fn();
}

int main(void)
{
int i = 20;
fn();
printf("%d \n", i);
return EXIT_SUCCESS;
}

Nobody said it had to end successfully.

Nobody said it had to compile successfully, either.

(Hint: What is the type of the expression in the
`return' statement?)
 
L

Lew Pitcher

Kotni said:
Write a function of type void fn (void)
so that the o/p of the program should be anything other than 20.
(Dont use #define fn()..... )

main() {
int i=20;
fn();
printf("%d \n",i);
}

#include <stdio.h>

void fn()
{
putchar('1');
}

int main(void)
{
int i=20;
fn();
printf("%d \n",i);
return 0;
}

/* Q E D */


--
Lew Pitcher

Master Codewright and JOAT-in-training
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
 
P

Peter Shaggy Haywood

Groovy hepcat Kotni was jivin' on 31 Oct 2003 02:09:20 -0800 in
comp.lang.c.
A C-Program Question's a cool scene! Dig it!
Write a function of type void fn (void)
so that the o/p of the program should be anything other than 20.
(Dont use #define fn()..... )

main() {
int i=20;
fn();
printf("%d \n",i);
}

Why repost? We saw this problem the first time you posted it.
Anyhow, how 'bout this?

void fn(void)
{
printf("anything other than ");
}

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

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

Forum statistics

Threads
474,093
Messages
2,570,614
Members
47,230
Latest member
RenaldoDut

Latest Threads

Top