{"id":3658,"date":"2024-03-06T11:39:49","date_gmt":"2024-03-06T11:39:49","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=3658"},"modified":"2024-03-06T11:39:49","modified_gmt":"2024-03-06T11:39:49","slug":"difference-between-getc-getchar-getch-and-getche","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/difference-between-getc-getchar-getch-and-getche\/","title":{"rendered":"Difference Between Getc(), Getchar(), Getch() and Getche()"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#difference-between-getc-getchar-getch-and-getche\">Difference between getc(), getchar(), getch() and getche()<\/a><\/li><li ><a href=\"#getc\">getc()<\/a><\/li><li ><a href=\"#getchar\">getchar()<\/a><\/li><li ><a href=\"#getch\">getch()<\/a><\/li><li ><a href=\"#getche\">getche()<\/a><\/li><li ><a href=\"#faq-difference-between-getc-getchar-getch-and-getche\">FAQ-  Difference between getc(), getchar(), getch() and getche()<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"difference-between-getc-getchar-getch-and-getche\">Difference between getc(), getchar(), getch() and getche()<\/h2>\n\n\n\n<p>When you use functions like <code>getc()<\/code>, <code>fgetc()<\/code>, and <code>getchar()<\/code> to read characters, they return a special value called EOF if something goes wrong or if there are no more characters to read.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"getc\">getc()<\/h2>\n\n\n\n<p>It will read a single character from a given input stream and thus, return the corresponding integer value (typically ASCII value of read character) on success. It returns EOF on failure.<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int getc(FILE *stream);\n<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Example for getc() in C\n#include &lt;stdio.h&gt;\nint main()\n{\n    printf(\"%c\", getc(stdin));\n    return (0);\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Input: g (press enter key)\nOutput: g<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"getchar\">getchar()<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>getc():<\/strong>\n<ul class=\"wp-block-list\">\n<li>Reads a character from any specified input stream.<\/li>\n\n\n\n<li>Example: <code>char ch = getc(filePointer);<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>getchar():<\/strong>\n<ul class=\"wp-block-list\">\n<li>Reads a single character from the standard input (stdin).<\/li>\n\n\n\n<li>Equivalent to <code>getc(stdin)<\/code>.<\/li>\n\n\n\n<li>Example: <code>char ch = getchar();<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int getchar(void);\n<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Example for getchar() in C\n#include &lt;stdio.h&gt;\nint main()\n{\n    printf(\"%c\", getchar());\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Input: g(press enter key)\nOutput: g<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"getch\">getch()<\/h2>\n\n\n\n<p>Like the above functions, getch() also reads a single character from the keyboard. But it does not use any buffer, so the entered character does not display on the screen and is immediately returned without waiting for the enter key.<\/p>\n\n\n\n<p>getch() is a nonstandard function and is present in &lt;conio.h&gt; header file which is mostly used by MS-DOS compilers like Turbo C. It is not part of the C standard library or ISO C, nor is it defined by POSIX.<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int getch();<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n\/\/ Example for getch() in C\n#include &lt;conio.h&gt;\n#include &lt;stdio.h&gt;\nint main()\n{\n    printf(\"%c\", getch());\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Input:  g (Without enter key)\nOutput: Program terminates immediately.\n        But when you use DOS shell in Turbo C,\n        it shows a single g, i.e., 'g'<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"getche\">getche()<\/h2>\n\n\n\n<p><strong>getche() Function:<\/strong><\/p>\n\n\n\n<p><code>getche()<\/code> reads a character from the keyboard and immediately displays it on the screen without waiting for the enter key. It&#8217;s a non-standard function in <code>&lt;conio.h&gt;<\/code>, commonly used in MS-DOS compilers like Turbo C. Example: <code>char ch = getche();<\/code><\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int getche(void);<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n\/\/ Example for getche() in C\n#include &lt;conio.h&gt;\n#include &lt;stdio.h&gt;\nint main()\n{\n    printf(\"%c\", getche());\n    return 0;\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Input:  g(without enter key as it is not buffered)\nOutput: Program terminates immediately.\n        But when you use DOS shell in Turbo C, \n        double g, i.e., 'gg'<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-difference-between-getc-getchar-getch-and-getche\">FAQ-  Difference between getc(), getchar(), getch() and getche()<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1700564167463\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is the difference between Getchar () and gets () function?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Reads from stdin until an end-of-line or end-of-file.<br \/>Unsafe due to lack of buffer size checking.<br \/>Prone to buffer overflow vulnerabilities.<br \/><strong>getchar():<\/strong><br \/>Reads a single character from stdin.<br \/>Safer alternative compared to gets().<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1700564210239\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is the difference between getc and gets?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.<strong>getc() and gets() Functions:<\/strong><br \/><strong>getc():<\/strong><br \/>Reads a single character from a file stream.<br \/>Used for character-wise reading.<br \/>Example: <code>char ch = getc(filePointer);<\/code><br \/><strong>gets():<\/strong><br \/>Reads a string from the standard input (stdin).<br \/>Deprecated and considered unsafe due to lack of buffer size checking.<br \/><strong>Differences:<\/strong><br \/><code>getc()<\/code> reads characters individually.<br \/><code>gets()<\/code> reads a sequence of characters until an end-of-line or end-of-file.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1700564214993\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is the use of Getchar () in C with example?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.<strong>Usage:<\/strong><br \/>Used when single-character input is needed from the user.<br \/><strong>Operation:<\/strong><br \/>Reads the input as an unsigned char.<br \/>Casts and returns it as an int or EOF.<br \/><strong>Return Values:<\/strong><br \/>Returns the character as an int or EOF.<br \/>EOF is returned if the end of the file is reached or an error occurs.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Difference between getc(), getchar(), getch() and getche() When you use functions like getc(), fgetc(), and getchar() to read characters, they return a special value called EOF if something goes wrong or if there are no more characters to read. getc() It will read a single character from a given input stream and thus, return the &#8230; <a title=\"Difference Between Getc(), Getchar(), Getch() and Getche()\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/difference-between-getc-getchar-getch-and-getche\/\" aria-label=\"More on Difference Between Getc(), Getchar(), Getch() and Getche()\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":3662,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[600,601],"class_list":["post-3658","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-difference-between-getc","tag-getchar","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3658","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=3658"}],"version-history":[{"count":8,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3658\/revisions"}],"predecessor-version":[{"id":7954,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3658\/revisions\/7954"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/3662"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=3658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=3658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=3658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}