In our last tutorial, we learned about data types, now we will learn how to get values and how to print values for diff data types. We will discuss two functions, one is scanf()
which used to get input from user and another is printf()
which used to display output to user. Also we will discuss about format specifier for each data types.
C Output
First we will discuss about print() function which is basically consider as main output function as its print value on screen in human readable format. For example,
C Normal Output
#include <stdio.h> int main() { // this will display the values inside double quotes. printf("C Programming"); return 0; }
Output
C Programming
How does this program work?
- All valid C programs must contain the
main()
function. The code execution begins from the start of themain()
function. - The
printf()
is a library function to send formatted output to the screen. The function prints the string inside quotations. - To use
printf()
in our program, we need to includestdio.h
header file using the#include <stdio.h>
statement. - The
return 0;
statement inside themain()
function is the “Exit status” of the program. It’s optional.
C Integer Output
#include <stdio.h> int main() { int testInteger = 5; printf("Number = %d", testInteger); return 0; }
Output
Number = 5
We use %d
format specifier to print int
types. Here, the %d
inside the quotations will be replaced by the value of testInteger.
C float and double Output
#include <stdio.h> int main() { float number1 = 13.5; double number2 = 12.4; printf("number1 = %f\n", number1); printf("number2 = %lf", number2); return 0; }
Output
number1 = 13.500000 number2 = 12.400000
To print float
, we use %f
format specifier. Similarly, we use %lf
to print double
values.
C Print Characters
#include <stdio.h> int main() { char chr = 'a'; printf("character = %c", chr); return 0; }
Output
character = a
To print char
, we use %c
format specifier.
C Normal Input
In C programming, scanf()
is one of the commonly used function to take input from the user. The scanf()
function reads formatted input from the standard input such as keyboards.
C Integer Input/Output
#include <stdio.h> int main() { int testInteger; printf("Enter an integer: "); scanf("%d", &testInteger); printf("Number = %d",testInteger); return 0; }
Output
Enter an integer: 4 Number = 4
Here, we have used %d
format specifier inside the scanf()
function to take int
input from the user. When the user enters an integer, it is stored in the testInteger variable.
Notice, that we have used &testInteger
inside scanf()
. It is because &testInteger gets the address of testInteger, and the value entered by the user is stored in that address.
C Float and Double Input/Output
#include <stdio.h> int main() { float num1; double num2; printf("Enter a number: "); scanf("%f", &num1); printf("Enter another number: "); scanf("%lf", &num2); printf("num1 = %f\n", num1); printf("num2 = %lf", num2); return 0; }
Output
Enter a number: 12.523 Enter another number: 10.2 num1 = 12.523000 num2 = 10.200000
We use %f
and %lf
format specifier for float
and double
respectively.
C Character I/O
#include <stdio.h> int main() { char chr; printf("Enter a character: "); scanf("%c",&chr); printf("You entered %c.", chr); return 0; }
Output
Enter a character: g You entered g
When a character is entered by the user in the above program, the character itself is not stored. Instead, an integer value (ASCII value) is stored.
And when we display that value using %c
text format, the entered character is displayed. If we use %d
to display the character, it’s ASCII value is printed.
C ASCII Value
#include <stdio.h> int main() { char chr; printf("Enter a character: "); scanf("%c", &chr); // When %c is used, a character is displayed printf("You entered %c.\n",chr); // When %d is used, ASCII value is displayed printf("ASCII value is %d.", chr); return 0; }
Output
Enter a character: g You entered g. ASCII value is 103.
C I/O Multiple Values
Here’s how you can take multiple inputs from the user and display them.
#include <stdio.h> int main() { int a; float b; printf("Enter integer and then a float: "); // Taking multiple inputs scanf("%d%f", &a, &b); printf("You entered %d and %f", a, b); return 0; }
Output
Enter integer and then a float: -3 3.4 You entered -3 and 3.400000
Format Specifier for I/O
As you can see from the above examples, we use
%d
forint
%f
forfloat
%lf
fordouble
%c
forchar
Here’s a list of commonly used C data types and their format specifiers.
Data Type | Format Specifier |
---|---|
int | %d |
char | %c |
float | %f |
double | %lf |
short int | %hd |
unsigned int | %u |
long int | %li |
long long int | %lli |
unsigned long int | %lu |
unsigned long long int | %llu |
signed char | %c |
unsigned char | %c |
long double | %Lf |
Leave a Reply
View Comments