V tomto kurzu se naučíte používat funkci scanf () k převzetí vstupu od uživatele a funkci printf () k zobrazení výstupu uživateli.
C Výstup
V programování C printf()
je jednou z hlavních výstupních funkcí. Funkce odesílá formátovaný výstup na obrazovku. Například,
Příklad 1: C výstup
#include int main() ( // Displays the string inside quotations printf("C Programming"); return 0; )
Výstup
C Programování
Jak tento program funguje?
main()
Funkci musí obsahovat všechny platné programy C. Provádění kódu začíná od začátkumain()
funkce.- Funkce
printf()
je knihovna pro odesílání formátovaného výstupu na obrazovku. Funkce vytiskne řetězec uvnitř uvozovek. - Chcete-li použít
printf()
v našem programu, musímestdio.h
pomocí#include
příkazu zahrnout záhlaví . - Příkaz
return 0;
uvnitřmain()
funkce je „Stav ukončení“ programu. Je to volitelné.
Příklad 2: Výstup celého čísla
#include int main() ( int testInteger = 5; printf("Number = %d", testInteger); return 0; )
Výstup
Počet = 5
Pro %d
tisk int
typů používáme specifikátor formátu . Zde budou %d
vnitřní nabídky nahrazeny hodnotou testInteger.
Příklad 3: float a dvojitý výstup
#include int main() ( float number1 = 13.5; double number2 = 12.4; printf("number1 = %f", number1); printf("number2 = %lf", number2); return 0; )
Výstup
number1 = 13.500000 number2 = 12.400000
K tisku float
používáme %f
specifikátor formátu. Podobně používáme %lf
k tisku double
hodnot.
Příklad 4: Tisk znaků
#include int main() ( char chr = 'a'; printf("character = %c", chr); return 0; )
Výstup
znak = a
K tisku char
používáme %c
specifikátor formátu.
C Vstup
V programování C scanf()
je jednou z běžně používaných funkcí, která přijímá vstup od uživatele. scanf()
Funkce čte formátovaný vstup ze standardního vstupu, jako je klávesnice.
Příklad 5: Celočíselný vstup / výstup
#include int main() ( int testInteger; printf("Enter an integer: "); scanf("%d", &testInteger); printf("Number = %d",testInteger); return 0; )
Výstup
Zadejte celé číslo: 4 Číslo = 4
Zde jsme použili %d
specifikátor formátu uvnitř scanf()
funkce k převzetí int
vstupu od uživatele. Když uživatel zadá celé číslo, uloží se do proměnné testInteger.
Všimněte si, že jsme použili &testInteger
uvnitř scanf()
. Je to proto, že & testInteger získá adresu testInteger a hodnota zadaná uživatelem je uložena na této adrese.
Příklad 6: Plovoucí a dvojitý vstup / výstup
#include int main() ( float num1; double num2; printf("Enter a number: "); scanf("%f", &num1); printf("Enter another number: "); scanf("%lf", &num2); printf("num1 = %f", num1); printf("num2 = %lf", num2); return 0; )
Výstup
Enter a number: 12.523 Enter another number: 10.2 num1 = 12.523000 num2 = 10.200000
We use %f
and %lf
format specifier for float
and double
respectively.
Example 7: C Character I/O
#include int main() ( char chr; printf("Enter a character: "); scanf("%c",&chr); printf("You entered %c.", chr); return 0; )
Output
Enter a character: g You entered g
When a character is entered by the user in the above program, the character itself is not stored. Instead, an integer value (ASCII value) is stored.
And when we display that value using %c
text format, the entered character is displayed. If we use %d
to display the character, it's ASCII value is printed.
Example 8: ASCII Value
#include int main() ( char chr; printf("Enter a character: "); scanf("%c", &chr); // When %c is used, a character is displayed printf("You entered %c.",chr); // When %d is used, ASCII value is displayed printf("ASCII value is %d.", chr); return 0; )
Output
Enter a character: g You entered g. ASCII value is 103.
I/O Multiple Values
Here's how you can take multiple inputs from the user and display them.
#include int main() ( int a; float b; printf("Enter integer and then a float: "); // Taking multiple inputs scanf("%d%f", &a, &b); printf("You entered %d and %f", a, b); return 0; )
Output
Enter integer and then a float: -3 3.4 You entered -3 and 3.400000
Specifikátory formátu pro I / O
Jak vidíte z výše uvedených příkladů, používáme
%d
proint
%f
profloat
%lf
prodouble
%c
prochar
Tady je seznam běžně používaných datových typů C a jejich specifikátory formátu.
Datový typ | Specifikátor formátu |
---|---|
int | %d |
char | %c |
float | %f |
double | %lf |
short int | %hd |
unsigned int | %u |
long int | %li |
long long int | %lli |
unsigned long int | %lu |
unsigned long long int | %llu |
signed char | %c |
unsigned char | %c |
long double | %Lf |