You must give printf something to print:Quote:
Originally Posted by cogeek
Regards.Code:printf("The character is %c\n", c);
Dave
Printable View
You must give printf something to print:Quote:
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;
}
I din't get what the (char) mean
?
Casts the returned integer down to a char. Both are integral types.
Look in your book for casting
>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.
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.
Actually no, you wouldn't. You'd get a compiler warning. At least with GCC you will:Quote:
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.
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*Quote:
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
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! :D
--CHriS
How many days left are there in that month thing?Quote:
Originally Posted by goosematt
Quzah.
» Roughly, 23 days; 10 hours; 56 minutes [GMT -7] As of right now. :cool:Quote:
Originally Posted by quzah
- Stack Overflow