• C

Help with C function call

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.
computereseAsked:
Who is Participating?
 
PaulCaswellConnect With a Mentor Commented:
>>      stop(), stop(), stop(); \n
You need somethink like:

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

Paul
0
 
PaulCaswellCommented:
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
 
cwwkieConnect With a Mentor Commented:
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
 
computereseAuthor Commented:
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
 
cwwkieCommented:
@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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.