Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

int c = getchar()?

Tags:

ok so im reading this book: The C Programming Language - By Kernighan and Ritchie (second Edition) and one of the examples im having trouble understanding how things are working.

#include <stdio.h>  #define MAXLINE 1000  int getline(char line[], int maxline); void copy(char to[], char from[]);  int main(int argc, char *argv[]) {     int len;      int max;     char line[MAXLINE];     char longest[MAXLINE];      max = 0;     while((len = getline(line, MAXLINE)) > 1)     {         if(len > max)         {             max = len;             copy(longest, line);         }     }     if(max > 0)         printf("%s", longest);      getchar();     getchar();     return 0;    }  int getline(char s[], int lim) {     int c, i;      for(i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)         s[i] = c;     if(c == '\n')     {         s[i] = c;         ++i;          }     s[i] = '\0';      return i; }  void copy(char to[], char from[]) {     int i;      i = 0;     while((to[i] = from[i]) != '\0')         ++i; } 

the line : for(i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) where it says c = getchar(), how can an integer = characters input from the command line? Integers yes but how are the characters i type being stored?

Thanks in advance

like image 861
Rhexis Avatar asked Aug 19 '11 09:08

Rhexis


People also ask

What does getchar () do in C?

getchar is a function in C programming language that reads a single character from the standard input stream stdin, regardless of what it is, and returns it to the program. It is specified in ANSI-C and is the most basic input function in C.

Can I use Getchar for int?

No you can't use getchar() for int , float, double as it takes single character as input.

What is the use of getchar () statement?

The getchar() function is equivalent to a call to getc(stdin). It reads the next character from stdin which is usually the keyboard. It is defined in <cstdio> header file.

Why should the return value from Getchar be int and not char type?

Because a char in a file may be any possible char value, including the null character that C-strings use for termination, getchar() must use a larger integer type to add an EOF-value. It simply happens to use int for that purpose, but it could use any type with at least 9 bit.


2 Answers

Unlike some other languages you may have used, chars in C are integers. char is just another integer type, usually 8 bits and smaller than int, but still an integer type.

So, you don't need ord() and chr() functions that exist in other languages you may have used. In C you can convert between char and other integer types using a cast, or just by assigning.

Unless EOF occurs, getchar() is defined to return "an unsigned char converted to an int" (same as fgetc), so if it helps you can imagine that it reads some char, c, then returns (int)(unsigned char)c.

You can convert this back to an unsigned char just by a cast or assignment, and if you're willing to take a slight loss of theoretical portability, you can convert it to a char with a cast or by assigning it to a char.

like image 61
Steve Jessop Avatar answered Sep 28 '22 02:09

Steve Jessop


The getchar() function returns an integer which is the representation of the character entered. If you enter the character A, you will get 'A' or 0x41 returned (upgraded to an int and assuming you're on an ASCII system of course).

The reason it returns an int rather than a char is because it needs to be able to store any character plus the EOF indicator where the input stream is closed.

And, for what it's worth, that's not really a good book for beginners to start with. It's from the days where efficiency mattered more than readability and maintainability.

While it shows how clever the likes of K&R were, you should probably be looking at something more ... newbie-friendly.

In any case, the last edition of it covered C89 and quite a lot has changed since then. We've been through C99 and now have C11 and the book hasn't been updated to reflect either of them, so it's horribly out of date.

like image 44
paxdiablo Avatar answered Sep 28 '22 00:09

paxdiablo