This question already has an answer here:
C Cyrillic console input with standard char data type instead of wchar_t
(1 answer)
Closed 6 days ago.
The purpose of the program is to get a symbol and then immediately display it on the screen. But the problem is that it outputs a different character, or nothing at all. I found that the output is in Windows-1251 encoding, and the input is in CP866. How do I solve this problem? How to make both output and input in Windows-1251 encoding.
Post Scriptum: the problem appears when you enter a Cyrillic character.
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
int main(void)
{
setlocale(LC_ALL, "");
printf("Уведіть символ.\n");
char ch = getchar();
printf("\n");
printf("%c", ch);
getchar();
getchar();
return 0;
}
I tried to use wchar_t (respectively procedures wprintf(), getwchar()), but the situation did not change.
Maybe this will help?
https://stackoverflow.com/a/44167461/8893124
they suggest to set:
system("chcp 1251");
setlocale(LC_ALL, "UTF8");
I found another way to solve this problem. Maybe someone will find it useful. However, it works only for Windows.
For this you need to include the header file <windows.h> and write before entering the following:
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
Then the input/output will be in Windows-1251 encoding.
Related
I'm using eclipse in Windows, and minGW.
I write the easy code like this.
#include <stdio.h>
int main()
{
char c;
printf("input: ");
scanf("%c",&c);
return 0;
}
Buffer error is rised, and I search the stackoverflow. It told me to use fflush().
And it works.
But, I must write fflush() after each printf().
I don't wanna do this.
I want to solve this problem in one statement.
This question already has answers here:
Hide password input on terminal
(16 answers)
Closed 5 years ago.
I want to know how to take characters from the console using
scanf, getch()
or something else and at the time that the user gives the input I want the screen to show:
******
for every character.
If you are on Windows, I think you can use something like given below -
#include <stdio.h>
#include <conio.h>
int main(){
char str[8];
int i=0;
printf("Enter the password :\n");
while (i< 8){
str[i]=getch();
printf("*");
i++;
}
str[i]='\0';
return 0;
}
The getch() function is not so readily available on Unix-like systems. (The curses or ncurses library provides a function getch(), but you have to call functions to setup the terminal correctly, and then reset it back to a known state.)
I am new to C and so I wanted to get more experience with the libraries function. When I started fiddling with getch() I could't for the life of me get it to work. All that happened was that the characters were echoed back to the console and I could type multiple and even press enter without it continuing throughout the program. Here is a little test code I made. Any idea where I am going wrong?
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(){
char i = '9';
i = getch();
printf("%c", i);
}
EDIT: Thank you all for your answers although I could've been more clear on what I wanted. I want to be getch() to be used almost like fgets() except I don't want it to echo and I want the console to accept the character entered immediately without any enter key pressed. Is getch the wrong tool to do this with?
Change this
i = getch();
to
i = getchar();
getchar() is a standard function that gets a character from the stdin.
getch() is non-standard. It gets a character from the keyboard (which could be different from stdin) and does not echo it.
getch() is a function is used to end or close main function used in windows with conio header file.
there is no use of stdlib header function it is used while taking string
as a function.
%c is used to print characters.
if u wants to print i than go like as mention below:-
#include <stdio.h>
#include <conio.h>
void main()
{
int i = 9;
printf("%d", i);
getch();
}
This question already has answers here:
fgets instructions gets skipped.Why?
(3 answers)
Closed 6 years ago.
I have written a simple program in C which is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int length;
printf("Enter the length of the string:\t");
scanf("%d",&length);
char str1[10];
printf("Enter string:\t");
gets(str1);
printf("%s",str1);
return 0;
}
When I execute it - I get an output as:
Enter the length of the string: 5
Enter string:
Process returned 0 (0x0) execution time : 1.740 s
Press any key to continue.
I don't know why it doesn't ask for the string input and simply quits the program.
When you type '5’ followed by the enter key, you are sending two chars to the program - '5' and newline. So your first scanf gets the '5' and the second gets the newline, which it converts to the number zero.
See How to read a line from the console in C?
When you enter 5 and press enter which is "\n" then "\n" remains in stream and gets assigned to str1. You need to take that "\n" out of the input stream for which many choices are there. You can figure that out. :) Perhaps later I will edit this answer to let you know.
Edit 1:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int length;
char c;
printf("Enter the length of the string:\t");
scanf("%d%c",&length, &c);
char str1[10];
printf("Enter string:\t");
gets(str1);
printf("%s",str1);
return 0;
}
This is incorrect way of doing it but your code will at least start working. You can also simply call getc(stdin) which is slightly better. The scanf regex specified in the other answers where it has been marked as duplicate will also work but is ugly and unnecessarily complicated.
I have not tested this and it may not work.
This question already has answers here:
In C, how should I read a text file and print all strings
(9 answers)
Closed 8 years ago.
#include "stdio.h"
int main(){
char str[20];
while(scanf("%19[^\n]",str)==1){
printf("%s",str);
}
return 0;
}
Compiled using:
$ gcc file.c -o file
$ file < input.txt
The program is reading only first line from the file input.txt :
hello this is
a test that
should make it
happen
I want the program to read the complete file, please help
Add a space:
while(scanf(" %19[^\n]",str)==1){
^
The space (unintuitively) consumes any white-space, including the \n, which otherwise you are not handling.
Of course, it is generally better to use e.g. fgets() and sscanf() rather than scanf() to parse input.
This changes the logic of you code slightly, but maybe better captures your intent. Any attempt to only skip only \n rather than all white-space, as in:
while(scanf("%19[^\n]\n",str)==1){
will fail, because here the second \n is exactly the same as a space .
Find the modified code
#include <stdio.h>
int main()
{
char str[20];
while(gets(str)!=NULL)
{
printf("%s",str);
}
return 0;
}