?
Solved

Where Did I Go Wrong?

Posted on 2008-10-10
7
Medium Priority
?
2,523 Views
Last Modified: 2013-12-14
The below is the warning I get when I try to debug inputs to my program:
warning C4996: 'scanf' was declared deprecated

It refers to this string:  scanf(" %c%c%c", &first, &second, &third);

I cannot figure out what is wrong.  Can anyone help?
0
Comment
Question by:Noo2this
  • 3
  • 3
7 Comments
 
LVL 8

Expert Comment

by:StrongBad_Rules
ID: 22692679
Could you put the whole code on here or at least the declarations?
0
 

Author Comment

by:Noo2this
ID: 22692689

int
main(void)
{
char first, second, third; /* input - first 3 letters of your name */
int yard_length, house_length; /* input - yard and house length */
int yard_width, house_width; /* input - yard and house width */
int grass_area; /* output - what you are working with */
int time_required; /* output - time required to cut the grass */
 
/* Get and display the customer's name. */
printf("Type in first 3 letters of your name and press return> ");
scanf(" %c%c%c", &first, &second, &third);
printf("Hello %c%c%c, let's see how big your yard is.\n", first, second, third);
	

Open in new window

0
 
LVL 8

Expert Comment

by:Thejaka
ID: 22692854
This is a microsoft CRT related warning. You can use

#pragma warning(disable:4996)

to disable the warning, then

#pragma warning(default:4996)

to reset it. Use before, after main method, respectively.

Alternately, try a later Microsoft CRT, or an alternate CRT.

Also, you can use scanf_s instead:
http://msdn.microsoft.com/en-us/library/w40768et(VS.80).aspx
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:Noo2this
ID: 22693717
Thejaka,
I used scanf_s and it gave me access violations.  I'm new to this visual studio so where exactly do I put the #pragma warning(disable:4996) in my program?
0
 
LVL 8

Expert Comment

by:Thejaka
ID: 22693953
The access violations may have been caused by your compile/link options. Try dynamically linking to CRT by selecting "Multithreaded DLL" build option and linking with msvcp, msvcrt libraries.

For the other, try like this:

#pragma warning(disable:4996)
int main(void)
{
  // Some code...
}
#pragma warning(default:4996)

Open in new window

0
 
LVL 8

Accepted Solution

by:
Thejaka earned 120 total points
ID: 22693974
Regarding the access violation:

In case you didn't check the msdn link, note the following excerpt:

==========

Unlike scanf and wscanf, scanf_s and wscanf_s require the buffer size to be specified for all input parameters of type c, C, s, S, or [. The buffer size is passed as an additional parameter immediately following the pointer to the buffer or variable. For example, if reading a string, the buffer size for that string is passed as follows:

char s[10];

scanf_s("%9s", s, 10);

The buffer size includes the terminating null. A width specification field may be used to ensure that the token read in will fit into the buffer. If no width specification field is used, and the token read is too big to fit in the buffer, nothing will be written to that buffer.

==========

Your line of code could be written as follows:

scanf_s(" %c%c%c", &first, 1, &second, 1, &third, 1);

Open in new window

0
 

Author Closing Comment

by:Noo2this
ID: 31505250
Thejaka, thanks.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Steps to fix error: “Couldn’t mount the database that you specified. Specified database: HU-DB; Error code: An Active Manager operation fail”
Mailbox Corruption is a nightmare every Exchange DBA wishes he never has. Recovering from it can be super-hectic if not entirely futile. And though techniques like the New-MailboxRepairRequest cmdlet have been designed to help with fixing minor corr…
An overview on how to enroll an hourly employee into the employee database and how to give them access into the clock in terminal.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question