The Wayback Machine - https://web.archive.org/web/20240903163148/https://www.geeksforgeeks.org/basic-input-and-output-in-c/
Open In App

Basic Input and Output in C

Last Updated : 04 Apr, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

C language has standard libraries that allow input and output in a program. The stdio.h or standard input output library in C that has methods for input and output.

scanf()

The scanf() method, in C, reads the value from the console as per the type specified and store it in the given address.

Syntax:

scanf("%X", &variableOfXType);

where %X is the format specifier in C. It is a way to tell the compiler what type of data is in a variable and & is the address operator in C, which tells the compiler to change the real value of variableOfXType, stored at this address in the memory.


printf()

The printf() method, in C, prints the value passed as the parameter to it, on the console screen.

Syntax:

printf("%X", variableOfXType);

where %X is the format specifier in C. It is a way to tell the compiler what type of data is in a variable and variableOfXType is the variable to be printed.

How to take input and output of basic types in C?

The basic type in C includes types like int, float, char, etc. Inorder to input or output the specific type, the X in the above syntax is changed with the specific format specifier of that type. The Syntax for input and output for these are:

  • Integer:
Input: scanf("%d", &intVariable);
Output: printf("%d", intVariable);
  • Float:
Input: scanf("%f", &floatVariable);
Output: printf("%f", floatVariable);
  • Character:
Input: scanf("%c", &charVariable);
Output: printf("%c", charVariable);

Please refer Format specifiers in C for more examples. 

C
// C program to show input and output

#include <stdio.h>

int main()
{

    // Declare the variables
    int num;
    char ch;
    float f;

    // --- Integer ---

    // Input the integer
    printf("Enter the integer: ");
    scanf("%d", &num);

    // Output the integer
    printf("\nEntered integer is: %d", num);

    // --- Float ---
  
    //For input Clearing buffer
      while((getchar()) != '\n');

    // Input the float
    printf("\n\nEnter the float: ");
    scanf("%f", &f);

    // Output the float
    printf("\nEntered float is: %f", f);

    // --- Character ---

    // Input the Character
    printf("\n\nEnter the Character: ");
    scanf("%c", &ch);

    // Output the Character
    printf("\nEntered character is: %c", ch);

    return 0;
}

Output
Enter the integer: 10
Entered integer is: 10

Enter the float: 2.5
Entered float is: 2.500000

Enter the Character: A
Entered Character is: A

How to take input and output of advanced type in C?

The advanced type in C includes type like String. In order to input or output the string type, the X in the above syntax is changed with the %s format specifier. The Syntax for input and output for String is:

Input: scanf("%s", stringVariable);
Output: printf("%s", stringVariable);

Example: 

C
// C program to show input and output
 
#include <stdio.h>
 
int main()
{
 
    // Declare string variable
    // as character array
    char str[50];
 
    // --- String ---
    // To read a word
 
    // Input the Word
    printf("Enter the Word: ");
    scanf("%s\n", str);
 
    // Output the Word
    printf("\nEntered Word is: %s", str);
 
    // --- String ---
    // To read a Sentence
 
    // Input the Sentence
    printf("\n\nEnter the Sentence: ");
    scanf("%[^\n]s", str);
 
    // Output the String
    printf("\nEntered Sentence is: %s", str);
 
    return 0;
}

Output
Enter the Word: GeeksForGeeks
Entered Word is: GeeksForGeeks

Enter the Sentence: Geeks For Geeks
Entered Sentence is: Geeks For Geeks

Related Articles:




Previous Article
Next Article

Similar Reads

