0
#include<stdio.h>
int main()
{
 int a;
 char c;
 int *A=&a;
 char *C=&c;
 printf("Enter the value of a,c\n");
 scanf("%d,%d",&a,&c);
 printf ("Adress of a,c= %d,%d\n",A,C);
 printf("value of a,c= %d %d\n",a,c);
 return 0;
}

output is:

c:\Users\Avinash\Desktop>a.exe

Enter the value of a,c

12,40

Adress of a,c= 6356740,6356739

value of a,c= 0, 40

5
  • 2
    Use %p to print address. Commented Dec 24, 2016 at 18:22
  • whatever value I am assigning to variable a, it always gives "0" in output Commented Dec 24, 2016 at 18:22
  • 3
    scanf("%d,%d",&a,&c); --> scanf("%d, %c",&a,&c); or char c; --> int c; Commented Dec 24, 2016 at 18:23
  • Thanks, problem is solved@BLUEPIXY and @MayurK Commented Dec 24, 2016 at 18:41
  • You never initialized the a and c variables, so they could have any value when your program starts. A good coding standard is to initialize variables with a value when they are defined. Commented Dec 24, 2016 at 18:41

3 Answers 3

1

As others said, the error is in the scanf() format expression '%d' used for reading the character c. What is happening is this: Note that the address of c is one lower than the address of a (this is expected for variables on the stack). Assume a character occupies one byte and an integer occupies 4 bytes. By using '%d' you are telling scanf() that the second pointer points to an integer; therefore it reads the second value as an integer, --padding out the high-order bits with zeros--, and stores the value --in the four bytes-- based at the address of c. The padding overwrites the low-order bits of a, explaining why the printf() shows its value as 0.

Sign up to request clarification or add additional context in comments.

Comments

0

%c for scanning char. Change

 scanf("%d,%d",&a,&c);

to

 scanf("%d,%c",&a,&c);   //make sure there is only single comma in between the inputs. Nothing else, nothing more.

Comments

0

you have some problem with the scanf format and the printf format

int a;
    char c;
    int *A = &a;
    char *C = &c;
    printf("Enter the value of a,c\n");
    scanf("%d, %c", &a, &c);  //%c for reading chars
    printf("Adress of a= %p, c= %p\n", A, C);  // printing the address %p
    printf("value of a= %d c= %c\n", a, c);    //%c for printing chars

2 Comments

thanks, but the result was same for "%d" and"%p" for printing address, what could be the possible problems, if I keep using '%d' for getting address
@AvinashKumarShudhanshu what problem are you getting from the code and what do you want to get that you are not getting? please be specific so that i can help. when you use %d for address it convert the address to integer, some compiler will give you warning, gcc -wall -werror.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.