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 […]
  • Top 100 Python Interview Questions and AnswersTop 100 Python Interview Questions and AnswersWe have listed Top 100 Python interview questions that have been designed for Python programmers who are preparing interviews on Python interviews. Python is regarded as being a great […]
  • 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 […]
  • Haskell Interview Questions and AnswersHaskell Interview Questions and AnswersWe have listed of Haskell Interview Questions that have been designed for Haskell professionals who are preparing interviews on Haskell. Haskell is a deep language; we think learning it is […]
  • 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 […]
  • ERP beginning and its role in business an OverviewIn this article I thought I’ll discuss a bit about an interesting topic that is common to all ERPs.I am certain most of us are aware of what an ERP is and its role in businesses, but have […]
  • General Ledger AccountingWhat Is New General Ledger Accounting in SAP?General Ledger Accounting in SAP. The central task of G/L accounting is to provide a comprehensive picture of external accounting and accounts. Recording all business transactions (primary […]