0% found this document useful (0 votes)
58 views23 pages

C Output

The document discusses C programming output and input functions like printf() and scanf(). It provides examples of: 1) Using printf() to output strings, integers, floats, doubles, and characters with format specifiers like %d, %f, %lf, %c. 2) Using scanf() to take integer, float, double, and character input and store it in variables. 3) Other operators like increment, decrement, assignment, and modulo and examples of using them.

Uploaded by

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

C Output

The document discusses C programming output and input functions like printf() and scanf(). It provides examples of: 1) Using printf() to output strings, integers, floats, doubles, and characters with format specifiers like %d, %f, %lf, %c. 2) Using scanf() to take integer, float, double, and character input and store it in variables. 3) Other operators like increment, decrement, assignment, and modulo and examples of using them.

Uploaded by

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

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

// Displays the string inside quotations

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 the main() 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 include stdio.h header file using the #include


<stdio.h> statement.

 The return 0; statement inside the main() function is the "Exit status" of the program. It's
optional.

Example 2: 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.

Example 3: 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.

Example 4: 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 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.

Example 5: 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.

Example 6: 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.

Example 7: 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.

Example 8: 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.

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 Specifiers for I/O

As you can see from the above examples, we use

 %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.

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

OPERANDS &

MODULO %
C Programming Operators
In this tutorial, you will learn about different operators in C programming with the
help of examples.

An operator is a symbol that operates on a value or a variable. For example:  +  is an


operator to perform addition.
C has a wide range of operators to perform various operations.

C Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition,
subtraction, multiplication, division etc on numerical values (constants and variables).

Operator Meaning of Operator

+ addition or unary plus

- subtraction or unary minus

* multiplication

/ division

% remainder after division (modulo division)

Example 1: Arithmetic Operators

// Working of arithmetic operators


