C Program to Find Total of Even Integers

C printf, scanf, fprintf and sprintf functions

printf and scanf are two standard C programming language functions for input and output. Both are functions in the studio library which means #include <studio. h> is required at the top of your file.

The standard library provides functions related to printf() and scanf() that can be used for dealing with files and strings. The use of these functions is explained. General file input/output is important in applications where data reside in files on disks and tapes. We will show how to open files for processing and how to use pointer into file. Some applications need temporary files.

printf and scanf Functions with Example,

printf Function

The printf() function has two nice properties that allow flexible use at a high level First, a list of arguments of arbitrary length can be printed, and second, the printing is controlled by simple conversion specifications, or formats.

printf function arguments:

int printf(const char * format,..)

printf like scanf, also requires format placeholders

Example,

printf("hello");

Example,

int number = 9;
printf("%d", number);

In this example we have declared an integer called number, which takes the value 9. The ” %d” in printf allows the function to recognize the variable number as being of integer data type.

Our output would simply be 9.

scanf Function

The function scanf() has generally two properties that allow flexible use at high level.The first is that list of arguments of arbitrary length can be scanned and the second is that this input is controlled by simple conversion specifications.


scanf Function arguments:

int scanf(const char * format, [varl], [var2],...)

scanf requires format placeholders within the format string to denote what data type we would like the input to be read as by the computer.

Here are a table of some of the most commonly used placeholders:

Example,

int number;
scanf ( "%d" , &number ) ;

In this example we have declared an integer called number. The ” %d” in scanf allows the function to recognise user input as being of an integer data type, which matches the data type of our variable number. The ampersand (&) allows us to pass the address of variable number which is the place in memory where we store the information that scanf read.

Example,

char word [ 256 ];
scanf ( "%s" , word) ;

In this example we have declared a string called word.

The ” %s “ in scanf allows the function to recognise user input as being of string data type, which matches the data type of our variable word. since we’re using a string here we require the header file <string . h>, but unlike the example of the integer before, we do not require the ampersand (&) since string is an array of characters.

An array is already a pointer to a character data type in memory as we have already learned before. Using the ampersand, the above example would be equivalent to:

for(int i=0;i<256;11=1){ scanf("%s", &word[i]);

In a past assignment we were required to read strings one at a time without the \n so that we could print them all on one line. Square brackets in scanf allow us to define which characters to read from input.

sprintf Function

Example,

int n = sprintf(storageString,"string"); printf("%s",storage);

This function is used along with strings. There are at least 2 parameters. The first parameter is where the string will be stored; the second parameter is the formatted string that needs to be printed. The formatted string that is defined in the same way that printf is with its respective placeholders. The possible multi-parameters that are needed in the function are defined the same way as printf. On success, this function returns an integer indicating how long the formatted string is. It is also followed by using printf to print the string to stdout.

fprintf Function

Example,

int n = fprintf(storageFile,"string");

This function is used along with file reading and writing. Similar to sprintf, it takes in at least 2 parameters and returns an integer indicating how long the formatted string is. One of the big differences between sprintf and fprintf is that the first parameter is not a string where the formatted string will be stored, but a file where the formatted string will be printed. Instead of printing to stdout, the formatted string will appear in the file, similar to the >out . txt option in shell command line.

Online Training Tutorials

  • C Program to Find Total of Even IntegersC While Loop with ExampleThe simplest of three loops in C Language is the C while loop. In common language while has fairly obvious meaning: the while-loop has a condition: while […]
  • C Program to Find Total of Even IntegersC Variable With ExamplesC variable is a sequence of program code with a name (also called its identifier). A name or identifier in C can be anything from a single letter to word. The name of variable must begin […]
  • SAP AuthorizationSAP Security and AuthorizationsAn authorization is a permission to perform a certain action in the SAP System. Authorizations are used to control access at the application level..SAP Authorization concept is basically […]
  • sap advantagesSAP Advantages and Disadvantages ExplainSAP is the world’s leading provider of business software – enterprise resource planning, business intelligence, and related applications and services that help companies of all sizes and […]
  • credit managementHow to Configure Credit Management in SAP?All business have their own credit management needs, SAP allows you to specify your own automatic credit checks based on a variety of criteria. You can also specify at which critical […]
  • SAP Installation GuideSAP Installation Guide Step by Step for Begineerswe here with share the sap installation step by step procedure for easy downloadable and installation of SAP. SAP Installation Step by Step Guide Download the Installation Guide […]
  • how-to-configure-printer-in-sap-system-by-using-spadHow to Configure Printer in SAP System by using SPAD?Printer in SAP System : We can configure a printer in sap in 2 different manners. Either you use LOCD (device type:SNI9014 for Dot-Matrix) or LOCL (device type:SAPWIN for HP or others) […]
  • Interest calculationInterest Calculation Process in SAPFirst the program identifies the items on which Interest calculation is to be calculated according to the rules defined in the interest indicator, and any additional specifications you […]