C | Input and Output | Question 13
Predict the output of following program? #include &quot;stdio.h&quot; int main() { char arr[100]; printf(&quot;%d&quot;, scanf(&quot;%s&quot;, arr)); /* Suppose that input value given for above scanf is &quot;GeeksQuiz&quot; */ return 1; } (A) 9 (B) 1 (C) 10 (D) 100 Answer: (B) Explanation: In C, scanf returns the no. of inputs it has successfully
1 min read
C | Input and Output | Question 2
Predict output of the following program C/C++ Code #include int main() { printf("\new_c_question\b\r"); printf("geeksforgeeks"); getchar(); return 0; } (A) ew_c_questiongeeksforgeeks (B) new_c_quesgeeksforgeeks (C) geeksforgeeks (D) Depends on terminal configuration Answer: (D) Explanation: See https://stackoverflow.com/questions/17236242/usage-of-
1 min read
C | Input and Output | Question 3
#include &lt;stdio.h&gt; int main() { printf(&quot; \&quot;GEEKS %% FOR %% GEEKS\&quot;&quot;); getchar(); return 0; } (A) “GEEKS % FOR % GEEKS” (B) GEEKS % FOR % GEEKS (C) \"GEEKS %% FOR %% GEEKS\" (D) GEEKS %% FOR %% GEEKS Answer: (A) Explanation: Backslash (\\\\) works as escape character for double quote (“). For explanation of %%, see https://
1 min read
C | Input and Output | Question 4
C/C++ Code #include // Assume base address of \"GeeksQuiz\" to be 1000 int main() { printf(5 + \"GeeksQuiz\"); return 0; } (A)GeeksQuiz (B)Quiz (C)1005 (D)Compile-time error Answer: (B)Explanation: printf is a library function defined under stdio.h header file. The compiler adds 5 to the base address of the string through the expression 5 + \"Geeks
1 min read
C | Input and Output | Question 5
Predict the output of the below program: C/C++ Code #include int main() { printf(\"%c \", 5[\"GeeksQuiz\"]); return 0; } (A) Compile-time error (B) Runtime error (C) Q (D) s Answer: (C) Explanation: The crux of the program lies in the expression: 5[\"GeeksQuiz\"] This expression is broken down by the compiler as: *(5 + \"GeeksQuiz\"). Adding 5 to t
1 min read
C | Input and Output | Question 6
Predict the output of below program: #include &lt;stdio.h&gt; int main() { printf(&quot;%c &quot;, &quot;GeeksQuiz&quot;[5]); return 0; } (A) Compile-time error (B) Runtime error (C) Q (D) s Answer: (C) Explanation: The crux of the program lies in the expression: "GeeksQuiz"[5]. This expression is broken down by the compiler as: *(“GeeksQuiz” + 5).
1 min read
C | Input and Output | Question 9
What does the following C statement mean? C/C++ Code scanf(\"%4s\", str); (A)Read exactly 4 characters from console. (B)Read maximum 4 characters from console. (C)Read a string str in multiples of 4 (D)Nothing Answer: (B)Explanation: Try following program, enter GeeksQuiz, the output would be \"Geek\" #include &lt;stdio.h&gt; int main() { char str[
1 min read
C | Input and Output | Question 13
#include&lt;stdio.h&gt; int main() { char *s = &quot;Geeks Quiz&quot;; int n = 7; printf(&quot;%.*s&quot;, n, s); return 0; } (A) Geeks Quiz (B) Nothing is printed (C) Geeks Q (D) Geeks Qu Answer: (C) Explanation: .* means The precision is not specified in the format string, but as an additional integer value argument preceding the argument that ha
1 min read
C | Input and Output | Question 11
Predict the output of following program? #include &lt;stdio.h&gt; int main(void) { int x = printf(&quot;GeeksQuiz&quot;); printf(&quot;%d&quot;, x); return 0; } (A) GeeksQuiz9 (B) GeeksQuiz10 (C) GeeksQuizGeeksQuiz (D) GeeksQuiz1 Answer: (A) Explanation: The printf function returns the number of characters successfully printed on the screen. The st
1 min read
C | Input and Output | Question 12
Output of following program? #include&lt;stdio.h&gt; int main() { printf(&quot;%d&quot;, printf(&quot;%d&quot;, 1234)); return 0; } (A) 12344 (B) 12341 (C) 11234 (D) 41234 Answer: (A) Explanation: printf() returns the number of characters successfully printed on the screen.Quiz of this Question
1 min read
C | Input and Output | Question 13
What is the return type of getchar()? (A) int (B) char (C) unsigned char (D) float Answer: (A) Explanation: The return type of getchar() is int to accommodate EOF which indicates failure:Quiz of this Question
1 min read
Formatted and Unformatted Input/Output functions in C with Examples
This article focuses on discussing the following topics in detail- Formatted I/O Functions.Unformatted I/O Functions.Formatted I/O Functions vs Unformatted I/O Functions.Formatted I/O Functions Formatted I/O functions are used to take various inputs from the user and display multiple outputs to the user. These types of I/O functions can help to dis
9 min read
Input-output system calls in C | Create, Open, Close, Read, Write
System calls are the calls that a program makes to the system kernel to provide the services to which the program does not have direct access. For example, providing access to input and output devices such as monitors and keyboards. We can use various functions provided in the C Programming language for input/output system calls such as create, ope
10 min read
Code valid in both C and C++ but produce different output
There are some syntactical structures that are valid for both C and C++ but different behavior when compiled and run in the both languages. Several differences can also be exploited to create code that compile in both languages but behave differently. For example, the following function will return different values in C and C++: Example Codes valid
2 min read
How to print range of basic data types without any library function and constant in C?
How to write C code to print range of basic data types like int, char, short int, unsigned int, unsigned char etc? It is assumed that signed numbers are stored in 2's complement form. We strongly recommend to minimize the browser and try this yourself first. Following are the steps to be followed for unsigned data types. 1) Find number of bytes for
4 min read
Output of the program | Dereference, Reference, Dereference, Reference....
Predict the output of below program #include&lt;stdio.h&gt; int main() { char *ptr = &quot;geeksforgeeks&quot;; printf(&quot;%c\n&quot;, *&amp;*&amp;*ptr); getchar(); return 0; } Output: g Explanation: The operator * is used for dereferencing and the operator &amp; is used to get the address. These operators cancel effect of each other when used on
1 min read
Output of C programs | Set 63
Prerequisite : Structure padding, integer promotion and sequence points. Q1. Consider the following code: C/C++ Code #include&lt;stdio.h&gt; struct geeks{ int i; char c; } obj; int main() { printf(&quot;%ld&quot;, sizeof(obj)); } What would be the output of the above code? A. 4 B. 5 C. 8 D. Can't be determined Output: Can't be determined Explanatio
4 min read
How to Change the Output of printf() in main() in C?
To change the output of printf() in main(), we can use Macro Arguments. #define macro can be used for this task. This macro is defined inside the function. Although, #define can be used without declaring it in the function, in that case always the printf() will be changed. The function needs to be called first to change the output of printf() in ma
2 min read
Output of C programs | Set 30 (Switch Case)
Prerequisite - Switch Case in C/C++ Interesting Problems of Switch statement in C/C++ Program 1 #include &lt;stdio.h&gt; int main() { int num = 2; switch (num + 2) { case 1: printf(&quot;Case 1: &quot;); case 2: printf(&quot;Case 2: &quot;); case 3: printf(&quot;Case 3: &quot;); default: printf(&quot;Default: &quot;); } return 0; } Output: Default:
2 min read
Basic Code Optimizations in C
Generally, resources are shared between different processes. Suppose your program takes more resources, then definitely it will affect the performance of other processes that need the same resources. So we have a need to write and optimize our program keeping in mind resources e.g. processor's time and main memory. Following are some Optimization t
7 min read
C Basic Syntax
C is a procedural programming language. It was developed by Dennis Ritchie at the Bell Laboratories in the year 1972. Despite being old C is a very popular language among programmers. It is a very fast language compared to other languages like Python, Java, etc. Below is the basic syntax structure of the C program: The basic syntax of the C program
5 min read
How to input or read a Character, Word and a Sentence from user in C?
C is a procedural programming language. It was initially developed by Dennis Ritchie as a system programming language to write an operating system. The main features of the C language include low-level access to memory, a simple set of keywords, and a clean style, these features make C language suitable for system programmings like operating system
4 min read
Input an integer array without spaces in C
How to input a large number (a number that cannot be stored even in long long int) without spaces? We need this large number in an integer array such that every array element stores a single digit. Input : 10000000000000000000000000000000000000000000000 We need to read it in an arr[] = {1, 0, 0...., 0} In C scanf(), we can specify count of digits t
2 min read
How to use getline() in C++ when there are blank lines in input?
In C++, if we need to read a few sentences from a stream, the generally preferred way is to use the getline() function as it can read string streams till it encounters a newline or sees a delimiter provided by the user. Also, it uses &lt;string.h&gt; header file to be fully functional. Here is a sample program in c++ that reads four sentences and d
2 min read
getchar_unlocked() – Faster Input in C/C++ For Competitive Programming
getchar_unlocked() is similar to getchar() with the exception that it is not thread-safe. This function can be securely used in a multi-threaded program if and only if they are invoked when the invoking thread possesses the (FILE*) object, as is the situation after calling flockfile() or ftrylockfile(). Syntax: int getchar_unlocked(void); Example:
2 min read
Clearing The Input Buffer In C/C++
What is a buffer? A temporary storage area is called a buffer. All standard input and output devices contain an input and output buffer. In standard C/C++, streams are buffered. For example, in the case of standard input, when we press the key on the keyboard, it isn’t sent to your program, instead of that, it is sent to the buffer by the operating
6 min read
Taking String input with space in C (4 Different Methods)
We can take string input in C using scanf("%s", str). But, it accepts string only until it finds the first space. There are 4 methods by which the C program accepts a string with space in the form of user input.Let us have a character array (string) named str[]. So, we have declared a variable as char str[20]. Method 1 : Using getsSyntax : char *ge
2 min read
Difference and Similarities between PHP and C
PHP is a server-side scripting language designed specifically for web development. It can be easily embedded in HTML files and HTML codes can also be written in a PHP file. The thing that differentiates PHP from a client-side language like HTML is, PHP codes are executed on the server whereas HTML codes are directly rendered on the browser. C is a
3 min read
Similarities and Differences between Ruby and C language
Similarities between Ruby and C There are many similarities between C and Ruby, and some of them are: Like C, in Ruby also… A programmer is able to program procedurally if they like to do. But still, behind the scenes, it will be object-oriented.Both the languages have the same operators, for example, compound assignment and bitwise operators. But
3 min read
Getting System and Process Information Using C Programming and Shell in Linux
Whenever you start a new process in Linux it creates a file in /proc/ folder with the same name as that of the process id of the process. In that folder, there is a file named "status" which has all the details of the process. We can get those Process Information Through shell as follows: cat /proc/1/status As can be seen, it displays most of the i
2 min read
Article Tags :