Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with C function call

Posted on 2006-06-27
5
Medium Priority
?
287 Views
Last Modified: 2010-04-15
I have a quick question while trying to learn C programming from Sams Teach Yourself C Programming in 21 Days.

I'm trying to call a straightforward print function to main, and have the function print data in a certain pattern. I need a little guidance on how to print the following pattern:

Stop!Stop!Stop!
Stop!Stop!
Stop!

I understand how to do the function prototyping OK. I have defined the function as:

void stop(void)
{
     printf("Stop!");
}

Now, is the best approach to use a \n somewhere in the function call, like this:

int main(void)
{
      stop(), stop(), stop(); \n
      stop(), stop(); \n
      stop(); \n

Or to simply put the \n in the function definition, like this:

void stop(void)
{
     printf("Stop\n");
}

If the above is right (which I think is the case), how do I call this function to produce the pattern above?

Please respond as soon as possible.

Thanks for your help.
0
Comment
Question by:computerese
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 16995827
Hi computerese,

The best method here would be to use a loop. Two 'for' loops would do. The outer one will go from 3 downwards (how many 'Stop's per line) and another one INSIDE that one printing that many 'Stop's. Why dont you have a go at that and post code if you get stuck.

I'm trying to give you help rather than answers here as this would be classified as 'homework' here at EE.

Paul
0
 
LVL 14

Assisted Solution

by:cwwkie
cwwkie earned 1000 total points
ID: 16996593
and if you have not yet learned the for loop, you can also define a newline function like this:

void newline(void)
{
     printf("\n");
}

and,

> stop(), stop(), stop(); \n

there is no reason to use the comma operator here, you can simply use the semicolon.
0
 

Author Comment

by:computerese
ID: 16996981
To: cwwkie:

Thanks for your comments. I had been trying to use the \n at the end of the statements, as you suggest and as can be seen from the example I included with my question. But I was, and still am, getting the following error: stray "\" in program. Maybe I should have simply asked what would cause this error? Could you help me with that? Your other suggestion about creating a newline function worked great! But I am in the learning stage, obviously (and I am teaching this stuff to myself with just the book I mentioned--not in any class), so many subtleties of C syntax escape me and would really like enlightenment on why I'm getting this error.
0
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 1000 total points
ID: 16997074
>>      stop(), stop(), stop(); \n
You need somethink like:

      stop(); stop(); stop(); printf("\n");

Paul
0
 
LVL 14

Expert Comment

by:cwwkie
ID: 17004919
@computerese

> Maybe I should have simply asked what would cause this error?

I think that would have been better. And we do not know how much you have learned from c. So if we suggest something which you have not learned yet, just say so, there is always an alternative way to solve a problem.

The reason why you got an error on the \n, is because \n is not a valid statement. The easiest thing to remember for now is, you always have to do "something'. That can be calling a function, or assign a variable.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question