What gets() and getch() does in C?

Need help with C, C++, perl, python, etc?

What gets() and getch() does in C?

Postby Grishan Pradhan » 2018-10-08 05:11

In the program:-

Code: Select all
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
char str[40];
printf("Enter your name:");
gets(str);
puts(str);
getch();
}


What gets() and getch() does in this program?
(EDITED)
Last edited by Grishan Pradhan on 2018-10-08 07:38, edited 2 times in total.
Grishan Pradhan
 
Posts: 21
Joined: 2018-09-30 12:46

Re: What gets() and getch() does in C?

Postby roseway » 2018-10-08 05:40

It waits for you to press any key before the program closes.
Eric
User avatar
roseway
 
Posts: 1495
Joined: 2007-12-31 22:50
Location: Kent, UK

Re: What gets() and getch() does in C?

Postby Grishan Pradhan » 2018-10-08 06:36

roseway wrote:It waits for you to press any key before the program closes.

Which one?
Grishan Pradhan
 
Posts: 21
Joined: 2018-09-30 12:46

Re: What gets() and getch() does in C?

Postby GarryRicketson » 2018-10-08 06:56

Well, any key you decide to press.

What gets() and getch() does in C?
User avatar
GarryRicketson
 
Posts: 5131
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: What gets() and getch() does in C?

Postby Grishan Pradhan » 2018-10-08 07:37

Sorry . I there was a mistake on my question.
My question was 'What gets() and getch() does in this program?'
By 'which one?' I meant getch() or gets().
Grishan Pradhan
 
Posts: 21
Joined: 2018-09-30 12:46

Re: What gets() and getch() does in C?

Postby GarryRicketson » 2018-10-08 08:11

You really need to at least try doing some searches before asking these kind of questions,
https://www.tutorialspoint.com/c_standard_library/c_function_gets.htm
The C library function char *gets(char *str) reads a line from stdin and stores it into the string pointed to by str. It stops when either the newline character is read or when the end-of-file is reached, whichever comes first.

===========
https://www.programmingsimplified.com/c/conio.h/getch
getch in C
getch in C language: getch function prompts a user to press a character and that character isn't printed on screen, getch header file is conio.h. This function is not a part of standard C library.


Please Read.. What we expect you have already Done.
User avatar
GarryRicketson
 
Posts: 5131
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: What gets() and getch() does in C?

Postby reinob » 2018-10-08 08:32

Grishan Pradhan wrote:In the program:-

Code: Select all
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
char str[40];
printf("Enter your name:");
gets(str);
puts(str);
getch();
}


What gets() and getch() does in this program?
(EDITED)


You should do your homework yourself.
And you should learn somewhere else. If your teacher is teaching you that then you should ask for a refund a go somewhere else.

In case you or your teacher haven't been awake in the last 30 years: DOS is not a thing anymore. "conio" was a library from Borland (of "Turbo C" fame) for DOS.

More importantly, drop, forget, kill, delete gets(). Never use it. It's bad, it's wrong and it's insecure. Ask your teacher why!

Good luck.

PS: Oh, and main() returns an int. void main() is actually wrong. Ask your teacher why.
reinob
 
Posts: 556
Joined: 2014-06-30 11:42

Re: What gets() and getch() does in C?

Postby Grishan Pradhan » 2018-10-08 08:55

FYI
I don't have any teacher. My class hasn't started yet. I am learning C programming on my own.
Grishan Pradhan
 
Posts: 21
Joined: 2018-09-30 12:46

Re: What gets() and getch() does in C?

Postby Grishan Pradhan » 2018-10-08 09:07

GarryRicketson wrote:You really need to at least try doing some searches before asking these kind of questions,
https://www.tutorialspoint.com/c_standard_library/c_function_gets.htm
The C library function char *gets(char *str) reads a line from stdin and stores it into the string pointed to by str. It stops when either the newline character is read or when the end-of-file is reached, whichever comes first.

