Whew!
Whew!
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Issue solved:
To avoid this thread going down a road that might be more practice than solution, I suppose I had better just post the working code and say we are good. Apparently Windows XP is not a "normal operating system" and requires the stdout to be flushed. MK27 is right that this has become more OS that C syntax though I feel it is very helpful to know little tricks like setvbuf(stdout,NULL,_IONBF,0)... Just in case.
Code:#include <stdio.h> int main(void) { setvbuf(stdout,NULL,_IONBF,0); int n = 0; int i = 0; int left = 1; int right = 1; int temp = 1; printf("Enter an index: "); scanf("%d", &n); printf("Index: %d", n); if (n < 0 || n == 0) { printf("\nFibonacci index: %d\n", -1); } if (n == 1) { printf("\nFibonacci index: %d\n", 1); } for (i = 0; i < (n - 1); i++) { temp = right; right = left + temp; left = temp; } printf("\nFibonacci index: %d\n", left); return 0; }
Well, that's why I said it sounds strange to me. I mean, it would suck to be starting out and get derailed by some weird problem that isn't your fault.
So, just to get this straight, you are saying you get a blinking cursor waiting for the scanf input before "Enter an index:" appears?
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
yep.
Well, not anymore because I am using the command you gave me.
Wow. What if you put a newline in there:
Which I know you don't want to do that in this case, but it could be your buffer isn't flushed until a newline (or the buffer is full) which wouldn't be unusual.Code:printf("Enter an index: \n");
It really is about the OS, by the way. But if you are used to Java, the Java VM/interpreter probably includes a mechanism to make it's own behavior uniform, taking the OS's behaviour into account. As you point out, C is not so fancy.
Last edited by MK27; 01-28-2010 at 01:26 PM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
That (the underlined part) is an excellent reason to do it.
And what about OS abstraction? Or debug output?
I've seen plenty of projects doing it. Can't remember any specific ones though.Go look at some C source. NOBODY does this. NOBODY. You can be the first exception, but that should give you reason to stop and pause. You are confused about something, or something else is very wrong.
Well, fair enough, I just wanted to make a strong point here, vis, it would considered extremely strange for a beginner to have a need to do that. Like, if you read a C textbook, they are going to use printf() start to finish. If you can find even one book where in chapter 2, "input/output" they say:
...I'll eat my hat. That is more of an assembly language type project. We're not that low level.First, C does not provide any standard, portable functions for console output. In this chapter, we'll learn how to write one.
It's not a bad exercise I guess, but probably brightmatter already understands stuff like datatypes, strings, bytes, how characters are represented, etc.
Last edited by MK27; 01-28-2010 at 01:37 PM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Well, I had tried this...
...and as you can see there is a "\n" on the end. It still would not print until it was at the return for main.Code:char ca[] = {'E', 'n', 't', 'e', 'r', ' ', 'a', 'n', ' ', 'i', 'n', 'd', 'e', 'x', ':', ' ', '\n'}; for(i = 0; i < sizeof(ca); i++) { putchar(ca[i]); }
...I tried this with a string "dhfdxg\n" and it does the same thing. If it does not fflush or setvbuf(stdout,NULL,_IONBF,0) then it not only doesn't print but it corrupts my variable 'n' during the scanf.
Nb,
is exactly the same as:Code:char ca[] = {'E', 'n', 't', 'e', 'r', ' ', 'a', 'n', ' ', 'i', 'n', 'd', 'e', 'x', ':', ' ', '\n'};
You may want to get into the habit of using "strlen()" with strings instead of "sizeof", because if you apply sizeof() to a char pointer, you'll get sizeof(char*) and not the length of the string pointed to.Code:char ca[] = "Enter an index: \n";
Stuff like:
confuses this slightly, since literal is considered an immutable string literal, so sizeof will work there.Code:char *literal="hello world";
But strlen() works with all of them, presuming there is a valid string involved.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
duly noted about card arrays.
Are you sure that strlen() does not require any non-standard libraries?
No, <string.h> is a standard library.
Also, sizeof will lie about the length of strings like
Code:char s[100] = "Hello";
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
strlen should be in string.h
Edit:
what the hell? Before I posted this then bright's question about strlen() was the last reply and now when I click submit 2 more replies pop up.. forum bug?
Last edited by _Mike; 01-28-2010 at 04:02 PM.
It was my impression that sizeof didn't lie so much as it just includes the null terminator at the end of a C-style char array
char s[100] = "Hello";
sizeof(s) == 100
strlen(s) == 5
char* s = "This is a long string!";
sizeof(s) == 4
strlen(s) == 22
Using sizeof for string length is not reliable..