Entering data from the user C language - ProgrammerTech
Language:

Search

Entering data from the user C language

  • Share this:
Entering data from the user C language

User data entry functions

Entering data from the user, there are some functions in programming languages, not only C, that require the user to enter data to play a certain role. In this lesson, we will mention some of these functions and from these functions:

  1. scanf() function.
  2. getchar() function.
  3. getche() function.
  4. getch() function.
  5. putchar() function.
  6. puts() function.
  7. gets() function.

 

general input function scanf

scanf function in C language. This function is used to read or receive data from the input tool "keyboard" during program execution, and those values ​​are integers, fractions, or letters, as in the following examples.

Example 1

Enter a valid number using Int.

#include<stdio.h>
main()
{
    int a;
    printf("inter is number = ");
    scanf("%d", &a); 
    printf("%d \n" , a);
}

We have declared a variable named a and we have printed a plain text which is "Enter a number" and after that the line sacnf is executed i.e. Enter a number and when a number is entered the entered number is printed as shown in the following execution

inter is number = 23
23

Example 2

Enter a character using char.

#include<stdio.h>
main()
{
    char a ;
    printf("inter One char = " ,a);
    scanf("%c" , &a);
        printf("%c \n" ,a);
}

In execution, we enter a letter and press the Enter button, and it prints the letter.

Example 3

Entering a real decimal number using Float.

#include<stdio.h>
main()
{
    float a ;
    printf("inter number by float = " , a);
    scanf("%f" , &a);
        printf("%f \n" ,a);
}

In execution, we enter a real number 1.1 and press Enter and it prints the number as 1.1000000.

Example 4

Write a program that receives a number from the user and then stores it in a variable and then prints it again.

#include<stdio.h>
int main()
{
    int num;
        printf("Enter a Number : ");
        scanf("%d", &num);
        printf("The number you have entered = %d", num);
        return 0;
}

 

Performing arithmetic operations by entering numbers by the user

We can make the user enter numbers for the program and in turn the program performs arithmetic operations for the numbers entered in it, for example we want to write a calculator program and ask the user to enter the numbers he wants and the program performs the required operation and outputs it on the screen.

Example 5

Enter a valid setting.

#include<stdio.h>
main()
{
    int a , b , c;
    printf("inter is number1 = ");
    scanf("%d", &a); 
    printf("inter is number2 = ");
    scanf("%d", &b); 
    c=a+b;
    printf("%d \n" ,c)'
}

In the execution, we entered the first number, which is 3 and pressed the Enter key on the keyboard, then we entered the second number and we pressed Enter and he added the two numbers and their result was 8.

Example 6

Entering real numbers using float

#include<stdio.h>
main()
{
    float a , b , c;
    printf(" Enter number by float =  " ,a);
    scanf("%f" , &a); 
        printf("Enter anther number by float =  " ,b);
    scanf("%f" , &b);
    c=a+b;
        printf("%f \n" ,c);
}

In execution, we entered the first number, which is 1.1, and press Enter on the keyboard, then we entered the second number, which is 2.1, and press Enter, and he added the two numbers, and their result was 3.2.

 

Functions for receiving literal data from the user

We talked above about the most important function, which is the scanf function, which reads or receives data from the keyboard input tool during program execution, and those values ​​are integers, fractions, or letters. And now we will talk about several functions responsible for entering a single character. Although the scanf function receives all kinds of data, the C language has other functions to deal with letters and phrases.

 

getchar() function to receive data

The getchat() function is used to enter only one character, and the character appears after pressing the enter button on the keyboard, as in the following example:

#include<stdio.h>
main()
{
    char a;
    a=getchar();
    printf("%c",a);
}

When we type the first letter of the name and press Enter, it prints the letter you entered.

And if we write a complete sentence to see what will happen, "I wrote the name of Saja".

Saja
S

We note here that he printed only the first letter and ignored the rest

 

getche() function to read inputs in C

When using the getche() function, the conio.h library must be used, and the function is used to read one character. The difference between it and getchar is that the getchar function must when executing, press enter from the keyboard, and you do not need to use the conio.h library. As for the getche function You need the conio.h library, and you do not need to press the enter button on the keyboard. It is used to enter only one letter, and the letter appears immediately after typing it.

#include<stdio.h>
#include<conio.h>
main()
{
    char a;
    a=getche();
    printf("%c \n",a);
}

 

getch() function to read inputs in C

When using the getch() function, the conio.h library must be used and the function is used to read a single character. The difference between it and getche is that the getche function needs. to the conio.h library, and you don't need to hit the enter button on the keyboard. And it is used to enter only one character, and the character appears immediately after typing it, and the getch function needs the conio.h library, but it does not need it. To press the enter button on the keyboard, and it is used to enter only one letter, and the letter does not appear on the screen.

#include<stdio.h>
#include<conio.h>
main()
{
    char a;
    a=getch();
    printf("%c \n",a);
}

 

putchar() function to print a single character in C

In the previous lessons, we learned that the function used in printing is the printf function, now we will learn about a function responsible for printing a single character, which is the putchar function.

#include<stdio.h>
main()
{
    char a;
    a=getchar();
    putchar(a);
}

 

puts() function for printing in C

puts() function This function is used to print characters as a name, we notice that you have typed several names inside the puts function.

#include<stdio.h>
main()
{
    puts("Muhammad Alush");
    puts("Database");
puts("Saja CS");
}

After we have written them and executed the code will it work as the printf function or not.

Muhammad Alush
Database
Saja CS

 

gets() function to receive and print characters from the user

The gets() function uses this function to enter characters by the user and prints them using the puts function.

#include<stdio.h>
main()
{
    char name[10];
    gets(name);
    puts(name);
}

The meaning of char name[10] is that the variable name is of a type char that is suitable for storing a word of maximum length 10.

Saja Osman
Saja Osman

We note that he did an input and output command, which is to write the name of saja osman and print it.


Tags:
Saja Alsadig

Saja Alsadig

من السودان مبرمجة مواقع ويب و تطبيقات أندرويد اتقن لغات "html, css, php, js" بالإضافة للتعامل مع اندرويد ستوديو و بناء التطبيقات, مهتمة بمجال التقنية منذ الصغر و كان لدي شقف كبير للدخول بالجامعه بقسم البرمجة و دخلته في عام 2013 و تعلمت كل شيئ ضمن الجامعة و خارجها من التعليم الذاتي و اتمنى أن أكون عند حسن ظن الجميع و إيصال الأمانة لكم, اعمل حاليا في إدارة موقع جامعة السودان.