That is to say, "library" is part of the comment "include information about standard library" that is attached to the line #include <stdio.h>
That is to say, "library" is part of the comment "include information about standard library" that is attached to the line #include <stdio.h>
Will someone please tell me if this example will fail so I can learn, or are they true? I get them to fail.
Here is a description:
Most of the work gets done in the body of the loop. The Celsius temperature is computed and
assigned to the variable celsius by the statement
celsius = 5 * (fahr-32) / 9;
The reason for multiplying by 5 and dividing by 9 instead of just multiplying by 5/9 is that in
C, as in many other languages, integer division truncates: any fractional part is discarded.
Since 5 and 9 are integers. 5/9 would be truncated to zero and so all the Celsius temperatures
would be reported as zero.
This example also shows a bit more of how printf works. printf is a general-purpose
output formatting function, which we will describe in detail in Chapter 7. Its first argument is
a string of characters to be printed, with each % indicating where one of the other (second,
third, ...) arguments is to be substituted, and in what form it is to be printed. For instance, %d
specifies an integer argument, so the statement
Quote from the book below:
Code:printf("%d\t%d\n", fahr, celsius);
My quote below:
Code:#include <stdio.h> int main() { printf("%d\t\%d\n", fahr, celsius"); }
You have an extra stray quote sign after celsius. You also seem to be missing the rest of the program (where fahr and celsius are defined, and one is read in, and the other is calculated). This line by itself will not compile.
That's it. I give up with this book.
If someone can't recommend a good book then I give up. Now, back to hello, world. This is exactly what I mean by no int
This is coming from the book.
Code:#include <stdio.h> main() { printf("hello, "); printf("world"); printf("\n"); }
1. As I said, I don't have a copy of the book, but I'm pretty sure I've seen that entire example from the book. It's there in print. But the point is you can't pull out one line of a program and expect it to work without all the rest of the stuff around it. There's not anything any book can do about that.
2. As for main vs. int main, K&R get a pass, since they wrote the language and they didn't require int main. Now since you are using a C++ compiler (for whatever reason only you know) you're getting in trouble, since C++ closed down that loophole and require int main. If you were compiling a .c file, using C settings you'd get a warning about it but not an error.
Visual Studio's compiler has a C compatibility switch you can flip, /Tc (somewhere I forget where). You should be getting that automatically if your file ends with .c instead of .cpp. (The easy way to check is to get into Windows and browse for your file -- if the icon says C++, then the extension is wrong. If you chose "New File" from inside Visual Studio, then you're probably sunk -- at best you might have a file like helloworld.c.cpp, which doesn't quite count.)
Of course you have a file -- where do you think all that stuff that you type goes? It's probably hidden a bit, something like My Documents\Visual Studio\Projects\C Programming\this\that\theother\and\so\on. If you follow it all the way down, I wouldn't be surprised at all that you have "C Programming.c.cpp" as your filename.
But no, if you choose File->New you won't get a C source file out of Visual Studio. If you're good with command line, you can rename the file, or do what I do when I have to compile a C file in Visual Studio which is roughly this:
- Cry a little bit
- Create a new file in notepad like "whatever.c" and put a few lines in it
- Go to Visual Studio, and choose create empty project
- Right click on the project name, choose add existing item
- Find your whatever.c and select it
I'm doing this from memory so the option names might be off a little bit.
Would C The Complete Reference would be a good book for someone that never programmed before?
You can write your source in notepad, yes, or any editor (emacs, vim, notepad++, or etc.).
If you want to get retro, you can then take that file, go to your start menu and choose "Visual Studio <year> Command Prompt" (it's in your visual studio folder thing), navigate to where you saved your file, and type "cl filename.c" and hey presto.
I assume you're talking about Schildt's book. I don't own it, haven't read the whole thing, just standing in the bookstore. I can't recommend it.
There's a whole thread about good books stickied at the top of the forum.
But the thing is that if you write good, conforming code, it will work just as well under C++ as C. I you don't need to work for embedded systems & all that (just learning still), then you can avoid all the loopholes of C by compiling it as C++.
For example, it disallowed the use of implicit main in the book. If you'd written code with a C compiler instead, it would compile, but you would be told by the members of this board to correct it.
And VC++ does not warn about implicit main from what I can remember.
Just a note, though. Don't use notepad.
Yes, you can use any editor, but I would still recommend Visual Studio. It's a good IDE.
I don't know if you can trust that. VC++'s compiler is known not to be too friendly on the command line. Better throroughly study the documentation first.If you want to get retro, you can then take that file, go to your start menu and choose "Visual Studio <year> Command Prompt" (it's in your visual studio folder thing), navigate to where you saved your file, and type "cl filename.c" and hey presto.
So I'll continue to recommend that you compile under the IDE.