This is default featured slide 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Wednesday, 16 March 2016


Difference between getc(), getchar(), getch() and getche()


All of these functions read a character from input and return an integer value. The integer is returned to accommodate a special value used to indicate failure. The value EOF is generally used for this purpose.
getc():
It reads a single character from a given input stream and returns the corresponding integer value (typically ASCII value of read character) on success. It returns EOF on failure.
Syntax:
int getc(FILE *stream); 
Example:
// Example for getc() in C
#include <stdio.h>
int main()
{
   printf("%c", getc(stdin));
   return(0);
}
Input: g (press enter key)
Output: g 
getchar():
The difference between getc() and getchar() is getc() can read from any input stream, but getchar() reads from standard input. So getchar() is equivalent to getc(stdin).
Syntax:
int getchar(void); 
Example:
// Example for getchar() in C
#include <stdio.h>
int main()
{
   printf("%c", getchar());
   return 0;
}

Input: g(press enter key)
Output: g 
getch():
getch() is a nonstandard function and is present in conio.h 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 (Source: http://en.wikipedia.org/wiki/Conio.h)
Like above functions, it reads also a single character from keyboard. But it does not use any buffer, so the entered character is immediately returned without waiting for the enter key.
Syntax:
int getch();
Example:
// Example for getch() in C
#include <stdio.h>
#include <conio.h>
int main()
{
   printf("%c", getch());  
   return 0;
}

Input:  g (Without enter key)
Output: Program terminates immediately.
        But when you use DOS shell in Turbo C, 
        it shows a single g, i.e., 'g'
getche()
Like getch(), this is also a non-standard function present in conio.h. It reads a single character from the keyboard and displays immediately on output screen without waiting for enter key.
Syntax:
int getche(void); 
Example:
#include <stdio.h>
#include <conio.h>
// Example for getche() in C
int main()
{
  printf("%c", getche());
  return 0;
}

Input: g(without enter key as it is not buffered)
Output: Program terminates immediately.
        But when you use DOS shell in Turbo C, 
        double g, i.e., 'gg'

Input and Output

Question 1
Predict the output of following program?
#include "stdio.h"
int main()
{
    char arr[100];
    printf("%d", scanf("%s", arr));
    /* Suppose that input value given
        for above scanf is "GeeksQuiz" */
    return 1;
}

A
9
B
1
C
10
D
100
Input and Output    
Discuss it

Question 2
Predict output of the following program
#include <stdio.h>
int main()
{
   printf("\new_c_question\by");
   printf("\rgeeksforgeeks");
   getchar();
   return 0;
}

A
ew_c_question
geeksforgeeks
B
new_c_ques
geeksforgeeks
C

geeksforgeeks
D
Depends on terminal configuration
Input and Output    
Discuss it

Question 3
#include <stdio.h>
int main()
{
  printf(" \"GEEKS %% FOR %% GEEKS\"");
  getchar();
  return 0;
}

A
“GEEKS % FOR % GEEKS”
B
GEEKS % FOR % GEEKS
C
\"GEEKS %% FOR %% GEEKS\"
D
GEEKS %% FOR %% GEEKS
Input and Output    
Discuss it

Question 4
#include <stdio.h>
// Assume base address of "GeeksQuiz" to be 1000
int main()
{
   printf(5 + "GeeksQuiz");
   return 0;
}

A
GeeksQuiz
B
Quiz
C
1005
D
Compile-time error
Input and Output    
Discuss it

Question 5
Predict the output of the below program:
#include <stdio.h>
int main()
{
    printf("%c ", 5["GeeksQuiz"]);
    return 0;
}

A
Compile-time error
B
Runtime error
C
Q
D
s
Input and Output    
Discuss it

Question 6
Predict the output of below program:
#include <stdio.h>
int main()
{
    printf("%c ", "GeeksQuiz"[5]);
    return 0;
}

A
Compile-time error
B
Runtime error
C
Q
D
s
Input and Output    
Discuss it

Question 7
Which of the following is true
A
gets() can read a string with newline chacters but a normal scanf() with %s can not.
B
gets() can read a string with spaces but a normal scanf() with %s can not.
C
gets() can always replace scanf() without any additional code.
D
None of the above
Input and Output    
Discuss it

Question 8
Which of the following is true
A
gets() doesn't do any array bound testing and should not be used.
B
fgets() should be used in place of gets() only for files, otherwise gets() is fine
C
gets() cannot read strings with spaces
D
None of the above
Input and Output    
Discuss it

Question 9
What does the following C statement mean?
scanf("%4s", str);

A
Read exactly 4 characters from console.
B
Read maximum 4 characters from console.
C
Read a string str in multiples of 4
D
Nothing
Input and Output    
Discuss it

Question 10
#include<stdio.h>
int main()
{
    char *s = "Geeks Quiz";
    int n = 7;
    printf("%.*s", n, s);
    return 0;
}

A
Geeks Quiz
B
Nothing is printed
C
Geeks Q
D
Geeks Qu
Input and Output    
Discuss it

Question 11
Predict the output of following program?
#include <stdio.h>
int main(void)
{
   int x = printf("GeeksQuiz");
   printf("%d", x);
   return 0;
}

A
GeeksQuiz9
B
GeeksQuiz10
C
GeeksQuizGeeksQuiz
D
GeeksQuiz1
Input and Output    
Discuss it

Question 12
Output of following program?
#include<stdio.h>
int main()
{
    printf("%d", printf("%d", 1234));
    return 0;
}

A
12344
B
12341
C
11234
D
41234
Input and Output    
Discuss it

Question 13
What is the return type of getchar()?
A
int
B
char
C
unsigned char
D
float
Input and Output    
Discuss it

Question 14
Normally user programs are prevented from handling I/O directly by I/O instructions in them. For CPUs having explicit I/O instructions, such I/O protection is ensured by having the I/O instructions privileged. In a CPU with memory mapped I/O, there is no explicit I/O instruction. Which one of the following is true for a CPU with memory mapped I/O?
A
I/O protection is ensured by operating system routine (s)
B
I/O protection is ensured by a hardware trap
C
I/O protection is ensured during system configuration
D
I/O protection is not possible
Input and Output    GATE-CS-2005    
Discuss it

Question 15
Which of the following functions from “stdio.h” can be used in place of printf()?
A
fputs() with FILE stream as stdout.
B
fprintf() with FILE stream as stdout.
C
fwrite() with FILE stream as stdout.
D
All of the above three - a, b and c.
E
In “stdio.h”, there’s no other equivalent function of printf().