Thread: code output...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    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);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This can't possibly be the code you've tried to compile, because it doesn't.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    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 . . .

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    @roann
    Don't use void main ever. See the FAQ.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    >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.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  3. Replies: 4
    Last Post: 01-16-2002, 12:04 AM
  4. problems with output from code
    By simhap in forum C++ Programming
    Replies: 0
    Last Post: 10-08-2001, 12:43 PM