36

C String and String functions with examples: String is an array of characters. Learn how to use strings in C programming along with string functions.

  • Related Questions & Answers

In C programming language, scanf function is used to read character, string, numeric data from keyboard Consider below example program where user enters a character. This value is assigned to the variable “ch” and then displayed. Then, user enters a string and this value is assigned to the variable “str” and then displayed. The subtleties of the scanf format strings are many; it is a very complex function to use correctly. You should take time to read the standard sometime (available from ANSI for $35 in PDF—well worth the investment if you're going to be a serious C programmer), or at the very least your system's man page. And reread it, often.

  • Selected Reading
CProgrammingServer Side Programming

Function scanf()

The function scanf() is used to read formatted input from stdin in C language. It returns the whole number of characters written in it otherwise, returns a negative value.

Here is the syntax of scanf() in C language,

Here is an example of scanf() in C language,

Example

Output

Function fscanf()

The function fscanf() is used to read the formatted input from the given stream in C language. It returns zero, if unsuccessful. Otherwise, it returns The input string, if successful.

This could not be evaluated since we didn't have any applications that tried to do this, although the program indicated it was up and running. After setting up defaults, it monitors the user's system for programs that attempt to send data out. Codekit little snitch In terms of function, the program requires little interaction and runs in the background.

Here is the syntax of fscanf() in C language,

Here is an example of fscanf() in C language,

Example

Output

  • The C Standard Library
  • C Standard Library Resources
  • C Programming Resources
  • Selected Reading

Scanf string dev c tutorial

Description

The C library function int scanf(const char *format, ..) reads formatted input from stdin.

Scanf From String

Declaration

Following is the declaration for scanf() function.

Parameters

  • format − This is the C string that contains one or more of the following items −

    Whitespace character, Non-whitespace character and Format specifiers. A format specifier will be like [=%[*][width][modifiers]type=] as explained below −

Sr.No.Argument & Description
1

*

This is an optional starting asterisk indicates that the data is to be read from the stream but ignored, i.e. it is not stored in the corresponding argument.

2

width

This specifies the maximum number of characters to be read in the current reading operation.

3

modifiers

Specifies a size different from int (in the case of d, i and n), unsigned int (in the case of o, u and x) or float (in the case of e, f and g) for the data pointed by the corresponding additional argument: h : short int (for d, i and n), or unsigned short int (for o, u and x) l : long int (for d, i and n), or unsigned long int (for o, u and x), or double (for e, f and g) L : long double (for e, f and g)

4

type

A character specifying the type of data to be read and how it is expected to be read. See next table.

fscanf type specifiers

typeQualifying InputType of argument
cSingle character: Reads the next character. If a width different from 1 is specified, the function reads width characters and stores them in the successive locations of the array passed as argument. No null character is appended at the end.char *
dDecimal integer: Number optionally preceded with a + or - signint *
e, E, f, g, GFloating point: Decimal number containing a decimal point, optionally preceded by a + or - sign and optionally followed by the e or E character and a decimal number. Two examples of valid entries are -732.103 and 7.12e4float *
oOctal Integer:int *
sString of characters. This will read subsequent characters until a whitespace is found (whitespace characters are considered to be blank, newline and tab).char *
uUnsigned decimal integer.unsigned int *
x, XHexadecimal Integerint *
  • additional arguments − Depending on the format string, the function may expect a sequence of additional arguments, each containing one value to be inserted instead of each %-tag specified in the format parameter, if any. There should be the same number of these arguments as the number of %-tags that expect a value.

Return Value

On success, the function returns the number of items of the argument list successfully read. If a reading error happens or the end-of-file is reached while reading, the proper indicator is set (feof or ferror) and, if either happens before any data could be successfully read, EOF is returned.

Scanf String Dev C Download

Example

Dev c++ scanf string

The following example shows the usage of scanf() function.

Dev C++ 5.11

Let us compile and run the above program that will produce the following result in interactive mode −