===========
https://www.programmingsimplified.com/c/conio.h/getch
getch in C
getch in C language: getch function prompts a user to press a character and that character isn't printed on screen, getch header file is conio.h. This function is not a part of standard C library.


Please Read.. What we expect you have already Done.


Yes , I did what you have expected before I came here but I didn't understand the answers that I found on the internet, I checked tutorialspoint too. But no I didn't get that either. So I came here if I could get the answer that I(beginner) could understand.
Grishan Pradhan
 
Posts: 21
Joined: 2018-09-30 12:46

Re: What gets() and getch() does in C?

Postby reinob » 2018-10-08 09:42

Grishan Pradhan wrote:FYI
I don't have any teacher. My class hasn't started yet. I am learning C programming on my own.


Fair enough. Then I'd recommend you to dump your book and get a new one.
This: https://en.wikipedia.org/wiki/The_C_Pro ... g_Language is still fine to learn C.
(I think it's even freely available online).

gets() is part of the C standard -- but deprecated -- even the manual page says "Never use this function."
It gets a line from the keyboard (stdin) and saves it at the address pointed by the input argument.
It's highly insecure because it will read and store characters until a carriage return/line feed (or end-of-file marker) is received, regardless of how much storage is reserved at the destination address.

getch() was a Borland-specific function for DOS. It reads a single character, without echoing it on screen (thus suitable e.g. for inputting a password).

Cheers.
reinob
 
Posts: 556
Joined: 2014-06-30 11:42

Re: What gets() and getch() does in C?

Postby Grishan Pradhan » 2018-10-08 11:58

reinob wrote:
Grishan Pradhan wrote:FYI
I don't have any teacher. My class hasn't started yet. I am learning C programming on my own.


Fair enough. Then I'd recommend you to dump your book and get a new one.
This: https://en.wikipedia.org/wiki/The_C_Pro ... g_Language is still fine to learn C.
(I think it's even freely available online).

gets() is part of the C standard -- but deprecated -- even the manual page says "Never use this function."
It gets a line from the keyboard (stdin) and saves it at the address pointed by the input argument.
It's highly insecure because it will read and store characters until a carriage return/line feed (or end-of-file marker) is received, regardless of how much storage is reserved at the destination address.

getch() was a Borland-specific function for DOS. It reads a single character, without echoing it on screen (thus suitable e.g. for inputting a password).

Cheers.


Thanks Mate!
Grishan Pradhan
 
Posts: 21
Joined: 2018-09-30 12:46

Re: What gets() and getch() does in C?

Postby pylkko » 2018-10-08 17:41

Grishan Pradhan wrote:
roseway wrote:It waits for you to press any key before the program closes.

Which one?


Just an idea. But if you want to learn programming, then it might be helpful to analyse questions and propositions and attempt to communicate in a unambiguous way. Here when you asked "which one", from the point of view of the reader, it could have referred to a) the pressed key (as Gary assumed), b) the function or possibly c) a program.

Not only does formulating a question in an unambiguous manner help people understand you, it is a skill that will be helpful in programming.
User avatar
pylkko
 
Posts: 1335
Joined: 2014-11-06 19:02

Re: What gets() and getch() does in C?

Postby Grishan Pradhan » 2018-10-13 16:20

pylkko wrote:
Grishan Pradhan wrote:
roseway wrote:It waits for you to press any key before the program closes.

Which one?


Just an idea. But if you want to learn programming, then it might be helpful to analyse questions and propositions and attempt to communicate in a unambiguous way. Here when you asked "which one", from the point of view of the reader, it could have referred to a) the pressed key (as Gary assumed), b) the function or possibly c) a program.

Not only does formulating a question in an unambiguous manner help people understand you, it is a skill that will be helpful in programming.


You got it wrong mate. Read my other replies.

When Gary answered my question there was a mistake on my question. I had asked about getch() only at that time while I was supposed to ask 'get() and getch()' , later I edited that.
Grishan Pradhan
 
Posts: 21
Joined: 2018-09-30 12:46


Return to Programming

Who is online

Users browsing this forum: No registered users and 3 guests

fashionable