![]() |
| | #1 |
| Registered User Join Date: Jun 2009 Location: US of A
Posts: 300
| In the program run below i get the output as follows: Enter the string Hackers have put us on hit list Hackers have put ┼ ─ I had a doubt regarding the output. The program has five scanf statements and i need to pass the string "Hackers have put us on hit list" when it prompts me. Now 1st scanf and printf shoudnt the output be "ckers" instead of Hackers . But in the fourth scanf it should take "us" as the input and 's' should be the output . Similarly for the fifth scanf it takes 'on' as the input but it gives something called as '-' as output. insert Code: #include <stdio.h>
#include <conio.h>
void main(){
printf("Enter the string");
scanf("\n\r%s", &s[2]);
printf("\n\r %s", &s[2]);
scanf("\n\r%s", s);
printf("\n\r%s", s);
scanf("\n%s", &s);
printf("\n%s", &s);
scanf("\n%c", &s[1]);
printf("\n%c", &s[1]);
scanf("\n%c", &s);
printf("\n%c", &s);
}
|
| roaan is offline | |
| | #2 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,259
| This can't possibly be the code you've tried to compile, because it doesn't. Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #3 |
| Registered User Join Date: Jun 2009 Location: US of A
Posts: 300
| This is what compiles on my machine insert Code:
#include <stdio.h>
#include <conio.h>
void main(){
static char s[30];
printf("Enter the string");
scanf("\n\r%s", &s[2]);
printf("\n\rHere %s", &s[2]);
scanf("\n\r%s", s);
printf("\n\r%s", s);
scanf("\n%s", &s);
printf("\n%s", &s);
scanf("\n%c", &s[1]);
printf("\n%c", &s[1]);
scanf("\n%c", &s);
printf("\n%c", &s);
}
Enter the string Hackers have put us on hit list Here Hackers have put ┼ ─Press any key to continue . . . |
| roaan is offline | |
| | #4 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,259
| You should consider reading the FAQ on how to read lines of text. Cprogramming.com FAQ > Get a line of text from the user/keyboard (C) Also, & means 'address of', so unless you're trying to actually display the address of something, you shouldn't be using it in printf. Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #5 |
| DESTINY Join Date: Jul 2008 Location: in front of my computer
Posts: 656
| @roann Don't use void main ever. See the FAQ.
__________________ HOPE YOU UNDERSTAND....... for( ; ; ) printf("If you can't make it good, at least make it look good"); PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D. IDE- Microsoft Visual Studio 2008 Express Edition |
| BEN10 is offline | |
| | #6 |
| gcc -Wall -pedantic *.c Join Date: Jan 2009 Location: London
Posts: 52
| >Also, & means 'address of', so unless you're trying to actually display the address of something, you shouldn't be using it in printf s is an array of chars, so '&(s[N])' in a printf with the '%s' format is correct. |
| flexo87 is offline | |
| | #7 | |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,259
| Quote:
Code: printf("\n%s", &s);
Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? | |
| quzah is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Binary Search Trees Part III | Prelude | A Brief History of Cprogramming.com | 16 | 10-02-2004 03:00 PM |
| << !! Posting Code? Read this First !! >> | kermi3 | Windows Programming | 0 | 10-14-2002 01:29 PM |
| Can you have nested code block? What does the compiler do? For example ... | albertr | C Programming | 4 | 01-16-2002 12:04 AM |
| problems with output from code | simhap | C++ Programming | 0 | 10-08-2001 12:43 PM |