Simple program to show different typesof input and output.



*Second C Program*
     In our previous article we have seen the very basic c program.

     Now, in this article, I will show you the program that gives the information about how to accept a character value a numeric value and sequence of characters that is nothing but string. String is any combination of alphabets i.e. nothing but any word.

     As we have seen in previous article when we you printf(); statement it displays the contents written in it. It means it is used to display the data. We can show the output using printf();


     Likewise when we want to accept the data from user, we need to use scanf(); statement.

     In scanf(); statement, ‘%d’ is used to accept integer values, ‘%c’ is used to accept character values and ‘%s’ is used to accept the string. ‘&’ is used as an address space. It stores the value at a particular location which is given by the user. In sort it is used to store the data at a particular location.

      When you use scanf(); statement you need to use & , because it is used to store the value but when you use to display the same value using printf(); statement then don’t use &. Because it will display you the memory location address. The address of memory, where the variable is stored.

      Try the following program to use integer and character data types


//This program is designed to show you how to enter number,character and string
//that is nothing but the combination of letters eg. your name, college name etc.

#include<stdio.h>
#include<conio.h>

void main()
{
            char x,str[20];
            int x1=0;//initialize an integer value to 0. it is not compulsory but
                        //it helps to prevent garbage

            clrscr();//clears the screen
            printf("\n Enter an alphabet ");
            flushall();//used to cear the buffer
            scanf("%c",&x);//used to accept the character given by the user

            printf("\n Enter a String ");//string means combination of characters
                                                    //you can enter your name
            scanf("%s",str);//accepts the string

            printf("\n Enter a numeric value ");
            scanf("%d",&x1);

            printf("\n You have entered the information as follow:-");
            printf("\n Character = %c",x); //dont use & in display
            printf("\n String = %s",str);
            printf("\n Number = %d",x1);
            getch();//wait for the character to be entered by the user
                        //it means output window remains visible till user presses any key
}

Output:-
Compile the program using Alt+F9 and run the program using Ctrl+F9

The output will be as follow




This is the second program to show you the basic working of integer and character values. Try it for using different variable names and give different values. Check your output each time.

In our next article we will see the working of array using for loop.
Till then Enjoy Programming..!!

Comments