Saturday, 20 January 2018

Commonly Used scanf() Formats | c language tutorial for beginners | basic c programs for beginners


Commonly Used scanf() Formats


Commonly Used scanf() Formats

code                Meaning

%d                  Read a decimal integer

%e                  Read a floating point value

%f                  Read a floating point value

%g                  Read a floating point value

%h                  Read a  short integer


%i                  Read a decimal,hexadecimal or
                         octal integer

%o                  Read an octal integer

%s                  Read a string

%u                  Read an unsigned decimal integer

%c                  Read a single character 
     
%x                  Read a hexadecimal integer 
 
%[....]             Read a string of word[s]


 
Points to remember while using scanf:
     If we do not plan carefully,some crazy things canhappen with scanf.Since the i/o routines are not a part of c language,they are made available either as a separate module of the c library or as a part of the operating system(like unix).Givenbelow are some of the general points to keep in mind while writing a scanf statement.

1.All function arguments,except the control string,must   be pointers to variables.
2.Format specifications contained in the control string should match the arguments in order.
3.Input data items must be separated by spaces and must   match the variables receiving the input in the same  order.
4.The reading will be terminated,when scanf encounter an   invalid mismatch of data or a character that is not valid for the value being  read.
5.When searching for a value,scanf ignores line boundaries and simply looks for the next appropriate character.
6.Any unread data items in a line will be considered as a   part of the data input line to the next scanf call.
7.When the field width specifier w is used,it should be  large enough to contain the input data size.

Formatted Output: The printf statement provides certain features that can be effectively exploited to control the alignment and pacing of print-outs n the terminals. The general form of printf statement is
    printf("control string",arg1,arg2,.........argn); control string consists of three of items:
1.Characters that will be printed on the screen as they   appear.
2.Format specifications that defines the output format   for display of each item.
3.Escape sequence characters such as \n,\t and \b.   The control string indicates how many arguments follow and what their types are. The arguments arg1,arg2,........ argn are the variables whose values are formatted and printed according to specifications of the control string.The argument should match in number,order and type with the format specifications. A  simple format specification has the following form: %w.p type-specifier.  Where "w"is an integer number that specifies the total
number of columns for the output value and p is another integer number that specifies the number of digits to the right of the decimal point or the number of characters to be printed from a string.
Both w and p are optional. Some examples of printf statement are:

basic c programs for beginners

programming using c language
printf("programming in c");
printf(" ");
printf("\n");
printf("%d",x);
printf("a=%f\n b=%f",a,b);
printf("sum=%d",1234);
printf("\n\n");
printf never supplies a new line automatically and therefore
multiple printf statements may be used to build one line of
output.

Related Posts:

  • STANDARD LIBRARY FUNCTIONS STANDARD LIBRARY FUNCTIONS Arithmetic Functions: abs --> Returns the absolute value of an integer. cos --> Calculates cosine. cosh --> Calculates hyperbolic cosine. exp --> Raises the exponential e to the xt… Read More
  • Function Declaration and prototype Function Declaration and prototype It is one of the most important feature of ANSI C.A function prototype tells the compiler the type of data returned by the function, the number of arguments the function expects to r… Read More
  • Introduction To ARRAYS Introduction To ARRAYS  An array is a group of related data items that share a common name.For instance,  we can define an array name Salary to represent a set of salaries of a group of employees. A particular … Read More
  • Parameter Passing Technique Parameter Passing Technique The parameter passing mechanism refers to the actions taken regarding the parameters when a function is invoked. There are two parameter passing techniques available in C. They are: 1) Call B… Read More
  • Introduction To Functions Introduction To Functions Functions are building blocks of C. The general form of C (User-defined) function is:       Return-type-function-name(parameter list)       parameter declaration &… Read More

0 comments:

Post a Comment