Re: program bug



On May 2, 6:16 pm, "Bill Cunningham" <nos...@xxxxxxxxx> wrote:
"Robert Gamble" <rgambl...@xxxxxxxxx> wrote in message

news:2d73e006-fc0d-4ed7-ae1e-43535dea6140@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx

a is not initialized, referencing its value invokes undefined
behavior.

a=fgetc(ifp);
fputc(a,ofp);

In the beginning a was declared. Is it not enough in this case to simply
declare an int? Or should I have done this int a=0; at the beginning of the
program?

You need to store a value into a variable before you use the value of
that variable, either through initialization (implicit or explicit) or
assignment. If you don't do this then the variable may contain
garbage ("indeterminate value" in standards parlance), that garbage
may be a trap value which will invoke UB when read. Note that the
alternative I provided does not have this problem because "a" is
assigned before its value is used.

--
Robert Gamble
.



Relevant Pages

  • Re: which is better... SET or SELECT?
    ... DECLARE @p int ... for each row of in the table #tmp. ... > One important difference between SET and SELECT for variable assignment is> the behaviour when no rows are returned from the SELECT statement. ... > DECLARE @id INTEGER ...
    (microsoft.public.sqlserver.programming)
  • Re: Really tough ADO Stored Procedure Question. Please Help!!!
    ... @lScenarioID_CopyFrom int, ... DECLARE @ErrMSG varchar--This is the max msg size ... ROLLBACK TRANSACTION ... SELECT @lRowCountHolder = MIN ...
    (microsoft.public.sqlserver.odbc)
  • Re: Really tough ADO Stored Procedure Question. Please Help!!!
    ... @lScenarioID_CopyFrom int, ... DECLARE @ErrMSG varchar--This is the max msg size ... ROLLBACK TRANSACTION ... SELECT @lRowCountHolder = MIN ...
    (microsoft.public.sqlserver.programming)
  • Re: Really tough ADO Stored Procedure Question. Please Help!!!
    ... @lScenarioID_CopyFrom int, ... DECLARE @ErrMSG varchar--This is the max msg size ... ROLLBACK TRANSACTION ... SELECT @lRowCountHolder = MIN ...
    (microsoft.public.sqlserver.server)
  • Re: Maths Formula Question
    ... And some times when copy and past happens Duplicate InstructionOrder numbers ... [ScenarioID] ... DECLARE @lScenarioID int ...
    (microsoft.public.sqlserver.programming)