C Programming

C Input and Output Functions

What are the C Input and Output Functions?

C Input and Output Functions are printf and scanf. In this tutorial, we will learn the input and output functions in C programming and write a few examples and consolidate them. The functions scanf() and printf() are commonly used functions for input and output. With the Scanf function, previously formatted data is entered into the program. With the printf function, it is the function that allows us to write text to the screen. For more videos check my youtube channel. And you can follow my web site for more C programming course. Let’s start C Input and Output Functions.

C Input and Output Functions

 

1. Example: C Input and Output Functions let’s write an example

#include <stdio.h>
int main()
{
 printf("Hello World \n");
return 0;
}

The program output will look like the following.

C Input and Output Functions

How does this program work?

For all c programs, main() must be written.Because code execution or startup starts from the main() function. The printf function is a function for displaying formatted output on the screen this function <stdio.it is located in the h> library. We will talk about C Programming libraries later, don’t get confused for now, our priority is to learn the input and output functions. We use #include to call the libraries we need. In addition, you will also skip a line down with \n. In C Input and Output Functions you can write \n only on output do not write on scanf. :)

 

2. Example Displaying an Integer on the Screen

#include <stdio.h>
int main()
{
int number=5;
printf("%d \n",number);
return 0;
}

The program output will look like the following. As you can see, we first defined a variable called number and assigned the number 5 to this variable. When we print our variable on the screen, it will now show us the number 5. %d is used to show integers in printf functions. Let’s learn another C Input and Output Functions.

C Input and Output Functions

 

3. Example : Now let’s take a number from the user and print it on the screen.

#include <stdio.h>
int main()
{
int number;
printf("enter a value : ");
scanf("%d",&number);
printf("Your number is %d",number);
return 0;
}

The program output will be as follows. The number you enter from the keyboard will appear in front of you. Of course, since we define the variable as int, we can use the data numerically (1,2,3.. ) you need to enter. When you enter any letter or phrase, you will receive an error. As it turned out, we found out how to get the int variable with scanf. When we use our int variables in the above format, we can now get the numbers from the user using the scanf function.

C Input and Output Functions

 

4. Example C Decimal number input output function

#include <stdio.h>
int main()
{
float number;
printf("Enter a value:");
scanf("%f",&number);
printf("The value you enter : %f",number);
return 0;
}

The program output will be as follows. Decimal numbers are used as shown in the figure. Maybe the code you typed will not always work with an integer. Maybe you may need to use decimals in some of your projects. Thus, by using the Float variable type, we learned how to ask the user for decimal numbers and how to print them on the screen.

C Input and Output Functions

 

 

5. Example : Input of the Character

#include <stdio.h>
int main()
{
char test;
printf("please enter a character:");
scanf("%c",&test);
printf("You entered %c",test);
return 0;
}

The use of %c to enter and display characters in the C program has been learned. However, because the character is a single letter, enter only a single letter in English. Please note that the program will give an error when you enter a sentence or a word.

C Input and Output Functions

 

 

6. Example : Showing the ASCII code of the value we entered

#include <stdio.h>
int main()
{
    char test;
    printf("bir karakter giriniz: ");
    scanf("%c",&test);     

    printf("girdiginiz karakter %c.",test);  

    printf("ASCII degeri : %c sayisal degeri ise %d.", chr, chr);  
    return 0;
}

Now that we have found out the ASCII code of the character we entered, we can use it in the next example. Below you can see the program output.

C Input and Output Functions

 

 

7. Example : learning the ASCII value of a number

#include <stdio.h>
int main()
{
    int chr = 69;
    printf("69 ASCII Value is : %c.",chr);
    return 0;
}

Aşağıda gördüğünüz gibi 69 karakter değeri E harfiymiş.

C Input and Output Functions

 

And we learn C Input and Output. These are printf and scanf. With C Input and Output we can enter value to program than screen them.

 

Leave a Reply

Your email address will not be published.

Back to top button