473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Two questions about a program in C

Hi there! I have two simple questions about a program i'm doing:
1) if I write:
enum logic {VERO=1, FALSO=0};
logic trovato=VERO;

Is this correct according to the ANSI '89 standard?

2) I have these lines:
void GetInfo(void)
{
int i=0;
char Dato[256];
printf("Inserire i dati numero %i: ", ++i);
scanf("%[^\n]", Dato);
fflush(stdin);

while( strcmp(Dato, "FINE") )
{
Riempi_Info(Dato); //Fills an array called Estremi
printf("Sigla: %s \t Inizio: %f \tFine: %f\n", Sigle[Nstr],
Estremi[Nstr][0], Estremi[Nstr][1] );
i++;
Nstr++;
printf("Inserire i dati numero %i: ", i);
scanf("%[^\n]", Dato);
fflush(stdin);
}
return;
}

Why does the program crash if I don't call the function fflush after
each scanf? And why in windows can't I use the function fpurge instead?
Feb 8 '08 #1
15 1706
On Feb 8, 1:31 pm, Eoghan <lucaga...@gmail.comwrote:
Hi there! I have two simple questions about a program i'm doing:
1) if I write:
enum logic {VERO=1, FALSO=0};
logic trovato=VERO;

Is this correct according to the ANSI '89 standard?
No. This isn't C++. You need to either add

typedef enum logic logic;

before the definition, or change the definition to

enum logic trovato=VERO;
>
2) I have these lines:
[...]
scanf("%[^\n]", Dato);
fflush(stdin);
[...]
>
Why does the program crash if I don't call the function fflush after
each scanf? And why in windows can't I use the function fpurge instead?
Don't do that. http://c-faq.com/stdio/stdinflush.html

Regards,

-=Dave
Feb 8 '08 #2
In article <ed**********************************@i29g2000prf. googlegroups.com>,
Eoghan <lu*******@gmail.comwrote:
>2) I have these lines:
void GetInfo(void)
{
int i=0;
char Dato[256];
printf("Inserire i dati numero %i: ", ++i);
scanf("%[^\n]", Dato);
Output is not certain to appear before the next input
(especially when the output does not end in '\n') unless
the output stream is fflush()'d.

You did not put a size constraint on the scanf(), so if the
user enters more characters than you have alloted (255) then
your program will have undefined behaviour.
> fflush(stdin);
fflush() is only defined for output and update streams.

--
"I was very young in those days, but I was also rather dim."
-- Christopher Priest
Feb 8 '08 #3
Eoghan wrote:
Hi there! I have two simple questions about a program i'm doing:
1) if I write:
enum logic {VERO=1, FALSO=0};
logic trovato=VERO;

Is this correct according to the ANSI '89 standard?
No. Change `logic' to `enum logic' and it will be correct.
2) I have these lines:
void GetInfo(void)
{
int i=0;
char Dato[256];
printf("Inserire i dati numero %i: ", ++i);
scanf("%[^\n]", Dato);
fflush(stdin);

while( strcmp(Dato, "FINE") )
{
Riempi_Info(Dato); //Fills an array called Estremi
printf("Sigla: %s \t Inizio: %f \tFine: %f\n", Sigle[Nstr],
Estremi[Nstr][0], Estremi[Nstr][1] );
i++;
Nstr++;
printf("Inserire i dati numero %i: ", i);
scanf("%[^\n]", Dato);
fflush(stdin);
}
return;
}

Why does the program crash if I don't call the function fflush after
each scanf?
Probably because of something in Riempi_Info, or in the
declarations of Estremi, Sigle, and Nstr. Unfortunately, you
have not shown any of these, so we cannot tell.

fflush(stdin) invokes undefined behavior. Perhaps the
error of fflush(stdin) somehow cancels out the other error(s).
And why in windows can't I use the function fpurge instead?
There is no fpurge() function in the Standard C library.
If it's a Windows thing, ask about it in a Windows forum.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Feb 8 '08 #4
Eoghan <lu*******@gmail.comwrites:
Hi there! I have two simple questions about a program i'm doing:
1) if I write:
enum logic {VERO=1, FALSO=0};
logic trovato=VERO;

