C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-02-2009, 08:57 PM   #1
Registered User
 
Join Date: Jun 2009
Location: US of A
Posts: 300
Question code output...

Hi,

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   Reply With Quote
Old 07-02-2009, 09:00 PM   #2
+++ OK NO CARRIER
 
quzah's Avatar
 
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   Reply With Quote
Old 07-02-2009, 09:03 PM   #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);
	
}
Output is this:
Enter the string
Hackers have put us on hit list

Here Hackers
have
put

─Press any key to continue . . .
roaan is offline   Reply With Quote
Old 07-02-2009, 09:15 PM   #4
+++ OK NO CARRIER
 
quzah's Avatar
 
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   Reply With Quote
Old 07-02-2009, 10:02 PM   #5
DESTINY
 
BEN10's Avatar
 
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   Reply With Quote
Old 07-03-2009, 12:49 AM   #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   Reply With Quote
Old 07-03-2009, 02:22 AM   #7
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,259
Quote:
Originally Posted by flexo87 View Post
s is an array of chars, so '&(s[N])' in a printf with the '%s' format is correct.
Yes, but we see it first as...
Code:
printf("\n%s", &s);

Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 01:38 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22