0% found this document useful (0 votes)
18 views7 pages

Basic Input and Output in C

Uploaded by

vrushank solanke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

Basic Input and Output in C

Uploaded by

vrushank solanke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Basic Input and Output in C



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.

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);

● Printing of an Integer

#include <stdio.h>
void main() {

int number = 42;

printf("The number is: %d", number);

Output:

The number is:


42

● Printing of a Character

#include <stdio.h>

void main() {

char ch = 'A';

printf("The character is: %c", ch);

Output:
The character is: A

● Printing of Float

#include <stdio.h>

void main() {

float num2 = 2.71828;

printf("The value of num2 is: %f", num2);

Output:

The value of num1 is: 3.141590

The value of num2 is: 2.718280

● Printing of Multiple Outputs

#include <stdio.h>

void main() {

int year = 1996;

int month = 8;

int day = 16;


printf("Today's date is: %d-%d-%d", year, month, day);

Output:

Today's date is 1996-8-16

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.

● Taking Input of Integer Value

#include <stdio.h>

void main() {

int num;

printf("Enter an integer: ");

scanf("%d", &num);

printf("The entered integer is: %d", num);


}

Output:

Enter an integer: 25

The entered integer is: 25

● Taking Input of Float Value

#include <stdio.h>

void main() {

float num;

printf("Enter a float value: ");

scanf("%f", &num);

printf("The entered float value is: %f", num);

Output:
Enter a float value: 3.14

The entered float value is: 3.140000

● Taking Input of Character Value

#include <stdio.h>

void main() {

char ch;

printf("Enter a character: ");

scanf(" %c", &ch);

printf("The entered character is: %c",


ch);

Output:

Enter a character: A

The entered character is: A

● Taking Input of Multiple Values


#include <stdio.h>

void main() {

int num1, num2;

printf("Enter two numbers separated by a space: ");

scanf("%d %d", &num1, &num2);

printf("The entered numbers are: %d and %d", num1, num2);

Output:

Enter two numbers separated by a space: 10


20

The entered numbers are: 10 and 20

You might also like