#include <stdio.h>
int main()
{
int a = 9,b = 4, 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("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

The operators  + ,  -  and  *  computes addition, subtraction, and multiplication


respectively as you might have expected.
In normal calculation,  9/4 = 2.25 . However, the output is  2  in the program.
It is because both the variables  a  and b are integers. Hence, the output is also an
integer. The compiler neglects the term after the decimal point and shows
answer  2  instead of  2.25 .
The modulo operator  %  computes the remainder. When  a=9  is divided by  b=4 , the
remainder is  1 . The  %  operator can only be used with integers.
Suppose  a = 5.0 ,  b = 2.0 ,  c = 5  and  d = 2 . Then in C programming,

// Either one of the operands is a floating-point number


a/b = 2.5

a/d = 2.5

c/b = 2.5

// Both operands are integers

c/d = 2

C Increment and Decrement Operators


C programming has two operators increment  ++  and decrement  --  to change the value
of an operand (constant or variable) by 1.
Increment  ++  increases the value by 1 whereas decrement  --  decreases the value by 1.
These two operators are unary operators, meaning they only operate on a single
operand.
Example 2: Increment and Decrement Operators

// Working of increment and decrement operators


#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;

printf("++a = %d \n", ++a);


printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
return 0;
}

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

// Working of assignment operators


#include <stdio.h>
int main()
{
int a = 5, c;

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.

Relational operators are used in decision making and loops.


Operator Meaning of Operator Example

== Equal to 5 == 3 is evaluated to 0

> Greater than 5 > 3 is evaluated to 1

< Less than 5 < 3 is evaluated to 0

!= Not equal to 5 != 3 is evaluated to 1

>= Greater than or equal to 5 >= 3 is evaluated to 1

<= Less than or equal to 5 <= 3 is evaluated to 0

Example 4: Relational Operators

// Working of relational operators


#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;

printf("%d == %d is %d \n", a, b, a == b);


printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);
return 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

An expression containing logical operator returns either 0 or 1 depending upon


whether expression results true or false. Logical operators are commonly used
in decision making in C programming.
Operato
Meaning Example
r

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

Logical NOT. True only if the operand


! If c = 5 then, expression !(c==5) equals to 0.
is 0

Example 5: Logical Operators

// Working of logical operators

#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;

result = (a == b) && (c > b);


printf("(a == b) && (c > b) is %d \n", result);

result = (a == b) && (c < b);


printf("(a == b) && (c < b) is %d \n", result);

result = (a == b) || (c < b);


printf("(a == b) || (c < b) is %d \n", result);

result = (a != b) || (c < b);


printf("(a != b) || (c < b) is %d \n", result);

result = !(a != b);


printf("!(a != b) is %d \n", result);

result = !(a == b);


printf("!(a == b) is %d \n", 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

Explanation of logical operator program


 (a == b) && (c > 5)  evaluates to 1 because both operands  (a == b)  and  (c >

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

During computation, mathematical operations like: addition, subtraction,


multiplication, division, etc are converted to bit-level which makes processing faster
and saves power.

Bitwise operators are used in C programming to perform bit-level operations.

Operators Meaning of operators

& Bitwise AND


Operators Meaning of operators

| Bitwise OR

^ Bitwise exclusive OR

~ Bitwise complement

<< Shift left

>> Shift right

Visit bitwise operator in C to learn more.


Other Operators

Comma Operator

Comma operators are used to link related expressions together. For example:

int a, c = 5, d;

The sizeof operator

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

Size of int = 4 bytes


Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte

Other operators such as ternary operator  ?: , reference operator  & , dereference


operator  *  and member selection operator  ->  will be discussed in later tutorials.

Previous Tutorial:
Program to Add Two Integers

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

int number1, number2, sum;

printf("Enter two integers: ");


scanf("%d %d", &number1, &number2);

// calculating sum
sum = number1 + number2;

printf("%d + %d = %d", number1, number2, sum);


return 0;
}

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.

printf("Enter two integers: ");


scanf("%d %d", &number1, &number2);

Then, these two numbers are added using the  +  operator, and the result is stored in
the  sum  variable.

sum = number1 + number2;

Finally, the  printf()  function is used to display the sum of numbers.

printf("%d + %d = %d", number1, number2, sum);

Program to Multiply Two Numbers


#include <stdio.h>
int main() {
double a, b, product;
printf("Enter two numbers: ");
scanf("%lf %lf", &a, &b);

// Calculating product
product = a * b;

// Result up to 2 decimal point is displayed using %.2lf


printf("Product = %.2lf", product);

return 0;
}

Output

Enter two numbers: 2.4


1.12
Product = 2.69

In this program, the user is asked to enter two numbers which are stored in
variables  a  and  b  respectively.

printf("Enter two numbers: ");


scanf("%lf %lf", &a, &b);

Then, the product of  a  and  b  is evaluated and the result is stored in  product .

product = a * b;

Finally,  product  is displayed on the screen using  printf() .

printf("Product = %.2lf", product);


Notice that, the result is rounded off to the second decimal place
using  %.2lf  conversion character.

Program to Compute Quotient and Remainder


#include <stdio.h>

int main() {

int dividend, divisor, quotient, remainder;

printf("Enter dividend: ");

scanf("%d", &dividend);

printf("Enter divisor: ");

scanf("%d", &divisor);

// Computes quotient

quotient = dividend / divisor;

// Computes remainder

remainder = dividend % divisor;

printf("Quotient = %d\n", quotient);

printf("Remainder = %d", 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.

printf("Enter dividend: ");

scanf("%d", &dividend);

printf("Enter divisor: ");

scanf("%d", &divisor);

Then the quotient is evaluated using  /  (the division operator), and stored in  quotient .

quotient = dividend / divisor;

Similarly, the remainder is evaluated using  %  (the modulo operator) and stored
in  remainder .

remainder = dividend % divisor;

Finally, the quotient and remainder are displayed using  printf() .

printf("Quotient = %d\n", quotient);

printf("Remainder = %d", remainder);

You might also like