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

Basic Input and Output in C

Last Updated : 04 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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
Article Tags :
three90RightbarBannerImg