473,785 Members | 2,317 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

K&R2 section 1.5.1 - "getchar" problem

i have slightly modified the programme from section 1.5.1 which takes
the input frm keyboard and then prints that to the terminal. it just
does not run and i am unable to understand the error message.

may you tell me what is wrong and how to make that right ?

------------------------------ INPUT ---------------------
#include <stdio.h>

int main() {
int c;

while((c = getchar()) != EOF)
{
printf("your input: ");
putchar(c);
printf("\n");
}

return 0;
}

--------------------- OUTPUT -----------------------------
[arch@voodo kr2]$ gcc -ansi -pedantic -Wall -Wextra 151.c
151.c:1: error: expected identifier or '(' before '/' token
151.c:2:12: error: too many decimal points in number
In file included from /usr/include/_G_config.h:44,
from /usr/include/libio.h:32,
from /usr/include/stdio.h:72,
from 151.c:4:
/usr/include/gconv.h:72: error: expected declaration specifiers or
'...' before 'size_t'
/usr/include/gconv.h:88: error: expected declaration specifiers or
'...' before 'size_t'
/usr/include/gconv.h:97: error: expected declaration specifiers or
'...' before 'size_t'
/usr/include/gconv.h:174: error: expected specifier-qualifier-list
before 'size_t'
In file included from /usr/include/stdio.h:72,
from 151.c:4:
/usr/include/libio.h:328: error: expected specifier-qualifier-list
before 'size_t'
/usr/include/libio.h:360: error: expected declaration specifiers or
'...' before 'size_t'
/usr/include/libio.h:369: error: expected declaration specifiers or
'...' before 'size_t'
/usr/include/libio.h:485: error: expected '=', ',', ';', 'asm' or
'__attribute__' before '_IO_sgetn'
In file included from 151.c:4:
/usr/include/stdio.h:306: error: expected declaration specifiers or
'...' before 'size_t'
/usr/include/stdio.h:608: error: expected '=', ',', ';', 'asm' or
'__attribute__' before 'fread'
/usr/include/stdio.h:614: error: expected '=', ',', ';', 'asm' or
'__attribute__' before 'fwrite'
[arch@voodo kr2]$

Mar 8 '07
14 2862
arnuld wrote:
On Mar 8, 12:17 pm, Keith Thompson <k...@mib.orgwr ote:
The only thing I can think of is that your source file originally
contained some funny characters.

yes, it did contain some *funny* characters. actually they were at the
beginning of programme. they look like:

// K&R2 section 1.5.1
//

i was writing C++ in C
Impossible. You were trying to feed the compiler C99 features while
telling it to stick to strict C89.

[ ... ]
[arch@voodo kr2]$ ./a.out
a
your input: a
your input:
b
your input: b
your input:
[arch@voodo kr2]$
why i am getting "your input" 2 times, instead of one ?
Let me guess. You typed a letter 'a', then hit the return key.
That's two characters.

NO, i am not talking about characters. i am talking about "words":
your input

after getchar(), there must be one sentence:

"your input: a"
but i get 2 sentences. 2nd one is:

"your input: "
why so ? (when i have used it once only in programme)
When you type in normal characters through the keyboard, under line
buffered systems, (which happens to be almost all operating systems),
you need to press the Return or Enter key to signal that your input
can be processed. The Enter key also generates a perfectly ordinary
value, (or values), like all other keys do. So your program is
actually receiving the value of 'a' followed by the value for '\n',
(which is C's representation of the system's end-of-line sequence,
which is itself generated by pressing the Return or Enter key.)

Both key values, ('a' and '\n'), are stored into the C library's
internal buffer for stdin stream. getchar goes and retrieves the top
character of the buffer, ('a'), and your loop prints it, followed by
your newline. The next iteration of loop calls getchar once again. You
_meant_ it to block for fresh input from the keyboard. But the '\n' is
still sitting in the internal buffer, and in C, it's a perfectly valid
character, which getchar retrieves and you print once again, as a
seemingly empty statement, but if you observe closely, you'll spot an
extra blank line. That's the effect of printing the '\n' character.

