• C

printf, scanf, characters with logical comparisons

ok, this is infact my second day of learning C, hence i'm very very new.  I have made the following code so far:
//-----------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
int main() {
int *p;
char myChar;
p = (int *)malloc(sizeof(int));
printf("Enter in a value to be stored in a block of memory: ");
scanf("%d",p);
printf("The value %d is stored in address %d\n",*p,p);
printf("Do you want to look at more memory integers? (Y/N) ");
scanf("%s",&myChar);
printf("You Said: %c\n",myChar);//Works
//if(myChar=="y"){printf("Confirmed, you typed y!");}//Doesn't Work
free(p);
}
//-----------------------------------------------------------------------------

I supose i have 3 small questions on this:

1)  I've used in scanf %s and in printf %c, is this correct?  (seems to work - ish)
2)  Why doesnt the comented out code work (when it's not commented of course)
3)  When you input an integer into p (line 9), the code runs as expected, however if you input a character into there, it was to my belief that it would convert it into ASCII and leave it as an integer, well it seems to do that, but it also skips the second scanf function and places that character into the variable myChar, why?

Thanks in Advance
Ask for clarificaiton if required :)
Lee
LVL 2
pitmanromfordAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PaulCaswellCommented:
Lee,

>>1)  I've used in scanf %s and in printf %c, is this correct?  (seems to work - ish)
This is essentially correct but the scanf will read ALL the characters that the user enters and add a '\0' on the end to make it into a string. You are telling it to put the result into a ONE character memory location (char myChar).
I'd suggest you use 'char userEntry[80]' or something like that.
This is what is commonly termed a 'buffer overflow' bug and is very common.

>>2)  Why doesnt the comented out code work (when it's not commented of course)
Because 'myChar' is a character and "y" is a string (an array of charactyers terminated by a zero code). To get a particular character out of a string use 'string[0]' for example. To compare strings you have to call a library function such as 'strcmp'.

>>3)  When you input an integer into p (line 9), the code runs as expected, however if you input a character into there, it was to my belief that it would convert it into ASCII and leave it as an integer, well it seems to do that, but it also skips the second scanf function and places that character into the variable myChar, why?
Because 'scanf("%d") will read all digits in the user input (none in this case). The remaining characters will then be presented to the NEXT scanf.

NOTE:
printf("The value %d is stored in address %d\n",*p,p);
Only works because an integer is the same size as a pointer. Most implementations allow for %p for pointers.

Any more questions?


Paul
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pitmanromfordAuthor Commented:
Hi paul, sorry for the late reply, had to go out just after I posted the quesiton.

I originaly learnt VB, then actionscript (flash), and now i've moved swiftly onto C.

In VB you could handle errors rather easily, in actionscript errors never crashed the program, it would just skip out the lines of code.  However in C it appears that there's no way to trap an error, if an error occurs your entire applicaiton crashes, and one would think that you have to anticipate every kind of error, such as dividing by zero, or the user typing in too many characters etc...

How would you avoid the problem of 'buffer overflow'?

Cheers
Lee
0
PaulCaswellCommented:
>>How would you avoid the problem of 'buffer overflow'?
In C, you have to use functions that can be passed limits.

Sadly, the ANSI standard was developed before buffer-overflow issues were considered important. Microsoft have tried with their extensions to the ANSI standards. They took the concept of a count-limited version of strcpy being called strncpy and produced snscanf etc. Sadly, this didnt catch on as the changes werent terribly well documented or consistent.

Generally speaking, you have to write your own.

Paul
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.