Η ΓΛΩΣΣΑ C - ΜΑΘΗΜΑ 15 - ΣΥΝΑΡΤΗΣΕΙΣ ΕΙΣΟΔΟΥ (ΕΚΤΥΠΩΣΗ)
-
Upload
dimitris-psounis -
Category
Education
-
view
676 -
download
0
Transcript of Η ΓΛΩΣΣΑ C - ΜΑΘΗΜΑ 15 - ΣΥΝΑΡΤΗΣΕΙΣ ΕΙΣΟΔΟΥ (ΕΚΤΥΠΩΣΗ)
H ������ C
���� 15:
� �����/������: ����������� �������
����� �����
����� ���� ���������. ����
1. ����
2. � �� � ���� ������ ������ stdin
3. � �� � ���� ������ ������ stdout
4. � �� � ���� ������ ������ ����� stderr
�. ���� ����� ��� ������ stdin
1. ����
2. � ���� ���� getchar()
3. � ���� ���� getch()
4. � ���� ���� getche()
5. H ���� ���� ungetc()
6. � ���� ���� gets()
7. � ���� ���� fgets()
8. � ���� ���� scanf()
9. � ���� ���� fflush()
����������� ����������������������� �������� ��!"��
A. !�"�1. ����#�
#���������� ����������������������� �������� ��!"��
� $ � ����� #�� "����� ��� C, % ����� "�& ��'� (streams).
� � � ��� � ��� �� �#�(��� � ���#���&� �)� � � ��� �� ������ %�� �� ��
����)������� ���(�%� � �� �"(�� ��.
� *� ( &� � �� �� %�&� +���) " ��� ����)������ ��� ��"�:
� ,� )��#������"� ��� ������� (stdin) �)� �� �)� � ���-�+��� ���#����� �)� ��
)(#���(�%��.
� ,� )��#������"� ��� ������ (stdout) ��� �)� � %��.��� ���#����� ��� ����.
stdout
stdin
A. !�"�1. ����#�
$���������� ����������������������� �������� ��!"��
� /��� ��� �� C (��)�� � ��� �� �#�(��� � ���#���&� �� �)� � )����� �� ��� ���������
�)� �� )��%��� ��. �"�� �)� ��� 2 �����#"� ��"� )�� " ��� ���("0�� " ��� ������
)�(("� �#� ��"� )�� )����� �� ��� ��������� � ����/"���� �)� #�� )��� �((�� ���#��"�,
).
stdout
stdin
printer
DVD-ROM
HARD DISK
A. !�"�2. $ ��� )��#������"�� ������� stdin
����������� ����������������������� �������� ��!"��
� H ANSI C (�����"� )����)� � ��� -���#"� (������% �� )�� )�")�� �� " �� #��� ���%(&�������
�� C) �� +�� ��� �� )���#��& 5 ��"� �)�� ��� �� #��� �(�)� � �� C:
� 1� ��"� � * ����)�������� �� )�(������� �#������ ��� DOS #�� ��&������� )("��
)��& "���.
����� ��� ������
stdin ���#������"� � ����� �(#���(�%��
stdout ���#������"� ������ 1���
stderr ���#������"�� ����� 1���
stdprn* ���#������"��� �#��)&��� �#��)&��� (LPT1)
stdaux* ���#������"�� 2���� ������#� 3��� (COM1)
A. !�"�2. $ ��� )��#������"�� ������� stdin
%���������� ����������������������� �������� ��!"��
� /�(� � ��� �� �#�.������ �� stdin ��� �� «�&(���» �)� � ����"�� �� )(#���(�%�� � ��
)��%��� ��
� �. . $ ������� scanf )�� ����, ���-�+�� ������ ���#����� �)� �� stdin, ����
#���� +����� �)� ��� )���������� )�� " ��� �"��� ��� 1o �����. �� � ����� " �� ����%��
)������������ ���#����� ���� ���� )���"���� ��� stdin %�� �)���� ���-��� �)�
#�)��� ����(� ��� � ���� �� )��#������"�� �������.
� 3� ���� #�� �((�� ����������� )�� ��� ��� +����� �� )��#������"� � ����.
stdin
11223 xyzw 223
A. !�"�3. $ ��� )��#������"�� ������� stdout
&���������� ����������������������� �������� ��!"��
� ��� ���� � stdout � ��� � «�&(���» )�� ����"�� �� )��%��� �� � �� ���� ���
�)�(�%����.
� �. . $ ������� puts, ��)����� ��� stdout �� ��-�(������ )�� �" ���� &� ����� #��
� ��� ����(� �� ��)&���� �� ���#����� )�� -� �#����� ��� stdout ��� ����.
stdout
A. !�"�4. $ ��� )��#������"�� ������ (��'� stderr.
'���������� ����������������������� �������� ��!"��
� $ stderr � ��� �� ������ ��� )�� (������%� �)&� stdout, ��� �)� � %��.��� �����
(����� �#�"(��� ��� )��%������ ��.
� �� �%�(� )��%����� � ��� �� �� �� �������� �� (������%��� %�� (�%��� )�� ��� � ���
�)��� ��� )��%��������#� (�� (). . ����� � �"����� ���, �)��� � �(�#��,
���% ���� �)�#���&� �� #.().)
� ���#��"��� �� � )��������� �� ����� )�� �"(��� �� )��-�((��� ��� ����, �
�� �����#� ����� ���� �� ��� )��%������, C �� +�� ����� �� -����#� ���, %��
�� % ��� ��� &����� �� ������ )��%������, �)� �� ����� (�����.
stderr
B. ����������� !��� �������1. ����0
(���������� ����������������������� �������� ��!"��
� H C �� )��" �� � ����� �)� ����������� %�� �� ��� ��������� �� � ���� �)� ��
)(#���(�%��.
� ��� � ��� �� -���#� ���#������#� �&� ���������&� )�� )�")�� �� " ��� �)�0 ��:
� �� �� ���#����� )�� )(#���(�%��� "���� �� "��� )���&���� '�� �)���#����
#�� )%� ���� ��� stdin )��'���� Enter, � �� #��� ���#����� )�� )(#���(�%���
)%� ��� #������ �� ��� stdin. ,� ���#������#� ���� �� �� �������� �������
�)���#��� ���#���&�
� �� ��� )(#���(�%��� )��-�((���� ����� ���� #�� ��� ���� � � �. ,� ���#������#�
���� �� �� �������� )��-�(� ���#���&�
B. ����������� !��� �������1. ����0
�)���������� ����������������������� �������� ��!"��
� �� � ���� �� )�'� ���� ���� ����������� )�� �� �(������� ���� �)����� ���.������:
����� ����� !� "�������
���������
#� ��� $�
% �&���
#� ��� $�
�&�����
*+,-./0 ���1���� � !�2���3�������!"��
456 456 7,89:;.
*+,-. ���1���� � !�2���3�������!"��
<=> <=> -:?9:;.
*+,-.+ ���1����� !� 2���3�������!"��
<=> @A> -:?9:;.
B?*+,- A �C������D��2���3����
E E 7,89:;.
*+,7 ���1���� ���1��������� @A> @A> 7,89:;.
F*+,7 ���1���� ���1��������� @A> @A> 7,89:;.
7-/?F ���1���� ���1��������� 456 456 7,89:;.
B. ����������� !��� �������2. $ ������� getchar()
������������ ����������������������� �������� ��!"��
� ,� )�&����)� �� �������� getchar � ���:
� /�� " �� ������ �� -�-(����#:
� $ ������� ���-�+�� "��� ���#���� �)� �� stdin #�� �)����".�� ��� ASCII #&��#� ��.
� �������%� � ������� �)���#��� ���#���&�, ��� ���"��� �)� ��� ��
)(#���(�%����� Enter %�� �� #��� &����� �� ���#����� ��� stdin.
� ���-�((�� ���� ���#����� )�� )(#���(�%��� ��� ����.
int getchar()
stdio.h
B. ����������� !��� �������2. $ ������� getchar()
������������ ����������������������� �������� ��!"��
� ,� )���#��& )������%� ������#���� �� ������� getchar
� 1 ����� ����%�� � ��-�(������ #�� ��� ���" ��� )����� Enter.
� $ � ����� �)��#������ ��� stdin #�� )������ ��� )��%��� �(�� � ����� )������ Enter.
/* getchar.c: Epideiksi tis entolis getchar */
#include <stdio.h>
main(){
char c;
c=getchar();while(c!='x'){
printf("%c,",c);c=getchar();
}printf("%c.",c);
}
B. ����������� !��� �������3. $ ������� getch ()
�#���������� ����������������������� �������� ��!"��
� ,� )�&����)� �� �������� getch � ���:
� /�� " �� ������ �� -�-(����#:
� $ ������� ���-�+�� "��� ���#���� �)� �� stdin #�� �)����".�� ��� ASCII #&��#� ��.
� �������%� &� � ������� �)���#��� ���#���&�, ��� � �� )�� )���� "���
���#���� ����� �)��#������ ��� stdin.
� ��� )��-�((�� ���� ���#����� )�� )(#���(�%��� ��� ����!!
� ����� �! ,� �� � � #�.�( ��� conio.h ��� � ��� "��� �� ANSI C, ��� ��� � ��� �)��� ���
�� �)�� �� �� �)������)��� ���%(&������. ������ �� DEV-C++ �� ���&��'���, ���
)����� �� �� ����)������� #�����#�!
int getch ()
conio.h
B. ����������� !��� �������3. $ ������� getch ()
�$���������� ����������������������� �������� ��!"��
� ,� )���#��& )������%� ������#���� �� ������� getch
� 1 ����� )(#���(�%� � ��-�(������.
� $ � ����� ��4 �)��#������ ��� stdin #�� )(#���(�%� #��� ���#���� ���-�+����
�"�&� �)� �� getch()
/* getch.c: Epideiksi tis entolis getch */#include <stdio.h>#include <conio.h>
main(){
char c;
c=getch();while(c!='x'){
printf("%c,",c);c=getch();
}printf("%c.",c);
}
B. ����������� !��� �������4. $ ������� getche ()
������������ ����������������������� �������� ��!"��
� ,� )�&����)� �� �������� getche � ���:
� /�� " �� ������ �� -�-(����#:
� $ ������� (������%� � ��� ��� ���)� � �� getch() � �� ���.��� ��� )��-�((�� ���
���#���� )�� )(#���(�%��� ��� ����.
� ����%(&�� ��� #�� �#��("��� �� )��%��� getche.c )�� �� ��� �� (������% � �� #�� ��
���.��� �)� �� ����(� getch.
int getche ()
conio.h
B. ����������� !��� �������4. $ ������� getche ()
�%���������� ����������������������� �������� ��!"��
� ,� )���#��& )������%� ������#���� �� ������� getche
� 1 ����� )(#���(�%� � ��-�(������.
� $ � ����� ��4 �)��#������ ��� stdin #�� )(#���(�%� #��� ���#���� ���-�+����
�"�&� �)� �� getche(). 5��� )(#���(�%��� ��� ���#���� )��-�((���� ��� ����.
/* getche.c: Epideiksi tis entolis getch */#include <stdio.h>#include <conio.h>
main(){
char c;
c=getche();while(c!='x'){
printf("%c,",c);c=getche();
}printf("%c.",c);
}
B. ����������� !��� �������5. $ ������� ungetc()
�&���������� ����������������������� �������� ��!"��
� ,� )�&����)� �� �������� ungetc � ��� :
� /�� " �� ������ �� -�-(����#:
� $ ������� ungetc ������ �� (�0 ��� ���#���� ch �)� �� ��� fp
� 3� ���� �� �)���� ���, ��� )����� �� #���� ���� #�� ��"� ������� �)�
�� � � #�� %�’ ���� &� 2� ����� )����� �� �� ���� #�� ��� �� � ��.
� ��� �� �����"���� (�0 �)� �� stdin, �)(� %��.��� &� ������� ����� � ��� stdin.
� $ ������� )��#��#� �)�����)����� ��� ��� stdin ��� ���#���� ch )�� ���-���.
� ��� )������%� �� �)���� ���.������ -(")��� )&� ����)����� �� ungetc.
int ungetc(int ch, FILE *fp)
stdio.h
B. ����������� !��� �������5. $ ������� ungetc()
�'���������� ����������������������� �������� ��!"��
� ,� )���#��& )������%� ������#���� �� ������� ungetc
� �(#���(�%���� �� ��-�(������ )�� )���" �� ���(� ����� � .��� �� %��� m #�� �� �� ��
�) ���� " �� ��� stdin, (�%& ��� ��� �)�����)������� "��� "���� ���#���� ��� stdin.
/* ungetc.c: Epideiksi tis entolis ungetc */#include <stdio.h>
main(){
char c;c=getchar();while(c!='\n'){
printf("%c",c);c=getchar();if (c=='m'){
printf("%c",c);ungetc('!',stdin);
}}
}
B. ����������� !��� �������6. $ ������� gets()
�(���������� ����������������������� �������� ��!"��
� ,� )�&����)� �� �������� gets � ��� :
� /�� " �� ������ �� -�-(����#:
� � ��� �� �(������ �� ��%#�#��"� ������� �� )��%����� ���.
� $ ������� ���-�+�� � ��-�(������ �)� �� stdin " �� ��� ���#���� ‘\n’ ��� �)� �
����#������ � �� ‘\0’ #�� �� �)��#���� ��� ��-�(������ str )�� �" ���� &� ����� "�&
���.����.
� �� %�� #�)��� (�%� �)��� ��, �)����".���� ��� NULL (#���� �� #��), �((�'�
�)����".���� �� #�� ��� str.
� 1 ������"��� ���)�� ���� �� gets � ���:
� 5)�� buffer � ��� ��-�(������ () ��#�� ���#���&�) )�� " ��� �������� � �� �����#�
� �� �����#�.
char *gets(char *str)
stdio.h
gets(buffer);
B. ����������� !��� �������7. $ ������� fgets()
�)���������� ����������������������� �������� ��!"��
� ��(("� .��"� " ��� )��-(� � �� gets, ����� ��-�(������ )�� ����)����� ���
" �� ��#��� '�� %�� �� �)��#����� �(� �� ����"�� )�� ����%�� � �����. H fgets "� ����
�� (���� ���� �� )��-(�.
� ,� )�&����)� �� �������� fgets � ���:
� /�� " �� ������ ��� �� � � -�-(����#�:
� $ ������� ���-�+�� � ��-�(������ �)� �� ��� fp #�� �� �)��#���� ��� str " ��:
� � �� ���-�+�� ��� ���#���� ‘\n’ ��� �)� � ����#������ � ‘\0’
� � �� ���-���� n-1 ���#�����. ,��� )����"��� ��� ���#���� ‘\0’ ��� �"(�� #�� ����� +��.
� �� �)(� (�%�� fgets -�+�� ���� ����� ���#����� )�� �� ���-���� )��#��"��� �� &����
��� ��-�(������ )�� ��� ������� &� �����.
� ,� )������%� �� �)���� ���.������ ������#���� �� ��� �&� )���)��&
char *fgets(char *str, int n, FILE *fp)
stdio.h
B. ����������� !��� �������7. $ ������� fgets()
������������ ����������������������� �������� ��!"��
� ,� )���#��& )������%� ������#���� �� ������� fgets
� �(#���(�%���� �� ��-�(������ � �#�� �%�(����� ��� 10 #�� )������ Enter.
������� ��� �� ��)���.��� ��� )��%������.
/* fgets.c: Epideiksi tis entolis fgets */
#include <stdio.h>
#define N 10
main(){
char buffer[N];
while(1){
fgets(buffer,N,stdin);printf("Eisodos: %s\n",buffer);
}}
B. ����������� !��� �������8. $ ������� scanf()
������������ ����������������������� �������� ��!"��
� ,� ������� scanf �� " ��� �(������ �� ��� .��"�.
� ��%& ��� )(����� )�� " �� ��� ��, ����� �� ���� #�� ��� ��(���� �� ������#��"���
������
� ��� ��)�#� )������%� ���� �� scanf )�� " �� ������ ��� � ��� �� ����:
� 5)�� �� 1� ����� � ��� ��-�(������ ��.�)� �� #�� ��� �)���� �� ����
�� +��� ��� (�����������) �&� ���-(�'� )�� �� �� ���� ��� ��"� )�� �� ����%�� �
�����
� �����#� �"��� � ��� �� ����:
� ,� )����� �� " ��� ��� ��-�(������ ��.�)� ��
� ����� � ��� �� )����������"� )�� )����� �� " ��� �� ��-�(������ ��.�)� ��
� �) �� �� )�")�� �� �"���� ���:
� � ����� )��-�(� �&� ���#���&� ��� ���� ��� )(#���(�%���.
� ��' % ����� #�� ������� �)���#��� �&� ���#���&�
stdio.h
scanf("%x",&���������);
B. ����������� !��� �������8. $ ������� scanf()
�#���������� ����������������������� �������� ��!"��
� ��� ��� 9: «����%&%� ��� � ����/����� ����"�&�» � ��� ���� )����������"� ��
scanf.
� � ��� &����� ������ )�����"�& ��� ����#"� ������ )����������'� '��� �� #���� +���
��� ���� ���#����� )�� )����� �� ���-������ (�������� �� �)�%������ �� ���-���
#�)��&� ���#���&�).
� ���� � ���:
% ���� ���� '(��� �������$� ��� ������)�!
G;;;H ���1�������� I���! ���2���3��������GH�C ���I�"�3��C;�!���"��1����2���3������3�!��J GH��������� � �K J��;
GL;;;H M�C"�� ���� GH������! ���2���3������3�!��J GH�C ���I�"��3��C
B. ����������� !��� �������8. $ ������� scanf()
�$���������� ����������������������� �������� ��!"��
� ,� )���#��& )��%��� ������#���� �� ��� �&� �)�)("�� )����������'� )�� ����%��:
� �(#���(�%���� #�� �#��("��� �� )��%��� ����%����� �� .��� )�� )���" �� ����
���#����� )�� )���� ������ ���� )�����������.
/* scanf.c: Epideiksi tis entolis scanf */
#include <stdio.h>
main(){
char buffer[80];
printf("Eisagete simvoloseira: ");scanf("%[abcdef]",buffer);printf("Diavastike: %s\n",buffer);printf("Eisagete simvoloseira: ");scanf("%[^abcdef]",buffer);printf("Diavastike: %s",buffer);
}
B. ����������� !��� �������9. $ ������� fflush()
������������ ����������������������� �������� ��!"��
� 5��� �#��(� ��� � #(�� ��� scanf (�-������ ��� � ����� �)� �� stdin �� � ���
�)��� �� %�� �� ���-���� � ��)�� �&� ����"�&� )�� �� ���.
� ���� %�� )������%� �� " ��� �� ����(�:
� /�� � ����� ����%��:
� ,��� ���-�+���� �� 15 #�� ���� ����� �� x, �%��� ��� �� #��� #�� ���-�+���� �� 20 #�� ���� �����
��� y.1� �)�(��)�� ���#����� )���"���� ��� stdin %�� �)���� ���-���.
� ����)'� �)���� ���"���� )���� #�#� ��)���.��� ��� ���� �)"����� ��� )��%���
)�")�� )���� �� )���"���� ��� ���" ���� �� " ��� �)�� ��� #�)���� ���#����� ��� stdin
��� �)� #�)��� ���-���.
� ����#� � ���#����� \n �)�"��� ��� � ���� ��� �)� ���-��� � �� scanf. ��� ��� (�%�
���� ����+����� �� ������� fflush.
stdio.hscanf("%d",&x);scanf("%d",&y);
15 20 30
B. ����������� !��� �������9. $ ������� fflush()
�%���������� ����������������������� �������� ��!"��
� ,� )�&����)� �� �������� fflush � ���:
� /�� " �� ������ ��� �� � � -�-(����#�:
� $ ������� «�����+��» �� stdin �)� �� �� ���#����� )�� " ��� �)�� ��� �� �����.
� ����%(&�� ��� �� )������%� �� �)���� ���.������ (�� �)� � ��� ���(���� �&���) �
��� �� fflush
int fflush(FILE *fp)
stdio.h
B. ����������� !��� �������9. $ ������� fflush()
�&���������� ����������������������� �������� ��!"��
/* fflush_wrong.c: Epideiksi tis entolis fflush */
#include <stdio.h>
main(){
int x,y,z;char c;
printf("Dwse enan tripsifio arithmo:");scanf("%1d%1d%1d", &x,&y,&z);
printf("Epelekse M-Monades,D-Dekades,E-Ekatontades: ");scanf("%c",&c);
B. ����������� !��� �������9. $ ������� fflush()
�'���������� ����������������������� �������� ��!"��
switch(c){
case 'M': printf("Monades=%d",z);break;
case 'D': printf("Dekades=%d",y);break;
case 'E': printf("Ekatontades=%d",z);break;
default:printf("Lathos eisodos!");
}}
2. ��#�����1. �������)� �� #�.�(� � (����)
�(���������� ����������������������� �������� ��!"��
���0�� "�� )��%��� �� �)� � �"(��� ��:
1. ���-�+�� � ��-�(������ �)� �� )(#���(�%�� (��4 �"(��� �� �)��#������ �� #�)����
) ��#� #�� ��4 �"(��� �� �.�� +���� ���� )�� )(#���(�%� � �����)
2. �.�� +�� ��� ���� �� ��� ��-�(������ �)�� #��� �#��� ���#����� " ��
����#�������� �)� ��� ��� ���� � #�.�(� � ���#����.
2. ��#�����2. ���-��� ��-�(�����'�
#)���������� ����������������������� �������� ��!"��
���0�� "�� )��%��� �� �)� �:
1. �('��� "��� ) ��#� ���#���&� (� ����� � string) �%"���� 10.
2. 6�� �)� ��� ���� �� )(#���(�%���� � ��-�(������.
3. ��� ��-�(������ &���� ��� string �� �� �)��#���� �� ���� #�� �� )��-�((�� ��� «
��-�(������ ���-���#�» #�� �� �� ��)'���.
4. ��� ��-�(������ ��� &���� ��� string �� �)��#���� �� "��� )�� )��� ��
�)��#���� #�� �� )��-�((�� « ��-�(������ ��� ���-���#�» #�� �� ��)'��� �� "���
)�� « '����» ��� string. 4� #���� +�� �� stdin �)� ���� ���#����� )�� " ��� �)�� ���.