C104: Input and output

For the discussion, three variables are declared each of the data type integer, float and character respectively.

int varA = 5;
float varB = 3.31;
char varC = 'B';

to printf() function sends output to the screen. To simply display a string on the screen, enclose the string in a double quotes and pass it to printf() as its parameter.

printf("Learning output and input in C programming");

output displayed in the console is:

Learning output and input in C programming

also can display a variable value. to do this a format string is provided to the printf() function. When compiler sees this a matching value sent as the second parameter is displayed.

printf("%d", varA);

the output for this is:

5

this function also allows us to include the variable values together with a text. Compiler simply replace the value of variable provided is the location of (%d, %f or %c).

printf("Integer value is %d\n", varA);
printf("Value %f has decimalpoint\n", varB);
printf("Character value is %c\n", varC);

the output for this is:

Integer value is 5
Value 3.31 has decimal point
Character value is B

to display value of more than one variable in single printf() function

printf("Integer %d, Float %f, Character %c", varA, varB, varC);

The compiler will display value of the first variable (varA) into the first format string by keeping in mind its data type, value of second variable (varB) into second format string and value of third variable(varC) into thirt format string (%c).

Output for this is:

Integer 5, Float 3.31, Character B

for the inputs scanf() function is used to read a formatted data from keyboard.

to read an integer value, %d format is used.

scanf("%d", &varA);

the & sign infront of the varA means it is the address of the varA variable. The value obtained is stored in this address location.

to read an floating point value, %f format is used.

scanf("%f", &varB);

to read a character value, %c format is used.

scanf("%c", &varC);

interactive

#include<stdio.h>

int main()
  int varA;
  float varB;
  char varC;

  printf("Enter a integer number:");
  scanf("%d", &varA);
  printf("Enter a floating point number:");
  scanf("%f", &varB);
  printf("Enter a character:");
  scanf("%c", &varC);

  printf("Integer value entered is %d\n", varA);
  printf("Floating value entered is %d\n", varB);
  printf("Character value entered is %d\n", varC);

  return 0;
}

One possible output for this program is:

Enter a integer number: 43
Enter a floating point number: 3.232
Enter a character: H
Integer value entered is 43
Floating value entered is 3.232
Character value entered is H


The stdio is one of the important header file included in most of the C programs because it contains printf() and scanf() functions. Both these functions are important to get an input into a program and to produce output from the program. These allows the program to interact with its user.


Comments

Popular posts from this blog

Drawing Simple Pie Chart

VB.net connecting to SQL Server

VB.net