
C Programming Question - Printable Version
-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: C Programming Question (/showthread.php?tid=51334)
C Programming Question by John Anderton on 10-03-2005 at 06:48 PM
I have made a few programs viz the pascals triange (it took me 10 mins to figure out how to print out the numbers in a perfect triangle) and fibonnici series (easy 1 1 2 3 5 8 13 21 34 55)
But i still dont know how i can print the asci value of a number .....
Question 1: Given a character variable, how can you print its ascii value
Question 2: Taking the input from the keyboard convert it (digit or an alphabet) into the corresponding alphabet or digit (respectively) by using the ascii chart.
So far i have done so much ..... really nothing much 
TurboC doesnt allow pasting here ....
* John Anderton opens file in notepad ....
code: /* Header Files */
#include <stdio.h>
#include <conio.h>
/* Global Variables */
char a;
void main ()
{
/* Clean Slate */
clrscr();
/* Input */
printf("Enter a alphanumerical character (ie Alphabet or a digit ");
a = getchar();
putchar('\n');
/* If loop to identify datatype */
if (isalpha(a) > 1)
printf("%c",a);
/* Correction reqd there ^^ */
else if (isdigit(a) > 1)
printf("%c",a);
/* And there too ^^ */
else
printf("Only alpha numericals alowed. %c is now allowed",a);
/* Applying glue on the output screen */
getch();
}
Someone please help quick 
And dz why does the [code] tag parse emotes 
[/code]
RE: C Programming Question by Yousef on 10-03-2005 at 07:03 PM
quote: Question 1: Given a character variable, how can you print its ascii value
__toascii(integer)
quote: Question 2: Taking the input from the keyboard convert it (digit or an alphabet) into the corresponding alphabet or digit (respectively) by using the ascii chart.
Char character = integer should work I think.
RE: RE: C Programming Question by Plik on 10-03-2005 at 07:06 PM
quote: Originally posted by Juzzi
quote: Question 1: Given a character variable, how can you print its ascii value
can you just typecast a char to an int to get the ascii value, or is that just in C++?
RE: C Programming Question by John Anderton on 10-03-2005 at 07:18 PM
quote: Originally posted by Madman
can you just typecast a char to an int to get the ascii value, or is that just in C++?
Erm .... im still a c newb if you didnt realise that from my coding style 
Yeah i know typecasting and all ..... but i dont think you can do it like that .....
Its something like
code: print("%something here",some special method of calling the variable);
usually we say printf("%c",a); but here it should be something like printf("%c",'a'); but that doesnt work ..... and i cant find it in any book 
quote: Originally posted by Juzzi
__toascii(integer)
Since i havent learned that .... teacher will be suspicious .... still there must be a header file reqd ... what is it .... and whats the syntax .... ill find it in my book or on google i guess
RE: C Programming Question by Plik on 10-03-2005 at 07:25 PM
quote: Originally posted by John Anderton
quote: Originally posted by Madman
can you just typecast a char to an int to get the ascii value, or is that just in C++?
Erm .... im still a c newb if you didnt realise that from my coding style 
Yeah i know typecasting and all ..... but i dont think you can do it like that .....
Well you can do it just using
code: int nAscii = (int)cChar;//cChar beeing a char variable
in C++ but i dont know if you can in C, give it a shot and find out (whats the worst that can happen )
quote: Originally posted by John Anderton
code: print("%something here",some special method of calling the variable);
usually we say printf("%c",a); but here it should be something like printf("%c",'a'); but that doesnt work ..... and i cant find it in any book 
To output a interger as the number value using printf you'd use.
code: printf("%d", anInterger);
P.S Dont worry if im wrong i havnt been coding with c for very long either
RE: C Programming Question by John Anderton on 10-03-2005 at 07:33 PM
quote: Originally posted by Madman
To output a interger as the number value using printf you'd use.
What i meant is that i heard that there is a way to do that whole thing in one printf statement 
Damn ..... why didnt i write it down 
Neways ill try that nAscii idea of yours tho im not that hopeful .... if anyone knows about it then help me .....
I got an idea like if i tell the compiler that a variable ( m ) .....
code: if (m >= 'A' || m >= 'Z')
printf("It is a capital letter");
Will that work I know i can use the conversion type functions calling "ctype.h" and the using functions like isalpha , isupper and/or islower ..... but thats a different approach cause the question says that i should use the ascii values 
EDIT:
quote: Originally posted by Madman
P.S Dont worry if im wrong i havnt been coding with c for very long either 
I got that a long time back 
thanks but keep trying .....
RE: C Programming Question by J-Thread on 10-03-2005 at 08:27 PM
How about
code: int num = (int)'A'
So if m is a char:
code: if ((int)m >= (int)'A' || (int)m >= (int)'Z')
printf("It is a capital letter");
I don't do C++ but I know a lot of programming languages and this is a method that is sometimes used...
RE: RE: C Programming Question by segosa on 10-04-2005 at 06:09 AM
quote: Originally posted by John Anderton
What i meant is that i heard that there is a way to do that whole thing in one printf statement 
Damn ..... why didnt i write it down 
code: printf("%d",a);
code: if (m >= 'A' || m <= 'Z')
printf("It is a capital letter");
RE: C Programming Question by John Anderton on 10-04-2005 at 07:45 AM
quote: Originally posted by segosa
code: printf("%d",a);
code: if (m >= 'A' || m <= 'Z')
printf("It is a capital letter");
Yeah now why didnt i think of that .... thanks seg .... i think thats it 
Thanks
RE: C Programming Question by RaceProUK on 10-04-2005 at 10:46 AM
A quick explanation of why it works:
In C, as in many programming languages, the char datatype is basically a number. Therefore, integer comparisons are possible, even without typecasts, and using %d in a printf is also perfectly valid.
As an example, take getchar(). It's signature is
code: int getchar(void);
It gets a character from the keyboard (stdin), and returns it as unsigned. This leaves the negative values of int free for error codes.
Put simply:
0-255 is a valid character
<0 is an error.
Edit: Of course, once you've checked it's positive, you will need to typecast into a char. char is typically 8 bits, int 16/32 bits.
|