Input and Output in C
Input :
C supports a number of ways for taking user input.
getchar() Returns the value of the next single character input.
For example:
printf("You entered: %c", a);
return 0;
}
The input is stored in the variable a.
The gets() function is used to read input as an ordered sequence of characters, also called a string.
A string is stored in a char array.
For example :
Here we stored the input in an array of 100 characters.
scanf() scans input that matches format specifiers.
For example:
printf("Enter two numbers:");
scanf("%d %d", &a, &b);
printf("\nSum: %d", a+b);
return 0;
}
For example :
The & sign before the variable name is the address operator. It gives the address, or location in memory, of a variable. This is needed because scanf places an input value at a variable address
As another example, let's prompt for two integer inputs and output their sum:
C supports a number of ways for taking user input.
getchar() Returns the value of the next single character input.
For example:
#include <stdio.h>
int main()
int main()
{
char a = getchar();
char a = getchar();
printf("You entered: %c", a);
return 0;
}
The gets() function is used to read input as an ordered sequence of characters, also called a string.
A string is stored in a char array.
For example :
#include <stdio.h>
int main()
int main()
{
char a[100];
gets(a);
printf("You entered: %s", a);
return 0;
}
char a[100];
gets(a);
printf("You entered: %s", a);
return 0;
}
Here we stored the input in an array of 100 characters.
scanf() scans input that matches format specifiers.
For example:
#include <stdio.h>
int main()
{
int a;
printf("Please enter an element : ", a);
scanf("%d", &a);
return 0;
}
The "&" sign before the variable name is the address operator. It gives the address, or location in memory, of a variable. This is needed because scanf places an input value at a variable address
As another example, let's prompt for two integer inputs and output their sum:
As another example, let's prompt for two integer inputs and output their sum:
#include <stdio.h>
int main()
int main()
{
int a, b;
int a, b;
printf("Enter two numbers:");
scanf("%d %d", &a, &b);
printf("\nSum: %d", a+b);
return 0;
}
Output
:
We have already used the printf() function to generate output in the previous lessons. In this lesson, we cover several other functions that can be used for output.
putchar() Outputs a single character.
We have already used the printf() function to generate output in the previous lessons. In this lesson, we cover several other functions that can be used for output.
putchar() Outputs a single character.
For example :
#include <stdio.h>
int main()
int main()
{
char a = getchar();
printf("You entered: ");
putchar(a);
return 0;
}
char a = getchar();
printf("You entered: ");
putchar(a);
return 0;
}
The input is stored in the variable a.
The puts() function is used to display output as a string.
A string is stored in a char array.
For example :
#include <stdio.h>
int main()
{
char a[100];
gets(a);
printf("You entered: ");
puts(a);
return 0;
}
Here we stored the input in an array of 100 characters.
scanf() scans input that matches format specifiers.
For example :
#include <stdio.h>
int main()
{
int a;
printf("Please enter an element : ", a);
scanf("%d", &a);
return 0;
}
The & sign before the variable name is the address operator. It gives the address, or location in memory, of a variable. This is needed because scanf places an input value at a variable address
As another example, let's prompt for two integer inputs and output their sum:
#include <stdio.h>
int main()
{
int a, b;
printf("Enter two numbers:");
scanf("%d %d", &a, &b);
printf("\nSum: %d", a+b);
return 0;
}
Note : scanf() stops reading as soon as it encounters a space, so text such as "Hello World" is two separate inputs for scanf().
Note : scanf() stops reading as soon as it encounters a space, so text such as "Hello World" is two separate inputs for scanf().
Formatted Input
:
The scanf() function is used to assign input to variables. A call to this function scans input according to format specifiers that convert input as necessary.
If input can't be converted, then the assignment isn't made.
The scanf() statement waits for input and then makes assignments:
The scanf() function is used to assign input to variables. A call to this function scans input according to format specifiers that convert input as necessary.
If input can't be converted, then the assignment isn't made.
The scanf() statement waits for input and then makes assignments:
int x;
float num;
char text[20];
scanf("%d %f %s", &x, &num, text);
float num;
char text[20];
scanf("%d %f %s", &x, &num, text);
Typing 10 22.5 abcd and then pressing Enter assigns 10 to x, 22.5 to num, and abcd to text.
Note that the & must be used to access the variable addresses. The & isn't needed for a string because a string name acts as a pointer.
Format specifiers begin with a percent sign % and are used to assign values to corresponding arguments after the control string. Blanks, tabs, and newlines are ignored.
A format specifier can include several options along with a conversion character:
%[*][max_field]conversion character
The optional * will skip the input field.
The optional max_width gives the maximum number of characters to assign to an input field.
The conversion character converts the argument, if necessary, to the indicated type:
d=decimal
c=character
s=string
f=float
x=hexadecimal
For example:
int x, y;
char text[20];
scanf("%2d %d %*f %5s", &x, &y, text);
/* input: 1234 5.7 elephant */
printf("%d %d %s", x, y, text);
/* output: 12 34 eleph */
Formatting Output
:
The printf() function was introduced in your very first Hello World program. A call to this function requires a format string which can include escape sequences for outputting special characters and format specifiers that are replaced by values.
For example :
printf("The tree has %d apples.\n", 22);
/* The tree has 22 apples. */
printf("\"Hello World!\"\n");
/* "Hello World!" */
Escape sequences begin with a backslash \:
\n=new line
\t=horizontal tab
\\=backslash
\b=backspace
\'=single quote
\"=double quote
Format specifiers begin with a percent sign % and are replaced by corresponding arguments after the format string. A format specifier can include several options along with a conversion character:
%[-][width].[precision]conversion character
The optional - specifies left alignment of the data in the string.
The optional width gives the minimum number of characters for the data.
The period . separates the width from the precision.
The optional precision gives the number of decimal places for numeric data. If s is used as the conversion character, then precision determines the number of characters to print.
The conversion character converts the argument, if necessary, to the indicated type:
d=decimal
c=character
s=string
f=float
e=scientific notation
x=hexadecimal
For example :
printf("Color: %s, Number: %d, float: %5.2f \n", "red", 42, 3.14159);
/* Color: red, Number: 42, float: 3.14 */
printf("Pi = %3.2f", 3.14159);
/* Pi = 3.14 */
printf("Pi = %8.5f", 3.14159);
/* Pi = 3.14159 */
printf("Pi = %-8.5f", 3.14159);
/* Pi = 3.14159 */
printf("There are %d %s in the tree.", 22, "apples");
/* There are 22 apples in the tree. */
To print the % symbol, use %% in the format string.
Comments
Post a Comment