This blog is under construction

Friday 27 April 2012

scanf example in C

Header file:
    stdio.h

Synopsis:
     int scanf(const char *format, ...);

Description:
    Performs input format conversion. It is similar to fscanf with stdin as its input stream.


scanf function C example:


  #include<stdio.h>
  #include<string.h>
  int main() {
        char name[100];
        int age;
        printf("Enter your name:");
        fgets(name, 90, stdin);
        name[strlen(name) - 1] = '\0';
        printf("Enter your age:");
        scanf("%d", &age);
        printf("Name:%s\n", name);
        printf("Age:%d\n", age);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/cpgms/exp$ ./a.out
  Enter your name:JP
  Enter your age:22
  Name:JP
  Age:22



No comments:

Post a Comment