Go Back   Site Owners Forums - Webmaster Forums > Web Programming > Programming General

Notices


Reply
 
Thread Tools Rate Thread Display Modes
Old 05-10-2012, 12:18 PM   #1
leox123
Registered User
 
Join Date: May 2012
Posts: 4
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 implement scanf in C in this tutorial.

The main reason beginners have trouble is scanf works like a pointer so you have to point to what you are 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 a scientific notation (1e10) ,"%c" for char , "%s" for strings.

Then in the second part we have to use & just 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 whitespace (sentences) and in files use fgets. Here is one of our tutorials on using it:
Reading And Writing Files In C.

You can bookmark or print the manual page on scanf for later reference: Scanf() - C reference
__________________
Make Money Online by locking links - bee4.biz/?ref=17978
leox123 is offline   Reply With Quote

Old 05-11-2012, 05:11 AM   #2
chikkiarora
Registered User
 
Join Date: Nov 2011
Posts: 128
Nice post. Thanks for sharing.

Scanf Function is basically used to get the input value from the user.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
chikkiarora is offline   Reply With Quote
Old 05-14-2012, 03:18 AM   #3
madhuriarora
Registered User
 
Join Date: Dec 2011
Posts: 217
Scan f() mainly used in c to input value by the user.Print f() used to print the value of enter by the user.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
madhuriarora is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 10:57 PM.


Powered by vBulletin Copyright © 2020 vBulletin Solutions, Inc.