Thread: scanning variable without address of variable

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    99

    scanning variable without address of variable

    I wrote this program that show result with scanning address of variable (scanf("%s", &name))

    Code:
    #include <stdio.h> 
    int main()
    {
        char name[100];
     
        printf("Enter a Name of candidate \n");
        scanf("%s", &name);
     
        printf("Name of candidate is  %s\n",name);
        
        return 0;
    }
    Enter a Name of candidate
    Dev
    Name of candidate is Dev

    again I wrote program without & (scanf("%s", name))
    Code:
    #include <stdio.h> 
    int main()
    {
        char name[100];
     
        printf("Enter a Name of candidate \n");
        scanf("%s", name);
     
        printf("Name of candidate is  %s\n",name);
    	
        return 0;
    }
    Enter a Name of candidate
    Dev
    Name of candidate is Dev

    when we write scanf in c program we should always write (&) in program. My second program is not correct but why it show the result.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    My second program is not correct but why it show the result.
    No your second program is correct, it is your assumptions that are wrong. What scanf() expects are addresses of variables and the address of a string is the name of the variable.


    By the way your first program is incorrect and your compiler should be warning you about the problems (if you have properly configured your compiler).

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by jimblumberg View Post
    By the way your first program is incorrect and your compiler should be warning you about the problems (if you have properly configured your compiler).
    when I remove address of variable in this program they it doesn't show result and if put Adrress & then it show result
    Code:
    #include <stdio.h>
    
     int main()
    {
        int Number;
     
        printf("Enter the Number \n");
    	
    	scanf("%d",Number);
    	
    	printf("Number is %d",Number);
    	
        return 0;
    }

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    when I remove address of variable in this program they it doesn't show result and if put Adrress & then it show result
    As I said in my last post scanf() requires an address, you're not providing that required address in your current snippet. Since the variable is not a C-string you need to use the address of operator (&) to pass the address of the variable.

    If your compiler was properly configured it should warn you about this problem.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    These two things are the same.
    Code:
      char name[100];
      scanf("%s", name);
      scanf("%s", &name[0]);
    This 'works' because a pointer to a whole array has the same value (but a different type).
    Code:
      char name[100];
      scanf("%s", &name);
    But it's a dangerous trap to believe it's always the same.

    If you use a pointer, then the correct forms still work.
    Code:
      char name[100];
      char *p = name;
      scanf("%s", p);
      scanf("%s", &p[0]);
    But saying this is broken!
    Code:
      char name[100];
      char *p = name;
      scanf("%s", &p);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 12-26-2013, 01:32 PM
  2. Replies: 2
    Last Post: 10-31-2012, 03:08 PM
  3. Replies: 4
    Last Post: 05-06-2012, 06:24 AM
  4. Scanning file contents to variable
    By Porl in forum C Programming
    Replies: 5
    Last Post: 02-11-2011, 09:15 AM
  5. Replies: 1
    Last Post: 09-22-2009, 08:47 PM

Tags for this Thread