Docsity

C Programming: Input/Output and Operators, Summaries of Mathematics

Partial preview of the text

Download C Programming: Input/Output and Operators and more Summaries Mathematics in PDF only on Docsity!

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

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

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 7: C Character I/O

โ— (^) 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

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

// print Hello World to the screen

printf("Hello World");

return 0;

Hello World

Output :

Types of Comments

There are two ways to add comments in C:

  1. // - Single Line Comment
  2. /.../ - Multi-line Comment

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

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)

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;

++a = 11 --b = 99 ++c = 11. --d = 99.

Output

Here, the operators ++ and -- are used as prefixes. These two operators can also be used as postfixes like a++ and a--.