Quick links
Latest articles
Internship
Members
New...
Algorithms
Discrete Mathematics
Big data
Languages
C
C++
C++ STL
Java
Data Structure
C#.Net
Android
Kotlin
SQL
Web
PHP
Python
JavaScript
CSS
Ajax
Node.js
Web prog.
Programs
C
C++
DS
Java
C#
Python
Aptitude
C
C++
Java
DBMS
Interview
C
Embedded C
Java
SEO
HR
CS Subjects
CS Basics
O.S.
Networks
DBMS
Embedded Systems
Cloud Computing
Machine learning
CS Organizations
Linux
DOS
More...
Articles
Puzzles
News/Updates

Home » C programs » C scanf() programs

Read a memory address using scanf() and print its value in C



Here, we are going to learn how to read a memory address using scanf() and print value stored at the given memory address in C programming language?
Submitted by IncludeHelp, on September 12, 2018

Here, we have to input a valid memory address and print the value stored at memory address in C.

To input and print a memory address, we use "%p" format specifier – which can be understood as "pointer format specifier".

Program:

In this program - first, we are declaring a variable named num and assigning any value in it. Since we cannot predict a valid memory address. So here, we will print the memory address of num and then, we will read it from the user and print its value.

#include <stdio.h>

int main(void) 
{

	int num = 123;
	int *ptr; //to store memory address

	printf("Memory address of num = %p\n", &num);

	printf("Now, read/input the  memory address: ");
	scanf ("%p", &ptr);

	printf("Memory address is: %p and its value is: %d\n", ptr, *ptr);

	return 0;
}

Output

Memory address of num = 0x7ffc505d4a44
Now, read/input the  memory address: 7ffc505d4a44
Memory address is: 0x7ffc505d4a44 and its value is: 123 

Explanation:

In this program, we declared an unsigned int variable named num and assigned the variable with the value 123.

Then, we print the value of num by using "%p" format specifier – it will print the memory address of num – which is 0x7ffc505d4a44.

Then, we prompt a message "Now, read/input the memory address: " to take input the memory address – we input the same memory address which was the memory address of num. The input value is 7ffc505d4a44. And stored the memory address to a pointer variable ptr. (you must know that only pointer variable can store the memory addresses. Read more: pointers in C language).

Note: While input, "0x" is not required.

And finally, when we print the value using the pointer variable ptr. The value is 123.






Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.