What is wrong?

  • Thread starter federico_bertola
  • Start date
F

federico_bertola

Hi everybody!
I have this function:

int Scan(char String[])
{
printf("%s", String);
}

it works but when I try to pass a dotted string o separetad with (" ",
"_" , "-" ...ecc)
it return only first word.
I pass the string in this way:

char CString[] = "I.am.an.example";
Scan(CString);

What's wrong?
Help me please!
Thx!
 
S

santosh

Hi everybody!
I have this function:

int Scan(char String[])
{
printf("%s", String);
}

You declared the function as returning as int but you don't return
anything. Consider changing it to:
void Scan(char String[]);
it works but when I try to pass a dotted string o separetad with (" ",
"_" , "-" ...ecc)
it return only first word.

Well it should print all the characters in the array upto the first
null character. Certainly dots and dashes should be printed.

Have you checked that your array doesn't contain any null characters
embedded before the end of valid data?
I pass the string in this way:

char CString[] = "I.am.an.example";
Scan(CString);

What's wrong?

Until you can show us a complete, compilable example that exhibits your
problem, (copy & paste the code, don't retype it), we can do much more
than keep guessing.
 
R

Richard Bos

int Scan(char String[])
{
printf("%s", String);
}

it works but when I try to pass a dotted string o separetad with (" ",
"_" , "-" ...ecc)
it return only first word.

It returns nothing; presumably you mean that it prints that.
I pass the string in this way:

char CString[] = "I.am.an.example";
Scan(CString);

What's wrong?

Something else.

Post a small, compilable sample of code that demonstrates the problem.
It is not in the uncompilable snippets you posted above.

For example, when I compile and run this program:

#include <stdio.h>

int Scan(char String[])
{
printf("%s", String);
}

int main(void)
{
char CString[] = "I.am.an.example";

Scan(CString);

getchar();
return 0;
}

the result is that I.am.an.example is printed, in its entirety.

Richard
 
S

Suman

Richard said:
int Scan(char String[])
{
printf("%s", String);
}

it works but when I try to pass a dotted string o separetad with (" ",
"_" , "-" ...ecc)
it return only first word.

It returns nothing; presumably you mean that it prints that.
I pass the string in this way:

char CString[] = "I.am.an.example";
Scan(CString);

What's wrong?

Something else.

Post a small, compilable sample of code that demonstrates the problem.
It is not in the uncompilable snippets you posted above.

For example, when I compile and run this program:

#include <stdio.h>

int Scan(char String[])
{
printf("%s", String);
}

int main(void)
{
char CString[] = "I.am.an.example";

Scan(CString);

getchar();
return 0;
}

the result is that I.am.an.example is printed, in its entirety.

I don't believe that! (I mean, from you, this little slip.)
(Look at http://snipurl.com/iqxx)
 
R

Richard Bos

Suman said:
Richard said:
int Scan(char String[])
{
printf("%s", String);
}

it works but when I try to pass a dotted string o separetad with (" ",
"_" , "-" ...ecc)
it return only first word.

It returns nothing; presumably you mean that it prints that.
I pass the string in this way:

char CString[] = "I.am.an.example";
Scan(CString);

What's wrong?

Something else.

Post a small, compilable sample of code that demonstrates the problem.
It is not in the uncompilable snippets you posted above.

For example, when I compile and run this program:

#include <stdio.h>

int Scan(char String[])
{
printf("%s", String);
}

int main(void)
{
char CString[] = "I.am.an.example";

Scan(CString);

getchar();
return 0;
}

the result is that I.am.an.example is printed, in its entirety.

I don't believe that!

You are free to disbelieve that the earth is pentangular, for all I
care. I wrote that when _I_ run that program, it prints the entire
string; and it does. As for the final newline, the cursor does indeed
start after the printed string, without a newline in between, and a
newline is only printed when I press enter; this is one legal result of
the program.

Richard
 
F

federico_bertola

As you want, the original source is...

File Sample.h

void Scan(char String[])
{
printf("%s", String); // this return only 1 word
}


File main.c

#include <stdio.h>
#include "Sample.h"

int main()
{
char SampleString[] = "I.am.a.sample.string";
Scan(SampleString);
return 0;
}


Help me!
 
S

santosh

As you want, the original source is...

File Sample.h

void Scan(char String[])
{
printf("%s", String); // this return only 1 word
}

Why are putting the function definition in a header file? Place the
definition in main.c and only the declaration in Sample.h

By the way, you don't provide a function declaration at all.

Also include a newline character at the end of your printf() format
string. Otherwise, the output is not guaranteed to appear on the output
device.
File main.c

#include <stdio.h>
#include "Sample.h"

int main()

Use the form int main(void)
{
char SampleString[] = "I.am.a.sample.string";
Scan(SampleString);
return 0;
}

The program should print the string correctly. Your code compiles and
behaves as it should on my system. I suggest adding a newline at the
end of the printf() statement in Scan(). What is the exact output that
you're getting?
 
A

Andrew Poelstra

int Scan(char String[])
{
printf("%s", String);
}

it works but when I try to pass a dotted string o separetad with (" ",
"_" , "-" ...ecc)
it return only first word.

It returns nothing; presumably you mean that it prints that.

Actually, I wouldn't doubt that some register issue is causing it
to in fact return the first word. Since dropping off the end of a
non-void function is undefined, this is more than plausible.

That and the fact that if this were true, he would be returning a
pointer as an int... the implications of UB are very interesting.
 
B

Bart Rider

As you want, the original source is...

File Sample.h

void Scan(char String[])
{
printf("%s", String); // this return only 1 word
}


File main.c

#include <stdio.h>
#include "Sample.h"

int main()
{
char SampleString[] = "I.am.a.sample.string";
Scan(SampleString);
return 0;
}


Help me!

No problem here. The program just prints the entire string
'I.am.a.sample.string' and exits.

So what do you think is wrong with it? Have you tested this
code snippet be yourself? Only this snippet, not embedded in
a greater project. Maybe you have some function overloading,
because by the name Scan seems to do something different
than only printing.

Bart
 
A

Andrew Poelstra

As you want, the original source is...

File Sample.h

void Scan(char String[])

You should declare, not define, functions in headers. The below belongs
in your .c file.
{
printf("%s", String); // this return only 1 word

I can't imagine how you know that, being as you don't check printf()'s
return value. Also, // is a syntax error in C89, which no doubt you are
supposed to be using.

Also, that statement is equivilant to puts (string); which is likely more
efficient, not to mention easier to read.
}

File main.c

#include <stdio.h>
#include "Sample.h"

int main()

int main(void) is clearer.
{
char SampleString[] = "I.am.a.sample.string";

Indent properly with 2 or 4 spaces.
Scan(SampleString);

This function would be better-named Print, no?
return 0;
}

Help me!


Other than that, it compiles cleanly. And it behaves exactly as
others have said that it does.

However, SampleString does not have a \n at the end, and so you
have no guarantee that anything will display at all.
 
R

Richard Bos

Also, that statement is equivilant to puts (string); which is likely more
efficient, not to mention easier to read.

Not quite. That would be printf("%s\n", String);.

Richard
 
E

Eric Sosman

Andrew said:
As you want, the original source is...
[...]
printf("%s", String); // this return only 1 word
[...]
Also, that statement is equivilant to puts (string); which is likely more
efficient, not to mention easier to read.

Not quite equivalent: puts() adds a newline.
 
A

Andrew Poelstra

Not quite. That would be printf("%s\n", String);.

His original statement /should/ have been printf ("%s\n", String); and
that was likely the cause of his original problem.

You are correct, though; I completely missed that. Guess I was looking
at the intent of the code instead of the actual code... good thing we
have quick compilers nowadays.
 
K

Keith Thompson

Andrew Poelstra said:
His original statement /should/ have been printf ("%s\n", String); and
that was likely the cause of his original problem.

Actually, I'd be surprised if that were the case. It's
implementation-defined whether the newline is required, and if it is
required and you don't supply it then printing a single word is one
legal consequence. But realistically, I doubt that any system the OP
is likely to be using actually does require the newline, and even if
it does, I'd expect the error to appear as nothing being printed at
all.

The OP needs to add the newline and try again, and he needs to
understand the difference between returning something and printing
something.
 
A

Andrew Poelstra

Actually, I'd be surprised if that were the case. It's
implementation-defined whether the newline is required, and if it is
required and you don't supply it then printing a single word is one
legal consequence. But realistically, I doubt that any system the OP
is likely to be using actually does require the newline, and even if
it does, I'd expect the error to appear as nothing being printed at
all.

Out of all the problems with his code, the missing \n seemed the most
likely cause of invalid output. Falling off of the int-returning
function occured /after/ the printf, and most of the other issues were
style-related.

Also, consider that everyone else who tried his code got correct results;
this suggests that the OP was using some uncommon platform or compiler.
But of course that's just speculation on my part.
The OP needs to add the newline and try again, and he needs to
understand the difference between returning something and printing
something.

Absolutely true.
 
S

Suman

[code relying on imp. defined behavior]
You are free to disbelieve that the earth is pentangular, for all I
care. I wrote that when _I_ run that program, it prints the entire
string; and it does. As for the final newline, the cursor does indeed
start after the printed string, without a newline in between, and a
newline is only printed when I press enter; this is one legal result of
the program.

<fumes>
I don't understand why you'd need such an analogy, for all _I_ care.
Simply
put, I expect one to be atleast as vigilant as you expect the others to
be.
Slip-ups happen, you'd better take them in stride.
</fumes>
 
A

Andrew Poelstra

[code relying on imp. defined behavior]
You are free to disbelieve that the earth is pentangular, for all I
care. I wrote that when _I_ run that program, it prints the entire
string; and it does. As for the final newline, the cursor does indeed
start after the printed string, without a newline in between, and a
newline is only printed when I press enter; this is one legal result of
the program.

<fumes>
I don't understand why you'd need such an analogy, for all _I_ care.

If you reorganize that sentence, you said "For all I care, I don't
understand..." (I didn't change the grammatical meaning whatsoever).
Simply
put, I expect one to be atleast as vigilant as you expect the others to
be.

Keith is much more vigilant than he expects most people to be. For
example, I can't imagine that he expects much from you.
Slip-ups happen, you'd better take them in stride.

In most cases, it's best to stop and fix slip-ups, especially in
mission-critical code. That isn't what "take in stride" means, just
so you know.

Being angry is no way to get help; step away from the keyboard for
a few hours.
 
S

Suman

Andrew said:
[ ...]
<fumes>
I don't understand why you'd need such an analogy, for all _I_ care.

If you reorganize that sentence, you said "For all I care, I don't
understand..." (I didn't change the grammatical meaning whatsoever).

I wanted to know why Richard Bos needed that analogy. Period.
Keith is much more vigilant than he expects most people to be. For
example, I can't imagine that he expects much from you.

I thought I had quoted enough to tell _anyone_ I was replying to
Richard Bos.
In most cases, it's best to stop and fix slip-ups, especially in
mission-critical code. That isn't what "take in stride" means, just
so you know.

Re-read. For all you know, you might start reading a bit more into my
post.
Being angry is no way to get help; step away from the keyboard for
a few hours.

Where are you? (I mean, are you having difficulty following the thread?)
 
A

Andrew Poelstra

Andrew said:
Richard Bos wrote:
[ ...]
<fumes>
I don't understand why you'd need such an analogy, for all _I_ care.

If you reorganize that sentence, you said "For all I care, I don't
understand..." (I didn't change the grammatical meaning whatsoever).
I wanted to know why Richard Bos needed that analogy. Period.

Which analogy? If you mean the pentagonal earth one, I think he was trying
to help you understand that you can't respond to helpful replies with "I
don't believe you" and expect to be taken seriouly.
I thought I had quoted enough to tell _anyone_ I was replying to
Richard Bos.

My mistake! I was working on another thread in which Keith Thompson
was involved.
Re-read. For all you know, you might start reading a bit more into my
post.

I read some helpful replies, and I read you refusing them. I certainly
can't see how it would be worth my time to go reread those; I myself
pointed out the problems with your code.
Where are you? (I mean, are you having difficulty following the thread?)

No, I just didn't read the attribution line. Mistakes happen. Take them
in stride. :)
 
M

Mark McIntyre

Andrew said:
Richard Bos wrote:
[ ...]
<fumes>
I don't understand why you'd need such an analogy, for all _I_ care.

If you reorganize that sentence, you said "For all I care, I don't
understand..." (I didn't change the grammatical meaning whatsoever).

I wanted to know why Richard Bos needed that analogy. Period.

Then you might want to have said that. What you instead said was "I
don't understand why you need that analogy, I don't care". Which makes
no sense!
Re-read. For all you know, you might start reading a bit more into my
post.

Frankly, as a native english speaker, I was pretty confused as to what
you meant.

By teh way, if you plan to criticize people's code, you should make
your criticism explicit. "I don't believe you" and "I expect you to be
vigilant" without any supporting evidence does not give anyone any
reason to take your posts seriously.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top