1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

store and print the input numbers entered by user

Discussion in 'Microcontrollers' started by Parth86, Aug 22, 2017.

  1. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    546
    Likes:
    5
    Hi
    I want to write c program that will store and print the input numbers entered by user. I have doubt I know how to print any number on screen but I don't know what logic use in c programming to store user input
    my effort
    Code (c):

    #include <stdio.h>
    int main()
    {
           int number;
     
        printf("Enter an integer: ");

        scanf("%d", &number);

       printf("display  entered value : %d", number);
     
        return 0;
    }
     
    I have tested this program when I enter any number I see that numbers display on Screen. I just want to know how does user input store in c programming I mean wichh logic statement is use to store data. I think we use for loop , array , pointer . In this case how to decide what will store user input.
     
    Last edited: Aug 22, 2017
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    The variable "number" is allocated for input... There is no error checking in that code, so if a non number is pressed, number will be corrupted..

    scanf() is a formatted scan routine that will allow you to input the number, it will convert the number, then it will store it in "number"..
     
  3. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    546
    Likes:
    5
    ok. can you help me with small example because when I try to understand big program I feel difficulty
    suppose we have three buttons
    first for number "1"
    second for number "2"
    third for "+"
    forth for "result "

    program
    enter fist operand and print on screen
    enter operator and print on screen
    enter second operand and print on screen
    display result on screen

    it will better if you will try to explain embedded c program. my goal to understand logic how do you apply logic to make any program
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE

    So! You want a simple calculator program for an 8051 in C?

    When the user presses the first button, a 1 is stored.. When the user presses the second button, 2 is stored.. then + and finally a result is displayed...
    Code (c):

    void main()
     {
     int first, second, third;
     char buff[] = {"Press the keys"};
     Lcd_init();
     init_ser();
     for( ; ; )
      {
        Lcd_line(1);
        Lcd_write(buff);
        while(BUT1);
        first = 1;
        Lcd_xy(1,2);
        Lcd_data(first + 0x30);
        while(BUT2);
        second = 2;
         Lcd_xy(3,2);
         Lcd_data(second + 0x30);
         while(BUT3);
         Lcd_xy(2,2);
         Lcd_data('+');
         Lcd_xy(4,2);
         Lcd_data('=');
         third = second + first;
         Lcd_xy(5,2);
         Lcd_data(third + 0x30);
      }
     }
     
     

Share This Page