Hi all,
I'm working through a book on electrical principles, transcoding
the end of chapter BASIC programs into C. I'm using the following version of gcc and Ubuntu.
$ gcc --version
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
With my latest small program, gcc is generating the following error
and I really don't see what the problem is. I should state here
that I am more used to C89 than C99 and so I wonder if the formatting of my code in the final for() loop is not suitable for the latter.
First, here are the contents of my makefile.
Code:
P=sect_13_9
OBJECTS=
CFLAGS= -g -Wall
LDLIBS= -lm
CC=c99
$(P): $(OBJECTS)
I should state that this has worked for several other programs with
only a change in the name of the program in the first line.
Now here are the contents of my source file.
Code:
/* sect_13_9.c */
#include <stdio.h>
#include <stdlib.h> // For system().
#include <math.h> // For pow().
int main(void)
{
char cr = 'a'; // Remove <cr> from input stream/buffer. 'a' is dummy value.
float Vdc = 0.0f; // User specified value of DC voltage source.
float capacitance = 0.0f; // User specified value of capacitance.
float resistance = 0.0f; // User specified value of resistance.
float timeIntervals = 0.0f; // User specified number of time intervals.
system("clear"); // Clear the screen.
// Tell the user what this program does.
printf("This program computes and tabulates the instantaneous\n");
printf("capacitor voltage and percent of full charge at\n");
printf("number of specified time intervals during charging\n");
printf("in a specified RC circuit.\n\n");
printf("Enter the Dc source voltage, Resistance in Ohms,\n");
printf("Capacitance in Farads, and desired number of time intervals.\n");
// Generate delay to allow user to read previous text.
for (int t = 0; t <= 4000; ++t)
; // NULL statement.
system("clear");
// Gather input from user.
printf("DC source voltage : ");
scanf("%f%c", &Vdc, &cr);
printf("Capacitance in Farads : ");
scanf("%f%c", &capacitance, &cr);
printf("Resistance on Ohms : ");
scanf("%f%c", &resistance, &cr);
printf("Number of time intervals, tau : ");
scanf("%f%c", &timeIntervals, &cr);
system("clear");
// Display column headings.
printf("Time (sec\tCapacitor Voltage\t%% of Full Charge\n");
// Perform calculations and display results.
for (float time = 0.0f; time <= (5 * resistance * capacitance); time = (5 * resistance * capacitance) / timeIntervals);
{
float exponent = time / (resistance * capacitance); // Line 50. This comment does not occur in source file.
float v = Vdc * (1 - pow(2.718, -exponent));
float pfc = (v / Vdc) * 100;
printf("%fs\t%fV\t%f%%", time, v, pfc);
}
// Exit program.
return 0;
}
Now here is the error generated by gcc.
$ make
c99 -g -Wall sect_13_9.c -lm -o sect_13_9
sect_13_9.c: In function ‘main’:
sect_13_9.c:50:22: error: ‘time’ undeclared (first use in this function)
float exponent = time / (resistance * capacitance);
^
sect_13_9.c:50:22: note: each undeclared identifier is reported only once for each function it appears in
make: *** [sect_13_9] Error 1
As the variable "time" is declared and initialised in the first
clause of the surrounding for() loop, I really don't see why this
error is being generated. Could someone please explain what I
clearly don't understand.
Thanks in advance,
Best regards,
Stuart