Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can anyone explain, Im learning Pointers in C, Here While running the code, why it asking 2 inputs when I wrote code to take single input

#include<stdio.h>
void square(int *n);
int main(){
  int number;printf("Enter number : ");
  scanf("%d\n", &number);

  square(&number);
  return 0;
  }
void square(int *n){
*n=(*n)*(*n);
printf("Square : %d\n", *n);
}

This is my code, Here while running, it taking two inputs to run. I don’t why. Can anyone explain this.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Please try removing the \n from your scanf.

scanf will try to consume each of the specifiers you are giving it. You are telling it to expect a ‘newline’, so it will consume the first newline you supply by hitting ‘enter’. Essentially you are telling your program to expect the user to hit Enter twice.

#include <stdio.h>
void square(int *n);
int main(){
  int number;printf("Enter number : ");
  scanf("%d", &number);

  square(&number);
  return 0;
}
void square(int *n){
    *n=(*n)*(*n);
    printf("Square : %d\n", *n);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading