Hi all

I'm wondering if some one could help me with something as i think i've just forgotten everything i knew :)
I have this code:

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

int main()
{
     char A[4];
     char B[2];
     char C[2];

     printf("Enter Numbers: ");
     scanf("%s",&A);
     fflush(stdin);
     printf("Enter Numbers: ");
     scanf("%s",&B);
     fflush(stdin);
     printf("Enter Numbers: ");
     scanf("%s",&C);
     fflush(stdin);

     puts(A);
     puts(B);
     puts(C);

     return(0);
}

When the program is running if i input;

1234
56
78

The output for the puts() functions shows

78561234
7856
78

I'm very confused as to why the output shows this as i would have expected the same as what i put in.

If i try and input only single numbers such as ;

1
2
3

I get the expected output.
I'm really sorry if i'm being stupid, i just don' understands whats happening.

Maybe i should get some sleep.

Hey,

Remove the ampersand & from the scanf() line of code. Strings don't work with that, just numbers and stuff.

Your char arrays are very small. Remember that all strings need a space for the end of string marker: '\0', which scanf() will try and place onto the end of the string.

You can't flush your kitchen faucet, only the toilet will flush. fflush(stdin), is undefined. Use fflush() with OUTWARD streams, only, not INWARD streams like stdin.

use getchar(); to remove the last newline from the key buffer stream. It IS necessary, sometimes.

Hi Adak

Thank you very much for the advice, it turns out it was a space issue with my variables.

As you said, they need space for the '\0' byte character but I was not taking into consideration the '\n' when pressing return so there was no room for the NULL character.

I just added an extra byte to each variable to test and magic! it works without problem now.

Can I just clarify, you mentioned not to use the '&' in the scanf() function with strings. I've never come across that (or I just missed it), so is it like the same reason to not use it just as you don't need it when initialising a pointer to an array?

Many thanks

Hi Adak

Thank you very much for the advice, it turns out it was a space issue with my variables.

As you said, they need space for the '\0' byte character but I was not taking into consideration the '\n' when pressing return so there was no room for the NULL character.

I just added an extra byte to each variable to test and magic! it works without problem now.

Can I just clarify, you mentioned not to use the '&' in the scanf() function with strings. I've never come across that (or I just missed it), so is it like the same reason to not use it just as you don't need it when initialising a pointer to an array?

Many thanks

its no difference in string weather you write & or not in string because the string name saves the address it self.
;)

its no difference in string weather you write & or not in string because the string name saves the address it self.
;)

Actually, there's a subtle but significant difference. The address may be the same, but the type is different. If you pass an object of a different type than scant() expects, the result is undefined behavior. In many cases, undefined behavior means working the same way as if the correct type were passed, but that doesn't excuse doing things the wrong way.

Thanks for your help all

I really appreciate it!!!!

There is no such thing as undefined behavior in C. It's programmers fault. The C compilers were made over a layer of abstractions. Everything is generalized. Only floating point wasn't and that was taken care of later. Anyways, my point is that there is no such thing as "UNDEFINED BEHAVIOR". You can sue the compiler maker for such problems!

Let me tell how scanf printf work
try

printf("hello\0world");

It will printf only hello because printf was defined that it takes till NULL character is encountered. Scanf too works this way
Try

char s[10];
scanf("%s",s);
puts(s);

Input two words and see that it prints only 1. This is because scanf was designed to take till \0 is encountered. The C standard library has all these defined. Printf and Scanf are designed to take input till \0 occurs if %s format specifier is used!

When working with strings always use gets and puts instead of scanf and printf! Why else were they made for?!

There is no such thing as undefined behavior in C.

The language definition appears to disagree with you. Undefined behavior is stated as being behavior for which the standard imposes no requirements. The compiler writer is free to choose any result, reasonable or unreasonable, or even not choose a result and simply let whatever falls out of the compiler's design happen. So yes, there's most certainly a such a thing as undefined behavior in C.

It's programmers fault.

Yes, it's the programmer's fault for invoking undefined behavior.

You can sue the compiler maker for such problems!

Not successfully. As an analogy, you can't successfully sue the police for punishing you in a clear case of violating the law. Compiler vendors are not responsible for code compiled under their software that violates the rules of the language. Now if the code was compliant with the language standard and failed catastrophically, the compiler vendor could be sued because that falls under their contractual obligations.

Input two words and see that it prints only 1. This is because scanf was designed to take till \0 is encountered.

Incorrect. The %s specifier is defined as being whitespace delimited, so it reads until encountering any whitespace character. The two words are separated by whitespace in your example, and that's why only the first is converted by scanf().

The C standard library has all these defined.

Indeed. Maybe you should take some time to read the standard since you're clearly not familiar enough with it. :icon_rolleyes:

When working with strings always use gets and puts instead of scanf and printf!

Never use gets(), it's impossible to use safely. puts() is fine, but it appends a newline to the stream which might not be desirable. If you're just dumping a string rather than formatting it, fputs() does not include a newline and is a decent alternative to puts(). On the other hand, if you do need to format the string first, the printf() family is by far the simplest way to display your output in one step.

Maybe you should take some time to read the standard since you're clearly not familiar enough with it.

Thats correct, I'm not familiar with the standards. I'm just 19 and still have lot to learn. But, I know that I won't be reading the standards... it's just mumbo jumbo to bring perfection and absolution in computing. To me, a standard is like a rule that some people follow to call themselves "good programmers". My only idea is that every problem is to be solved. Perfection is secondary!

And honestly, the explanation I gave works on major of my programs. If I ever get a problem then I'll revise my ideas. Google isn't going away for a looooong time! :D

But, I know that I won't be reading the standards... it's just mumbo jumbo to bring perfection and absolution in computing.

:|

And honestly, the explanation I gave works on major of my programs.

Oh joy, yet another ignorant amateur touting the tired old argument of "it works for me, so I'm right".

If I ever get a problem then I'll revise my ideas.

Well it's not just your problem, because you're trying to teach others. So when some tard's undefined behavior causes the plane you're riding to plow into a mountain because he listened to your advice, be sure to revise your ideas quickly. :icon_rolleyes:

fine mister highly professional. gonna wiki you soon to see if your name comes up. After all you could judge kanetkar so easily then you must be someone like steve wozniak. hmm

gonna wiki you soon to see if your name comes up. After all you could judge kanetkar so easily then you must be someone like steve wozniak.

Unless someone wrote an article about me (I doubt it), you won't find anything on Wikipedia. However, you're welcome to visit my website or continue reading my posts on Daniweb and CBoard (under the user name Prelude) and judge for yourself if I know what I'm talking about. Be sure to wiki Kanetkar too, as pages dedicated to him are rather critical of his work. Fifteen years ago I'd be less critical, but since he's completely failed to update his books to keep with the times, I see no reason to be tolerant.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.