Re: Hi guys~ i wonder that why this simple code error!!



Groovy hepcat problem. was jivin' in comp.lang.c on Wed, 19 Sep 2007
9:45 pm. It's a cool scene! Dig it.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{

int a, b;
float result = 0.0f;
char symbol = '\0';
int loop = 0;

printf("*******************************\n");
printf("This is the Calculator Program\n");
Printf("Please, Enter first Number : \n");
scanf("%f", &a);
printf("Please, Enter second Number : \n");
scanf("%f", &b);
printf("Please, Select the menu (+, -, *, /, % .\n");
scanf("%c", &symbol);
printf("*********************************\n");

do{
if (symbol == '+')
result = a + b;
else if (symbol == '*')
result = a * b;
else if (symbol == '/')
if (b == 0)
{
printf("Error!!, You reinsert second value\n");
loop = 1;
}
result = a/b;
if (symbol == '%')
if (b == 0)
{
printf("Error!!, You reinsert second
value\n");
loop = 1;
}
result = a % b;
}while(loop);
printf("Result : %d %c %d = %f\n", a, symbol, b,
result);
system("PAUSE");
return 0;
}

the error message is printf is undeclared.(first use this function)

That's not what it says at all, I'll bet.

why?

Because your code is broken. Here's what I get:

gcc -ansi -pedantic -Wall -o testing testing.c
testing.c: In function `main':
testing.c:14: warning: implicit declaration of function `Printf'
testing.c:15: warning: float format, different type arg (arg 2)
testing.c:17: warning: float format, different type arg (arg 2)
testing.c:18: warning: unknown conversion type character 0xa in format
testing.c:37: error: missing terminating " character
testing.c:38: error: stray '\' in program
testing.c:38: error: `value' undeclared (first use in this function)
testing.c:38: error: (Each undeclared identifier is reported only once
testing.c:38: error: for each function it appears in.)
testing.c:38: error: syntax error before "n"
testing.c:38: error: missing terminating " character

Other people have told you about your Printf() and scanf() errors, as
indicated in the above diagnostic output, and even one not indicated
there. But they seem to have missed a couple of other problems.
First, you have a printf() format string with an embedded '%'. This
character introduces a conversion specifier. But you have an invalid
conversion specifier composed of a '%' followed by a space. To
represent a literal '%' in a printf() format string, use two '%'
characters. For example:

printf("%%\n");

Next, you have a line wrapped within a string literal. This is causing
the remaining cascade of diagnostic messages.
I must also add that your code is hard to understand. Your indentation
is all over the place, making it difficult to follow. This has also led
to the line wrap problem mentioned above. You should try to format any
code you post here so that lines are no more than about 70 characters
long. Indent consistently. Long string literals can be divided, if need
be, either by using a line concatenation sequence (a backslash at the
end of a line) or (better still) by taking advantage of the fact that
adjacent string literals are automatically concatenated. For example,
this:

printf("A string "
"literal.\n");

is interpreted as this:

printf("A string literal.\n");

--
Dig the sig!

----------- Peter 'Shaggy' Haywood ------------
Ain't I'm a dawg!!
.



Relevant Pages

  • Re: "round" - improvement suggestion
    ... should return a float number for round/100.0, which, due to IEEE floating point representation, may be different from ... by choosing the shortest string of digits that reconverts to the ... % format %.2f 34.258123 ... My sample aims only to show difference between round and format. ...
    (comp.lang.tcl)
  • Re: %n !!
    ... of security problems in C programs are so-called "format string ... if a user-supplied string is passed to a function like ... printf(), the user might include %n specifiers, which would have the ... the only way to log a string uses printf-type format specifiers; ...
    (comp.lang.c)
  • Re: Formatted printing (D5.1 and D6)
    ... such as the printf() family. ... the variadic functions rely on the native stack ... then the C compiler will push a pointer to the format ... string, and 64-bits worth of floating point data onto the stack. ...
    (comp.lang.smalltalk.dolphin)
  • Re: Just a bit of silliness
    ... it depends whether the compiler is confident that format[] ... is an immutable string literal. ... In my code I think 100% of printf calls use a string constant. ... the C language is really very simple in terms of it's ...
    (comp.lang.c)
  • Re: Variadic functions
    ... >for format specifiers, even when there are no additional arguments? ... printf and friends is the format string. ... >putsinstead of printf() for output of a string. ...
    (comp.lang.c)