View Single Post
  #1 (permalink)  
Old 06-10-2007, 12:45 AM
twitch twitch is offline
Newbie
 
Join Date: Jun 2007
Posts: 11
Rep Power: 0
twitch is on a distinguished road
Default How to use scanf in C.

A lot of new C programmers have trouble with scanf(). So I'm going to show you how to do it

The main reason beginners have trouble is scanf works like a pointer so you have to point to what your getting input for

Code:
#include<stdio.h>

int main()
{
   int myvariable;

   printf("Enter a number:");
   scanf("%d",&myvariable);
   printf("%d",myvariable);
   
   return 0;
}

See when we used scanf we first declared what the variables type was
"%d" for int ,"%f" for float ,%e for double ,"%c" for char , "%s" for strings.

Then in the second part we have to use & like in a pointer to point to the variable instead of just getting its value Remember without & your program will likely crash

For handling strings with space and in files use fgets Here is a link to a tutorial on using it User Input: Strings and Numbers [C]

Last edited by twitch; 06-10-2007 at 05:53 PM.
Reply With Quote

Sponsored Links