Monday, April 28, 2025

C Tutorial - 6 - User Input

#include <stdio.h>

int main()
{
    int firstNumber;
    float secondNumber;
    char someWord[30];

    printf("Enter First Number: \n");
    scanf("%d", &firstNumber);

    printf("Enter Second Number: \n");
    scanf("%f", &secondNumber);

    printf("Enter Some Word: \n");
    scanf("%s", &someWord);

    printf("-------------------------- \n");

    printf("First Number: %d \n", firstNumber);
    printf("Second Number: %f \n", secondNumber);
    printf("Some Word: %s \n", someWord);

    printf("-------------------------- \n");

    return 0;
}

Result:

Enter First Number:
5
Enter Second Number:
20
Enter Some Word:
asdas
--------------------------
First Number: 5
Second Number: 20.000000
Some Word: asdas
--------------------------

Process returned 0 (0x0)   execution time : 5.460 s
Press any key to continue.

This code is a simple program that prompts the user to input three values (an integer, a float, and a string) using the scanf() function and then displays those values using the printf() function.

Here is how the code works:

  • The program includes the standard input/output library (stdio.h).
  • In the main() function, three variables are declared: firstNumber (an integer), secondNumber (a float), and someWord (a character array that can hold up to 30 characters).
  • The program then prompts the user to input the first number using the printf() function and reads in the input using the scanf() function with the %d format specifier.
  • Next, the program prompts the user to input the second number using the printf() function and reads in the input using the scanf() function with the %f format specifier.
  • Finally, the program prompts the user to input a string using the printf() function and reads in the input using the scanf() function with the %s format specifier.
  • The program then displays the values of the three variables using the printf() function with appropriate format specifiers.

Note that in the scanf() function, the & operator is used to get the memory address of each variable to read the input into. Also, the %s format specifier reads in a string of characters terminated by a whitespace character (space, tab, or newline), so it may not work as expected if the user enters a string with whitespace characters.

 

What is char someWord[30];

char someWord[30] is a character array in C, which is capable of holding 30 characters. In other words, it is a sequence of characters, represented as a contiguous block of memory, with each character taking one byte of memory. The size of the array is specified in square brackets, in this case 30.

This particular character array, someWord, can be used to store strings of up to 29 characters long, with one extra byte reserved for the null terminator character '\0'. The null terminator indicates the end of the string and is automatically added to the end of the string by C when you use string functions such as printf() or scanf().

So, char someWord[30] is used to declare a character array that can store a string of up to 29 characters in C.

 

Why do we need %s format specifier.

We use the %s format specifier in C to read input as a string of characters from the user using the scanf() function. The %s format specifier tells the scanf() function to read a string of characters from the user input until it encounters a whitespace character, such as space, tab or newline.

For example, consider the following code:

#include <stdio.h>

int main() {
    char name[20];
    printf("Enter your name: ");
    scanf("%s", name);
    printf("Hello, %s!\n", name);
    return 0;
}

Result:

Enter your name: Alan
Hello, Alan!

Process returned 0 (0x0)   execution time : 2.280 s
Press any key to continue.

In this program, we use %s format specifier to read a string of characters from the user and store it in the name character array. Then, we use the printf() function to print the string Hello, followed by the name variable, which contains the user's name.

So, %s format specifier is useful when we want to read a string of characters from the user input in C programming.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...