?
Solved

printf, scanf, characters with logical comparisons

Posted on 2004-11-17
3
Medium Priority
?
840 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:pitmanromford
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 1200 total points
ID: 12603022
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
 
LVL 2

Author Comment

by:pitmanromford
ID: 12607531
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 12612748
>>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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question