Scanf and Address Operator in C

Previous
Next

scanf():

  • C library is a Collection of Header files.
  • Header file contains Variables and Functions.
  • printf() and scanf() functions belongs to stdio.h header file.
  • Printf() function is used to display the information on Monitor(standard output).
  • Scanf() function is used to read information from keyboard(standard input)

Address operator:

  • We need to specify the location (memory address) using variable to read the data. 
  • ‘&’ is called Address operator in C.
#include <stdio.h>
int main()
{
	int a;
	printf("Default value is : %d \n", a);
	
	printf("Enter a value : ");
	scanf("%d", &a);
	
	printf("New value is : %d \n", a);
	return 0;
}

Output:
Default value is : 1794675883
Enter a value : 100
New value is : 100

Can we display the memory locations of each variable?

  • Yes allowed, we can print address of variable using ‘&’ operator.
  • Memory locations are different from system to system.
  • Memory address is a random integer value.
  • We must use integer format specifier(%d) to display the address.

Code:

#include <stdio.h>
int main()
{
	int val=10;
	char sym='$';
	
	printf("Address of val : %d \n", &val);
	printf("Value of val : %d \n", val);
	
	printf("Address of sym : %d \n", &sym);
	printf("Value of sym : %c \n", sym);
	return 0;
}

Addition of 2 Numbers:

#include <stdio.h>
int main()
{
	int a, b;
	printf("Enter 2 integers : \n");
	scanf("%d%d", &a, &b);
	printf("Sum is : %d \n", a+b);
	return 0;
}

Output:
Enter 2 integers :
13
12
Sum is : 25
Previous
Next

Add Comment

Courses Enquiry Form