This "issue" confuses many a newbie. Sometimes this behaviour of the
stdio functions is expected, sometimes it's an annoyance. If you don't
want this behaviour you'll have to "read" and discard the remaining
characters in the internal buffer uptil, and including, the next
newline. You can do it through a simple while loop and wrap it up in a
function named, say, consume_line or something.

Mar 8 '07 #11
On Mar 8, 4:53 pm, "santosh" <santosh....@gm ail.comwrote:
// K&R2 section 1.5.1
//
i was writing C++ in C

Impossible. You were trying to feed the compiler C99 features while
telling it to stick to strict C89.
which one is a good practice: C99 or C89 ?

if it is C99 then just tell what options of "gcc" on Linux make it C99
compliant.

When you type in normal characters through the keyboard, under line
buffered systems, (which happens to be almost all operating systems),
you need to press the Return or Enter key to signal that your input
can be processed. The Enter key also generates a perfectly ordinary
value, (or values), like all other keys do. So your program is
actually receiving the value of 'a' followed by the value for '\n',
(which is C's representation of the system's end-of-line sequence,
which is itself generated by pressing the Return or Enter key.)

Both key values, ('a' and '\n'), are stored into the C library's
internal buffer for stdin stream. getchar goes and retrieves the top
character of the buffer, ('a'), and your loop prints it, followed by
your newline. The next iteration of loop calls getchar once again. You
_meant_ it to block for fresh input from the keyboard. But the '\n' is
still sitting in the internal buffer, and in C, it's a perfectly valid
character, which getchar retrieves and you print once again, as a
seemingly empty statement, but if you observe closely, you'll spot an
extra blank line. That's the effect of printing the '\n' character.

This "issue" confuses many a newbie. Sometimes this behaviour of the
stdio functions is expected, sometimes it's an annoyance. If you don't
want this behaviour you'll have to "read" and discard the remaining
characters in the internal buffer uptil, and including, the next
newline. You can do it through a simple while loop and wrap it up in a
function named, say, consume_line or something.
thanks for the pretty long explanation

:-)

Mar 8 '07 #12
arnuld wrote:
On Mar 8, 4:53 pm, "santosh" <santosh....@gm ail.comwrote:
// K&R2 section 1.5.1
//
i was writing C++ in C
Impossible. You were trying to feed the compiler C99 features while
telling it to stick to strict C89.

which one is a good practice: C99 or C89 ?
Anyone. The core language hasn't changed all that much. The constructs
that've been added are not likely to be used at a beginner level. If
your compiler and standard library has acceptable amount of C99
coverage, and you're aware of the possibility of your code not working
with other implementations , then there's nothing wrong in learning C99
features. It is, after all, the current Standard for C.
if it is C99 then just tell what options of "gcc" on Linux make it C99
compliant.
First off gcc isn't yet fully C99 compliant, though it's getting there
faster than some other compilers.

The -std=c99 and -pedantic options can be used. Also you might want to
always use the -Wall and -Wextra options for maximum diagnostic
output. The manual for gcc has lots of interesting information you can
use to tailor gcc's behaviour for your use. Invoke 'info gcc'.

Mar 8 '07 #13
On Mar 8, 10:27 pm, "santosh" <santosh....@gm ail.comwrote:

which one is a good practice: C99 or C89 ?

Anyone. The core language hasn't changed all that much. The constructs
that've been added are not likely to be used at a beginner level. If
your compiler and standard library has acceptable amount of C99
coverage, and you're aware of the possibility of your code not working
with other implementations , then there's nothing wrong in learning C99
features. It is, after all, the current Standard for C.
if it is C99 then just tell what options of "gcc" on Linux make it C99
compliant.

