C Output
C Output
In C programming, printf() is one of the main output function. The function sends formatted output to
the screen. For example,
Example 1: C Output
#include <stdio.h>
int main()
printf("C Programming");
return 0;
Output
C Programming
All valid C programs must contain the main() function. The code execution begins from the start
of the main() function.
The printf() is a library function to send formatted output to the screen. The function prints the
string inside quotations.
The return 0; statement inside the main() function is the "Exit status" of the program. It's
optional.
#include <stdio.h>
int main()
int testInteger = 5;
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.
#include <stdio.h>
int main()
return 0;
Output
number1 = 13.500000
number2 = 12.400000
#include <stdio.h>
int main()
Output
character = a
C 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.
#include <stdio.h>
int main()
int testInteger;
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.
#include <stdio.h>
int main()
float num1;
double num2;
scanf("%f", &num1);
scanf("%lf", &num2);
return 0;
Output
num1 = 12.523000
num2 = 10.200000
#include <stdio.h>
int main()
char chr;
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.
#include <stdio.h>
int main()
char chr;
scanf("%c", &chr);
return 0;
Output
Enter a character: g
You entered g.
ASCII value is 103.
Here's how you can take multiple inputs from the user and display them.
#include <stdio.h>
int main()
int a;
float b;
return 0;
Output
3.4
%d for int
%f for float
%lf for double
%c for char
Here's a list of commonly used C data types and their format specifiers.
int %d
char %c
float %f
double %lf
unsigned int %u
signed char %c
unsigned char %c
OPERANDS &
MODULO %
C Programming Operators
In this tutorial, you will learn about different operators in C programming with the
help of examples.
C Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition,
subtraction, multiplication, division etc on numerical values (constants and variables).
* multiplication
/ division
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
Output
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1
a/d = 2.5
c/b = 2.5
c/d = 2
Output
++a = 11
--b = 99
++c = 11.500000
--d = 99.500000
Here, the operators ++ and -- are used as prefixes. These two operators can also be
used as postfixes like a++ and a-- . Visit this page to learn more about how increment
and decrement operators work when used as postfix.
C Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common
assignment operator is =
Operator Example Same as
= a=b a=b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
Example 3: Assignment Operators
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);
return 0;
}
Output
c = 5
c = 10
c = 5
c = 25
c = 5
c = 0
C Relational Operators
A relational operator checks the relationship between two operands. If the relation is
true, it returns 1; if the relation is false, it returns value 0.
== Equal to 5 == 3 is evaluated to 0
Output
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1
C Logical Operators
Logical AND. True only if all If c = 5 and d = 2 then, expression ((c==5) &
&&
operands are true (d>5)) equals to 0.
Logical OR. True only if either one If c = 5 and d = 2 then, expression ((c==5) |
||
operand is true (d>5)) equals to 1.
Operato
Meaning Example
r
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
return 0;
}
Output
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
b) is 1 (true).
(a == b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false).
(a == b) || (c < b) evaluates to 1 because (a = b) is 1 (true).
(a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c <
b) are 0 (false).
!(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a != b)
is 1 (true).
!(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is 0
(false).
C Bitwise Operators
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
Comma Operator
Comma operators are used to link related expressions together. For example:
int a, c = 5, d;
The sizeof is a unary operator that returns the size of data (constants, variables, array,
structure, etc).
Example 6: sizeof Operator
#include <stdio.h>
int main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
return 0;
}
Output
Previous Tutorial:
Program to Add Two Integers
#include <stdio.h>
int main() {
// calculating sum
sum = number1 + number2;
Output
Enter two integers: 12
11
12 + 11 = 23
In this program, the user is asked to enter two integers. These two integers are stored
in variables number1 and number2 respectively.
Then, these two numbers are added using the + operator, and the result is stored in
the sum variable.
// Calculating product
product = a * b;
return 0;
}
Output
In this program, the user is asked to enter two numbers which are stored in
variables a and b respectively.
Then, the product of a and b is evaluated and the result is stored in product .
product = a * b;
int main() {
scanf("%d", ÷nd);
scanf("%d", &divisor);
// Computes quotient
// Computes remainder
return 0;
Output
Enter dividend: 25
Enter divisor: 4
Quotient = 6
Remainder = 1
In this program, the user is asked to enter two integers (dividend and divisor). They
are stored in variables dividend and divisor respectively.
scanf("%d", ÷nd);
scanf("%d", &divisor);
Then the quotient is evaluated using / (the division operator), and stored in quotient .
Similarly, the remainder is evaluated using % (the modulo operator) and stored
in remainder .