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

C Programming

The document discusses various input and output functions in C programming like printf(), scanf() and provides examples of using them. It also explains different types of operators used in C like arithmetic, relational, logical operators and provides examples of using them.

Uploaded by

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

C Programming

The document discusses various input and output functions in C programming like printf(), scanf() and provides examples of using them. It also explains different types of operators used in C like arithmetic, relational, logical operators and provides examples of using them.

Uploaded by

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

C Programming

C Input Output (I/O)


Now we will learn to use scanf() function to take input from the user, and printf() function to display output to the
user.

C Output
In C programming, printf() is one of the main output function. The function sends formatted output to the
screen. For example,

Example 2: Integer Output


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.
We use %d format specifier to print int types. Here, the %d inside the quotations will be
replaced by the value of testInteger.

Example 2: C Output
To print float, we use %f format specifier. Similarly, we use %lf to print double values.

Example 3: float and double Output


To print char, we use %c format specifier.

Example 4: Print Characters


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

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.
Example 6: Float and Double Input/Output

We use %f and %lf format specifier for float and


double respectively.
Example 7: C Character I/O
Example 8: ASCII Value

● 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.
I/O Multiple Values

Here's how you can take


multiple inputs from the user
and display them.
C Comments
In programming, comments are hints that a programmer can add to make their code easier to
read and understand. For example,

#include <stdio.h>

int main() {
Output :
// print Hello World to the screen Hello World
printf("Hello World");
return 0;
}
Types of Comments

There are two ways to add comments in C:

1. // - Single Line Comment

2. /*...*/ - Multi-line Comment


1. Single-line Comments in C
In C, a single line comment starts with //. It starts and ends in the same
line. For example,

#include <stdio.h>

int main() {

// create integer variable


int age = 25;

// print the age variable


printf("Age: %d", age);

return 0;
}
2. Multi-line Comments in C
In C programming, there is another type of comment that allows us to comment on multiple lines at

once, they are multi-line comments.

To write multi-line comments, we use the /*....*/ symbol. For example,


/* This program takes age input from the user
It stores it in the age variable
And, print the value using printf() */

#include <stdio.h>

int main() {

int age;

…………………………….
C Programming Operators
Operator Meaning of Operator
An operator is a symbol that operates on a value or a variable. For
example: + is an operator to perform addition. + addition or unary plus

C has a wide range of operators to perform various operations. - subtraction or unary


minus

C Arithmetic Operators * multiplication

/ division

An arithmetic operator performs mathematical operations such as % remainder after


addition, subtraction, multiplication, division etc on numerical division (modulo
values (constants and variables). division)
// Working of arithmetic operators
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;

c = a+b;
Example 1: Arithmetic Operators 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);
Output : c = a%b;
printf("Remainder when a
a+b = 13 divided by b = %d \n",c);
a-b = 5
a*b = 36
a/b = 2 return 0;
Remainder when a divided by b=1 }
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()
Here, the operators ++ and { Output
-- are used as prefixes.
int a = 10, b = 100;
These two operators can
float c = 10.5, d = 100.5;
also be used as postfixes ++a = 11
like a++ and a--. --b = 99
printf("++a = %d \n", ++a); ++c = 11.500000
printf("--b = %d \n", --b); --d = 99.500000
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);

return 0;
}
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
C Relational Operators Operat Meaning of Operator Example
or

A relational operator checks the == Equal to 5 == 3 is evaluated to

relationship between two operands. If 0


the relation is true, it returns 1; if the
> Greater than 5 > 3 is evaluated to 1
relation is false, it returns value 0.

Relational operators are used in < Less than 5 < 3 is evaluated to 0


decision making and loops.
!= Not equal to 5 != 3 is evaluated to
1

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


to 1

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


0
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 If c = 5 and d = 2 then, expression ((c==5)


one operand is true || (d>5)) equals to 1.

! Logical NOT. True only if the If c = 5 then, expression !(c==5) equals to


operand is 0 0.

You might also like