473,769 Members | 3,857 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 #1
14 2859
On Mar 8, 11:07 am, "arnuld" <geek.arn...@gm ail.comwrote:
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 ?
refer this link,
http://gcc.gnu.org/ml/gcc/2005-03/msg00865.html

if its match with your configuration (OS+compiler+li bs+headers) apply
latest patch or do needful.
>
------------------------------ 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
I dont think -Wextra needed (or it is there) after -Wall
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]$
posted to gcc.gnu.help so u may got more help from other people.

Its failed telling
"Google Groups does not currently support posting to the following
usenet groups: "gcc.gnu.he lp" " but you may try there.


--Raxit Sheth

Mar 8 '07 #2
"arnuld" <ge*********@gm ail.comwrites:
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]$
I just tried your program, no problem...are you sure you are post
exactly what you input?
Mar 8 '07 #3
"arnuld" <ge*********@gm ail.comwrites:
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
[more error messages snipped]

When I compile your program with the same options, it compiles without
errors or warnings and works properly. Probably your <stdio.hheade r
is messed up and/or your compiler is misconfigured.

I'm assuming the code you posted is exactly what you compiled
(copy-and-pasted, not re-typed).

Try compiling a C source file consisting only of a single line:

#include <stdio.h>

You'll probably see the same problem.

If I'm right, this is a question for gnu.gcc.help. Be sure to tell
them the details of your system (what operating system, what version
of gcc, and how gcc was installed); such details are usually off-topic
here, but often vital in tracking down a compiler-specific problem.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 8 '07 #4
On Mar 8, 11:34 am, Keith Thompson <k...@mib.orgwr ote:

When I compile your program with the same options, it compiles without
errors or warnings and works properly. Probably your <stdio.hheade r
is messed up and/or your compiler is misconfigured.

I'm assuming the code you posted is exactly what you compiled
(copy-and-pasted, not re-typed).
YES, i did copy-paste.

Try compiling a C source file consisting only of a single line:

#include <stdio.h>
You'll probably see the same problem.

no trouble with that.

If I'm right, this is a question for gnu.gcc.help. Be sure to tell
them the details of your system (what operating system, what version
of gcc, and how gcc was installed); such details are usually off-topic
here, but often vital in tracking down a compiler-specific problem.

i do not know BUT this time, i *copied* the programme from my *post*
here to *emacs* and it worked, i do not how. this is th output:
[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 ?
--
Keith Thompson (The_Other_Keit h) k...@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Mar 8 '07 #5
"Sheth Raxit" <ra************ @yahoo.co.inwri tes:
[...]
posted to gcc.gnu.help so u may got more help from other people.

Its failed telling
"Google Groups does not currently support posting to the following
usenet groups: "gcc.gnu.he lp" " but you may try there.
It's gnu.gcc.help, not gcc.gnu.help.

Please don't use silly abbreviations like "u". This isn't a chat
room.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 8 '07 #6
"arnuld" <ge*********@gm ail.comwrites:
>On Mar 8, 11:34 am, Keith Thompson <k...@mib.orgwr ote:
When I compile your program with the same options, it compiles without
errors or warnings and works properly. Probably your <stdio.hheade r
is messed up and/or your compiler is misconfigured.

I'm assuming the code you posted is exactly what you compiled
(copy-and-pasted, not re-typed).

YES, i did copy-paste.

>Try compiling a C source file consisting only of a single line:

#include <stdio.h>
You'll probably see the same problem.


no trouble with that.

>If I'm right, this is a question for gnu.gcc.help. Be sure to tell
them the details of your system (what operating system, what version
of gcc, and how gcc was installed); such details are usually off-topic
here, but often vital in tracking down a compiler-specific problem.


i do not know BUT this time, i *copied* the programme from my *post*
here to *emacs* and it worked, i do not how. this is th output:
The only thing I can think of is that your source file originally
contained some funny characters. If you still have the original file,
try "cat -A" or "cat -v" to display any control characters.
[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.
>--
Keith Thompson (The_Other_Keit h) k...@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Don't quote signatures unless you're commenting on them. (You've been
posting here long enough to know that.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 8 '07 #7
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

now i see compiler *did* mention that:

151.c:1: error: expected identifier or '(' before '/' token

[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)

Don't quote signatures unless you're commenting on them. (You've been
posting here long enough to know that.)
sorry.

BTW, that was an accident, happened because i was careless.

Mar 8 '07 #8
"arnuld" <ge*********@gm ail.comwrites:
>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
"//" comments are also allowed in C99, and gcc accepts them as an
extension in certain modes. With the options you used, it doesn't
recognize them.

The lesson: copy-and-paste the entire source file. If you fed it to
the compiler, we need to see it.

[...]
[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)
[...]

Here's your program (with the invalid comments deleted):

#include <stdio.h>

int main() {
int c;

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

return 0;
}

You call getchar() in a loop. The loop, which includes printing the
message "your input: ", will be executed once for each input
character. You typed a total of 4 characters: 'a', newline, 'b',
newline. The "putchar(c) " statement printed the value of each one;
when it prints a newline, it produces a blank line in your output.

I advised you in another thread to read section 12 of the comp.lang.c
FAQ. Please do so before posting more questions.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 8 '07 #9
arnuld wrote:
On Mar 8, 12:17 pm, Keith Thompson <k...@mib.orgwr ote:
It would help if you left in more context (eg. the program...)
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
//
so you *didn't* post your exact code.

i was writing C++ in C
bad idea with a C compiler
now i see compiler *did* mention that:

151.c:1: error: expected identifier or '(' before '/' token
compilers do their best to tell you what the problem is

[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.
well you should be. Your code was similar to this

while ((c = getchar()) != EOF)
printf ("your input %c\n", c);

so it keeps printing the line "your input..." until it gets an EOF.
You typed two characters ('a' and '\n') that weren't EOF, hence
two lines of output. (ok, technically, EOF isn't a character...)

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)
Don't quote signatures unless you're commenting on them. (You've been
posting here long enough to know that.)

sorry.

BTW, that was an accident, happened because i was careless.

--
Nick Keighley

Mar 8 '07 #10

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

Similar topics

12
2178
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
2280
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
4762
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
1809
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
1739
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
1819
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
9589
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
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10048
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
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
8872
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
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3963
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
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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.