Operators, Expressions, and Data Input/Output
1. Operators and Expressions
Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations:
+(Addition): Adds two values.- Example:
x + yaddsxandy.
- Example:
-(Subtraction): Subtracts one value from another.- Example:
x - ysubtractsyfromx.
- Example:
*(Multiplication): Multiplies two values.- Example:
x * ymultipliesxandy.
- Example:
/(Division): Divides one value by another.- Example:
x / ydividesxbyy.
- Example:
%(Modulus): Returns the remainder of division.- Example:
x % yreturns the remainder whenxis divided byy.
- Example:
Unary Operators
Unary operators operate on a single operand to perform operations like incrementing or decrementing a value:
++(Increment): Increases a value by 1.- Example:
x++; // Increment x by 1.--(Decrement): Decreases a value by 1.- Example:
x--; // Decrement x by 1.
Relational and Logical Operators
These operators are used to compare values and combine conditions:
- Relational Operators: Compare two values:
==(Equal to): Returns true if both operands are equal.!=(Not equal to): Returns true if operands are not equal.<(Less than): Returns true if left operand is less than the right.>(Greater than): Returns true if left operand is greater than the right.
- Logical Operators: Combine multiple boolean expressions:
&&(Logical AND): True if both operands are true.||(Logical OR): True if at least one operand is true.!(Logical NOT): Inverts the truth value of the operand.
Assignment Operators
Assignment operators are used to assign values to variables:
=: Assigns a value to a variable.- Example:
a = 5; // Assigns 5 to a.- Shorthand Assignment:
+=,-=,*=: Update the value of a variable using the current value.- Example:
a += 5; // Equivalent to a = a + 5;
Conditional Operator (?:)
The conditional operator is a shorthand for if-else statements:
- Example:
result = (a > b) ? a : b; // If a > b, result = a; else, result = b.Library Functions
Library functions are predefined functions provided by C to perform common tasks:
- Example:
printf()for printing output to the console.scanf()for reading input from the user.
2. Data Input and Output
Single Character Input/Output
Functions used for handling single characters in C:
getchar(): Reads a single character from standard input.- Example:
char c = getchar(); // Reads a character from input.putchar(): Outputs a single character to standard output.- Example:
putchar(c); // Outputs the character c.
Formatted Input/Output
Functions for reading and writing formatted data:
scanf(): Reads formatted input from the user.- Example:
scanf("%d", &x); // Reads an integer and stores it in x.printf(): Prints formatted output to the screen.- Example:
printf("x = %d", x); // Prints the value of x.
Gets and Puts Functions
Functions for handling strings:
gets(): Reads a string from the user (Note:gets()is unsafe and has been removed in C11; usefgets()instead).- Example:
gets(str); // Reads a string into str.puts(): Outputs a string to the console.- Example:
puts(str); // Prints the string str.