Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

The scanf method.

c scanf

  • Please log in to reply
3 replies to this topic

#1 gibsray

gibsray

    CC Newcomer

  • Member
  • PipPip
  • 10 posts
  • Programming Language:C, C#, Assembly
  • Learning:C, C#, Assembly

Posted 01 November 2012 - 07:05 AM

Hi
I have an exercise which I'm not sure how to do.
I need to build a program in which the user enters commands and the program will execute what he commands. for example there's a "print" command and the user enters (when the program runs):
print Hello world
the output is:
Hello world
so I wrote this:
char *command;
char *printme;
scanf("%s",command);
if(cmpstr(command, "print")==true)
{
scanf("%s",printme);
while(*printme!=EOF)
{
printf("/n %s", printme);
scanf("%s",printme);
}
}
will it work? and does scanf stops when space is encountered?
thanks
  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 01 November 2012 - 07:32 AM

You haven't allocated space for your character arrays. It is very unlikely to work reliably.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 01 November 2012 - 10:03 AM

In C, a char* is merely a pointer to some space in memory, but it doesn't automatically allocate that space in memory. You need malloc() to actually provision some of your computer's memory for your program to use.

Example:
char *command = (char*) malloc(20 * sizeof(char)); // Allocates enough memory in the heap for 20 characters.

Don't forget to release that memory when you're done with it when it is allocated in this way:
free(command); // Releases the memory pointed to by 'command'

Of course, there's no reason in your program why you must allocate memory from the heap. A static allocation on the stack will work just as well, and you won't need to concern yourself with freeing it afterwards:
char command[20];

EDIT:

and does scanf stops when space is encountered?

Yes, according to the scanf() documentation (http://www.cplusplus...y/cstdio/scanf/)

s String of characters Any number of non-whitespace characters, stopping at the first whitespace character found. A terminating null character is automatically added at the end of the stored sequence.


  • 2

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#4 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 01 November 2012 - 08:54 PM

Greg explained it very clearly to you.

Still, my advice for you is to take a look to Codecall's scanf tutorial , you might find interesting reading and practice there.

If you endeavors in searching you will find some other scanf/printf related posts too :D
  • 1





Also tagged with one or more of these keywords: c, scanf

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download