What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » C Programming Question

C Programming Question
Author: Message:
John Anderton
Elite Member
*****

Avatar

Posts: 3908
Reputation: 80
37 / Male / Flag
Joined: Nov 2004
Status: Away
O.P. C Programming Question
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 :-/
:blah:

This post was edited on 10-03-2005 at 06:50 PM by John Anderton.
[

KarunAB.com
]

[img]http://gamercards.exophase.com/459422.png[
/img]
10-03-2005 06:48 PM
Profile E-Mail PM Web Find Quote Report
Yousef
Full Member
***

Avatar
(previously known as Juzzi)

Posts: 487
Reputation: 19
35 / Male / Flag
Joined: Jul 2004
RE: C Programming Question
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.
Developer of BuddyFuse: Google Talk, Twitter and Hyves in Windows Live Messenger
Ex-Microsoft intern and Windows Live Developer MVP in 2007 & 2008
10-03-2005 07:03 PM
Profile E-Mail PM Web Find Quote Report
Plik
Veteran Member
*****

Avatar

Posts: 1489
Reputation: 46
34 / Male / –
Joined: Jun 2004
RE: RE: C Programming Question
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++?
10-03-2005 07:06 PM
Profile PM Find Quote Report
John Anderton
Elite Member
*****

Avatar

Posts: 3908
Reputation: 80
37 / Male / Flag
Joined: Nov 2004
Status: Away
O.P. RE: C Programming Question
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 :P
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 :sad:

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 8-)
[

KarunAB.com
]

[img]http://gamercards.exophase.com/459422.png[
/img]
10-03-2005 07:18 PM
Profile E-Mail PM Web Find Quote Report
Plik
Veteran Member
*****

Avatar

Posts: 1489
Reputation: 46
34 / Male / –
Joined: Jun 2004
RE: C Programming Question
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 :P
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 :P (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 :sad:
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 :P

This post was edited on 10-03-2005 at 07:32 PM by Plik.
10-03-2005 07:25 PM
Profile PM Find Quote Report
John Anderton
Elite Member
*****

Avatar

Posts: 3908
Reputation: 80
37 / Male / Flag
Joined: Nov 2004
Status: Away
O.P. RE: C Programming Question
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 8-)

Damn ..... why didnt i write it down :sad:

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 8-) I know i can use the conversion type functions calling "ctype.h" and the using functions like isalpha(a), isupper(a) and/or islower(a) ..... but thats a different approach cause the question says that i should use the ascii values :sad:

EDIT:
quote:
Originally posted by Madman
P.S Dont worry if im wrong i havnt been coding with c for very long either :P

I got that a long time back :P
;) thanks but keep trying .....

This post was edited on 10-03-2005 at 07:36 PM by John Anderton.
[

KarunAB.com
]

[img]http://gamercards.exophase.com/459422.png[
/img]
10-03-2005 07:33 PM
Profile E-Mail PM Web Find Quote Report
J-Thread
Full Member
***

Avatar

Posts: 467
Reputation: 8
– / Male / –
Joined: Jul 2004
RE: C Programming Question
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...
10-03-2005 08:27 PM
Profile E-Mail PM Find Quote Report
segosa
Community's Choice
*****


Posts: 1407
Reputation: 92
Joined: Feb 2003
RE: RE: C Programming Question
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 8-)

Damn ..... why didnt i write it down :sad:



code:
printf("%d",a);

code:
if (m >= 'A' || m <= 'Z')
  printf("It is a capital letter");


This post was edited on 10-04-2005 at 06:10 AM by segosa.
The previous sentence is false. The following sentence is true.
10-04-2005 06:09 AM
Profile PM Find Quote Report
John Anderton
Elite Member
*****

Avatar

Posts: 3908
Reputation: 80
37 / Male / Flag
Joined: Nov 2004
Status: Away
O.P. RE: C Programming Question
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 ;)

:bow: Thanks :)
[

KarunAB.com
]

[img]http://gamercards.exophase.com/459422.png[
/img]
10-04-2005 07:45 AM
Profile E-Mail PM Web Find Quote Report
RaceProUK
Elite Member
*****

Avatar

Posts: 6073
Reputation: 57
39 / Male / Flag
Joined: Oct 2003
RE: C Programming Question
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.

This post was edited on 10-04-2005 at 10:46 AM by RaceProUK.
[Image: spartaafk.png]
10-04-2005 10:46 AM
Profile PM Web Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On