You must give printf something to print:Originally Posted by cogeek
Regards.Code:printf("The character is %c\n", c);
Dave
You must give printf something to print:Originally Posted by cogeek
Regards.Code:printf("The character is %c\n", c);
Dave
OK: i writed this:
#include <stdio.h>
main()
{
int c, ch;
printf("Add a character: ");
scanf("%c", &ch);
ch = tolower(ch);
printf("The character is %c\n", ch);
}
but when riting a character i get the same for example:
bash-2.05b$ ./big
Add a character: A
The character is A
The biggest problem is with scanf. It expects a pointer to char, but you gave it a pointer to int. That's not a good thing.
Code:#include <stdio.h> #include <ctype.h> int main(void) { char ch; printf("Add a character: "); scanf("%c", &ch); ch = (char)tolower(ch); printf("The character is %c\n", ch); return 0; }
My best code is written with the delete key.
I din't get what the (char) mean
?
Casts the returned integer down to a char. Both are integral types.
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.
>I din't get what the (char) mean
It means "I really do want a char, I promise that I know what I'm doing". Why do you need to tell the compiler that? Because ch is a char and tolower returns an int. Trying to assign a wider type to a narrower type requires a warning, and the cast silences the warning in situations where you really do know what you're doing. Try removing the (char) cast and see what happens, then put it back and compare the results.
My best code is written with the delete key.
And I can't believe no one's told you this yet, but I will however, in lieu of certain "initiatives", shall we say, be nice about it.
When you post source code as if it was regular text, it's very hard to read. The symbols are hard to read, and there is no indentation. Instead, place [/code] at the end of your source code, and [code] at the beginning, and when you post your message, the site will automatically format your code nicely - there's a good example in Prelude's second-to-last post.
Last edited by sean; 12-12-2004 at 04:10 PM.
Actually no, you wouldn't. You'd get a compiler warning. At least with GCC you will:Originally Posted by sean_mackrory, regarding not #include<ctype.h>
However, it still compiles. And, since no one here pays attention to their warnings anyway...Code:ctyp.c: In function `main': ctyp.c:5: warning: implicit declaration of function `toupper'
Quzah.
Hope is the first step on the road to disappointment.
could your problem be the fact your using a %c in your printf statement and then not assigning the variable associated with it! Rule1 broken perhaps??? *wags finger*Originally Posted by cogeek
also "int main()" is better than just main() as main returns an int.
try that? (ive not yet so i might edit this post yet! .... i AM tired!)Code:#include <stdio.h> int main() { char c; printf("Add a character: "); scanf("%c", &c); toupper(c); printf("The character is %c\n", c); return(0); }
--CHriS
'rm -fr /bin/laden'
'kill all'
right well theres an improvement already!
guyattc2-practical_c3-> ./file
Add a character: c
The character is c
guyattc2-practical_c3->
now you see your other problem is youve not actually assigned anything to the operation "toupper(c)"
so heres a revision of the code:
and heres the output:Code:#include <stdio.h> int main() { char input; char output; printf("Add a character: "); scanf("%c", &input); output = toupper(input); printf("The character is %c\n", output); return(0); }
guyattc2-practical_c3-> gcc -o file file.c
guyattc2-practical_c3-> ./file
Add a character: c
The character is C
guyattc2-practical_c3->
game, set and match! *bows* hehe sorry im in a random m00d today!
--CHriS
'rm -fr /bin/laden'
'kill all'
How many days left are there in that month thing?Originally Posted by goosematt
Quzah.
Hope is the first step on the road to disappointment.
» Roughly, 23 days; 10 hours; 56 minutes [GMT -7] As of right now.Originally Posted by quzah
- Stack Overflow
Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.