Is this correct according to the ANSI '89 standard?
No, you need to say: 'enum logic trovato = VERO;' and if you are not
getting a diagnostic from your version you should review what compiler
options you are using.
>
2) I have these lines:
void GetInfo(void)
{
int i=0;
char Dato[256];
printf("Inserire i dati numero %i: ", ++i);
scanf("%[^\n]", Dato);
fflush(stdin);
You simply are not allowed to do this in a portable program. Your
system is allowed to define some meaning for this code, but it means
nothing in standard C. fflush is for output streams.

To replace this, write a look that skips to EOF or newline (as a
function, of course!). Maybe:

int c;
do {
c = getc(stdin);
} while (c != EOF && c != '\n');
>
while( strcmp(Dato, "FINE") )
{
Riempi_Info(Dato); //Fills an array called Estremi
printf("Sigla: %s \t Inizio: %f \tFine: %f\n", Sigle[Nstr],
Estremi[Nstr][0], Estremi[Nstr][1] );
i++;
Nstr++;
printf("Inserire i dati numero %i: ", i);
scanf("%[^\n]", Dato);
fflush(stdin);
}
return;
}

Why does the program crash if I don't call the function fflush after
each scanf?
My guess is that problem is be caused by other parts of your program
(not shown) doing something wrong when the scanf reads an empty
string.
And why in windows can't I use the function fpurge instead?
Why would you want to? Stick to standard C and your program will run
almost anywhere.

--
Ben.
Feb 8 '08 #5
On the web fpurge is said to be included in stdio.h

I'm sure the string has less than 256 characters, however here's the
code of RiempiInfo:
void Riempi_Info(char* str)
{
int k=0;

while(str[k]!=' ' && str[k]!='\0') Sigle[Nstr][k]=str[k++];

str=strchr(str, (int)' ');
Estremi[Nstr][0]=atof(&str[1]);

/*Parto da str[1] perche' str[0] e' gia uno spazio*/
str=strchr(&str[1], (int)' ');
Estremi[Nstr][1]=atof(&str[1]);
return;
}

where the variables are:
char Sigle[256][10]; (I'm way sure Nstr is less than 256)
float Estremi[100][2];

Feb 8 '08 #6
If I don't use fflush the program behaves liek this:

Inserire i dati numero 1:

I write:One 0.0 0.3 and I press return

Then the programs writes:

Sigla: ewf Inizio: 0.000000 Fine: 0.300000
Inserire i dati numero 2: Sigla: ewf Inizio: 0.000000 Fine:
0.300000
Inserire i dati numero 3: Sigla: ewf Inizio: 0.000000 Fine:
0.300000
Inserire i dati numero 4: Sigla: ewf Inizio: 0.000000 Fine:
0.300000
Inserire i dati numero 5: Sigla: ewf Inizio: 0.000000 Fine:
0.300000
Inserire i dati numero 6: Sigla: ewf Inizio: 0.000000 Fine:
0.300000
Inserire i dati numero 7: Sigla: ewf Inizio: 0.000000 Fine:
0.300000
Inserire i dati numero 8: Sigla: ewf Inizio: 0.000000 Fine:
0.300000
Inserire i dati numero 9: Sigla: ewf Inizio: 0.000000 Fine:
0.300000
.....
and so on

Feb 8 '08 #7
Eoghan wrote:
On the web fpurge is said to be included in stdio.h
It is not a standard C function. Whether or not some implementation
includes a declaration in a standard header doesn't change that.

Brian
Feb 8 '08 #8
Eoghan wrote:
If I don't use fflush the program behaves liek this:
You're probably leaving newlines in the input buffer. See the FAQs:

<http://c-faq.com/stdio/stdinflush.html>
<http://c-faq.com/stdio/stdinflush2.html>
You can't portably use fflush() on an input stream.


Brian
Feb 8 '08 #9
Well, as some of you said, I left my computer for 5 minutes, I
executed the program again and fflush didn't work anymore!

But I think I solved the problem. Because I used this version of
scanf: scanf("%[^\n]", Dato), the \n character was never read. So
using the function proposed by Ben Bacarisse, everything seems to be
allright.
Do you think that this way to use scanf could have been the problem?

Anyway, abaout the use of enum logic:
I buill the source with VC6 and XCode but I didn't have problems. I
had problems with and older version of XCode. However, you have said
that the enumerations are included in ansi '89 standard of C++, but
also in the ansi '89 of C?
Feb 8 '08 #10
In article <05**********************************@e25g2000prg. googlegroups.com>,
Eoghan <lu*******@gmail.comwrote:
>However, you have said
that the enumerations are included in ansi '89 standard of C++, but
also in the ansi '89 of C?
No, the first C++ standard was 1998, not 1989.

The 1989 ANSI C standard describes enumerations in sections
3.1.2.5, 3.5.2.2, and 3.5.2.3.
--
"There are some ideas so wrong that only a very intelligent person
could believe in them." -- George Orwell
Feb 8 '08 #11
Eoghan <lu*******@gmail.comwrites:
But I think I solved the problem. Because I used this version of
scanf: scanf("%[^\n]", Dato), the \n character was never read. So
using the function proposed by Ben Bacarisse, everything seems to be
allright.
Do you think that this way to use scanf could have been the problem?
Yes. I think about three (maybe more!) people have posted remarks to
say that not reading past the newline character is the root of the
problem.
Anyway, abaout the use of enum logic:
I buill the source with VC6 and XCode but I didn't have problems. I
had problems with and older version of XCode. However, you have said
that the enumerations are included in ansi '89 standard of C++, but
also in the ansi '89 of C?
You have to decide if you want C or C++ code. All versions of both
language standards have enumerations, but in C++ you can omit the
"enum" keyword because the enumeration tag acts as a type name. In C
it does not (and that applies to all versions of the language
standard).

--
Ben.
Feb 8 '08 #12
Eoghan wrote:
>
Hi there! I have two simple questions about a program i'm doing:
1) if I write:
enum logic {VERO=1, FALSO=0};
Is this correct according to the ANSI '89 standard?
That line is correct, but I think that
any of these ways to write it, would be better:

