48525

Input/Output scanset in c

Question:

#include<stdio.h> int main() { char str[50]={'\0'}; scanf("%[A-Z]s",str); printf("%s",str); return 0; }

1) Input:

<blockquote>

helloWORLD

</blockquote>

output:

<blockquote>

</blockquote>

2) Input:

<blockquote>

HELLoworlD

</blockquote>

output:

<blockquote>

HELL

</blockquote>

In output 1, i expected the output as "WORLD" but it didnt give any outout. From output 2, i understood that this is working only if the first few characters are in upper case.

Can you please explain how it actually works?

Answer1:

When you do

scanf("%[A-Z]s",str);

It takes input as long as you enter upper-case letters. And since you set all the array to '\0', printf() will stop printing when it meets one.

Therefore, the first input is blank, and the second is printing until the end of the upper-case string.

Answer2:

<h3>Interpretation of scansets</h3>

When it is given helloWORLD, the conversion specification %[A-Z] fails immediately because the h is not an upper-case letter. Therefore, scanf() returns 0, indicating that it did not successfully convert anything. If you tested the return value, you'd know that.

When it is given HELLoworlD, the scanset matches the HELL and stops at the first o. The format string also attempts to match a literal s, but there's no way for scanf() to report that it fails to match that after matching HELL.

<hr /><h3>Buffer overflow</h3>

Note that %[A-Z] is in general dangerous (as is %s) because there is no constraint on the number of characters read. If you have:

char str[50];

then you should use:

if (scanf("%49[A-Z]", str) != 1) ...some problem in the scan...

Also note that there is a 'difference by one' between the declared length of str and the number in the format string. This is awkward; there's no way to provide that number as an argument to scanf() separate from the format string (unlike printf()), so you may end up creating the format string on the fly:

int scan_upper(char *buffer, size_t buflen) { char format[16]; if (buflen < 2) return EOF; // Or other error indication snprintf(format, sizeof(format), "%%%zu[A-Z]", buflen-1); // Check this too!? return scanf(format, buffer); }

Recommend

  • Pandas multi-index subtract from value based on value in other column
  • creating password field in oracle
  • Ruby regex to remove all consecutive letters from string
  • Add spaces between words in spaceless string
  • QPushButton is not changing the background-color proper
  • CKeditor stripping font tags instead of converting to span
  • Layout design help Android
  • What does the TypeScript “lib” option really do?
  • calculating number of bytes of each row in an image
  • @tailrec why does this method not compile with 'contains a recursive call not in tail position&
  • (Tcl/Expect) clear screen after exit
  • Scanner nextInt() and hasNextInt() problems
  • jQuery: How to AJAXify WordPress Search?
  • SQL Query - Table Joining Problems
  • Azure webjobs output logs indexing taking very long
  • Trying to get the char code of ENTER key
  • Query to find the duplicates between the name and number in table
  • Zurb Foundation _global.scss meta styles for js?
  • ADO and msqli connections very slow
  • Marklogic : Query response time is very high
  • Content-Length header not returned from Pylons response
  • Play WS (2.2.1): post/put large request
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • Avoid links criss cross / overlap in d3.js using force layout
  • How to access EntityManager inside Entity class in EJB3
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • TFS: Get latest causes slow project reloading
  • vba code to select only visible cells in specific column except heading
  • what is the difference between the asp.net mvc application and asp.net web application
  • How to make Safari send if-modified-since header?
  • Akka Routing: Reply's send to router ends up as dead letters
  • How to pass list parameters for each object using Spring MVC?
  • Matrix multiplication with MKL
  • How to set the response of a form post action to a iframe source?
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • JaxB to read class hierarchy
  • reshape alternating columns in less time and using less memory
  • Binding checkboxes to object values in AngularJs