C/Console/Console Read Int

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Add format to scanf

<source lang="cpp">

  1. include <stdio.h>

int main(void) {

 int i, j;
 printf("Enter an integer: ");
 scanf("%3d %d", &i, &j);
 printf("%d %d", i, j);
 return 0;

}


      </source>


Demonstrates how to read a number

<source lang="cpp">

  1. include <stdio.h>

char line[100]; int value; int main() {

   printf("Enter a value: ");
   fgets(line, sizeof(line), stdin);
   sscanf(line, "%d", &value);
   printf("Twice %d is %d\n", value, value * 2);
   return (0);

}


      </source>


Read and write an int value from and to console

<source lang="cpp">

  1. include <stdio.h>

int main(void) {

 int i;
 scanf("%d", &i);
 printf("%d", i);
 return 0;

}


      </source>


Read formatted data from stdin: how to use scanf

<source lang="cpp">

  1. include <stdio.h>

int main () {

 char str[80];
 int i;
 printf ("Enter your last name: ");
 scanf ("%s",str);  
 printf ("Enter your age: ");
 scanf ("%d",&i);
 printf ("Last Name =  %s , age = %d .\n",str,i);
 
 printf ("Enter a hexadecimal number: ");
 scanf ("%x",&i);
 
 printf ("You have entered %#x (%d).\n",i,i);
 
 return 0;

}

      </source>


Read int value from console

<source lang="cpp">

  1. include <stdio.h>

main() {

   int i = 0;
   int k,j=10;
   
   i=scanf("%d%d%d",&j,&k,&i);
   printf("total values inputted %d\n",i);
   printf("The input values %d %d\n",j,k);

}

      </source>


Read unsigned int, long and short from console

<source lang="cpp">

  1. include <stdio.h>

int main(void) {

 unsigned u;
 long l;
 short s; 
 printf("Enter an unsigned: ");
 scanf("%u", &u); 
 
 printf("Enter a long: ");
 scanf("%ld", &l);
 
 printf("Enter a short: ");
 scanf("%hd", &s);
 printf("Here are they: %u %ld %hd\n", u, l, s);
 return 0;

}

      </source>