First off gcc isn't yet fully C99 compliant, though it's getting there
faster than some other compilers.

i did not know that.
The -std=c99 and -pedantic options can be used. Also you might want to
always use the -Wall and -Wextra options for maximum diagnostic
output. The manual for gcc has lots of interesting information you can
use to tailor gcc's behaviour for your use. Invoke 'info gcc'.
thanks buddy

;-)

Mar 8 '07 #14
arnuld <ge*********@gm ail.comwrote:
>which one is a good practice: C99 or C89 ?
C99 isn't entirely widespread yet, so that might be a problem for
portability. Personally, I don't shy away from it.

You should at least know about the new features in C99 so you don't end
up doing things like this:

int restrict = 1;
>if it is C99 then just tell what options of "gcc" on Linux make it C99
compliant.
http://gcc.gnu.org/onlinedocs/gcc-4....t-Options.html

Check out "-std=".

gcc isn't fully compliant yet. Here's a link with the details:

http://gcc.gnu.org/gcc-4.1/c99status.html

-Beej

Mar 8 '07 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2179
by: Chris Readle | last post by:
Ok, I've just recently finished a beginning C class and now I'm working through K&R2 (alongside the C99 standard) to *really* learn C. So anyway, I'm working on an exercise in chapter one which give me strange behavior. Here is the code I've written: /****************************************************************************** * K&R2 Exercise 1-9 * Write a program to copy its input to its output, replacing strings of blanks * with a...
16
2281
by: Josh Zenker | last post by:
This is my attempt at exercise 1-10 in K&R2. The code looks sloppy to me. Is there a more elegant way to do this? #include <stdio.h> /* copies input to output, printing */ /* series of blanks as a single one */ int main() { int c;
8
4763
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. --------------------------------- PROGRAMME -------------------------------- /* K&R2 section 1.9 exercise 1.19
12
2224
by: arnuld | last post by:
this is exercise 2-3 from the mentioned section. i have created a solution for it but i see errors and i tried to correct them but still they are there and mostly are out of my head: ------------------------------- PROGRAMME -------------------------- /* Section 2.7 type conversions we are asked to write a function "htoi(an array)" that accomplishes this:
16
1813
by: arnuld | last post by:
i have created solution which compiles and runs without any error/ warning but it does not work. i am not able to understand why. i thought it is good to post my code here for correction before looking at CLC-Wiki for K&R2 solutions: --------------- PROGRAMME ------------ /* K&R2 section 1.5.3, exercise 1-9 STATEMENT: write a programme to copy its input to output replacing
5
2919
by: arnuld | last post by:
this is a programme that counts the "lengths" of each word and then prints that many of stars(*) on the output . it is a modified form of K&R2 exercise 1-13. the programme runs without any compile-error BUT it has a semantic BUG: what i WANT: I want it to produce a "horizontal histogram" which tells how many characters were in the 1st word, how many characters were in the second word by writing equal number of stars, *, at the...
16
1740
by: arnuld | last post by:
i am not able to make it work. it compiles without any error but does not work: what i WANTED: 1.) 1st we will take the input in to an array. (calling "getline" in "main") 2.) we will print that input array on terminal. (in "main") 3.) we will reverse the array. (calling "reverse" in "main") 4.) we will print that reversed array. (in "main")
2
1982
by: arnuld | last post by:
on page 29, section 1.9 Character Arrays, i see an example: /* getline: read a line into s, return length */ int getline(char s,int lim) { int c, i; for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i) s = c; if (c == '\n') {
15
1823
by: arnuld | last post by:
STATEMENT: Write the function strindex(s, t), which returns the position of the rightmost occurence of t in s, or -1 if there is none. PURPOSE: this program prints the index of the rightmost match on the line. The match we have to find is a char array named pattern. We also print out the number of matches we have found. We will take the input from command-line. PROBLEM: Segmentation Fault
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10315
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10083
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.