enum logic {FALSO = 0, VERO};

enum logic {FALSO, VERO};

enum logic {FALSO = 0, VERO = 1};

--
pete
Feb 9 '08 #13
On Fri, 8 Feb 2008 13:56:50 -0800 (PST), Eoghan <lu*******@gmail.com>
wrote in comp.lang.c:
On the web fpurge is said to be included in stdio.h
Said by whom? What compiler, for what platform, are they talking
about?

If there is not an option to disable its inclusion in <stdio.h>, then
the implementation is not conforming.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Feb 9 '08 #14
pete wrote:
Eoghan wrote:
>Hi there! I have two simple questions about a program i'm doing:
1) if I write:
enum logic {VERO=1, FALSO=0};
>Is this correct according to the ANSI '89 standard?

That line is correct, but I think that
any of these ways to write it, would be better:

enum logic {FALSO = 0, VERO};

enum logic {FALSO, VERO};

enum logic {FALSO = 0, VERO = 1};
... but don't make this mistake, which a colleague
of mine once uncovered in somebody's actual code:

typedef enum { TRUE, FALSE } Boolean;

--
Eric Sosman
es*****@ieee-dot-org.invalid
Feb 9 '08 #15
Ok, Thanks to everybody!
Feb 9 '08 #16

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

Similar topics

3
by: bill ramsay | last post by:
Hello just found the wonderful world of python in the last two weeks and I think that it is great. I have a couple of questions for those who are wiser a la python than I am. Background:
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
9
by: questions? | last post by:
if I use malloc() in a function to allocate a space for my array of structures. I didn't free() them anywhere in the program. I found that I can still use that space after I come back to...
0
by: Michael.Suarez | last post by:
So we develop and maintain several applications used by several people in the same company, on the same intranet. There are several applications written in VB6, but going forward all of the new...
13
by: M.Siler | last post by:
Let me clarify from my last post. I am not using these 4 questions as the sole screening method. Currently in, the Tampa Bay area (Florida) there is an extreme shortage of C# developers. We have...
13
by: blangela | last post by:
I have a former student who came to me and asked if I had any idea the types of C++ questions that EA asks potential new hires to evaluate their C++ knowledge/skills. I do not, but I thought...
23
by: TefJlives | last post by:
Hi all, I'm learning a bit about C, and I have a few questions. I'm not trying to insult C or anything with these questions, they're just honestly things I don't get. It seems like pointers...
4
by: Viviana Vc | last post by:
Hi all, I've read the WindowsVistaUACDevReqs.doc documentation and I have done different small tests on Vista to understand the bahaviour and now I have a few questions. 1) If I create a...
30
by: GeorgeRXZ | last post by:
Hi Friends, I have some questions related to C Language. 1What is the difference between the standard C language and Non standard C language ? 2which is better C Lanugage, C under Linux/...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...
0
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...
0
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,...

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.