Operators in C are symbols that perform operations on variables or values. They can be classified into several categories:
a. Arithmetic Operators
+: Addition
-: Subtraction
*: Multiplication
/: Division
%: Modulus (remainder of division)
Example:
int a = 5, b = 2;
int sum = a + b; // sum = 7
b. Relational Operators
==: Equal to
!=: Not equal to
>: Greater than
<: Less than
>=: Greater than or equal to
<=: Less than or equal to
c. Logical Operators
&&: Logical AND
||: Logical OR
!: Logical NOT
d. Bitwise Operators
&: AND
|: OR
^: XOR
~: NOT
<<: Left shift
>>: Right shift
e. Assignment Operators
=: Assign value
+=: Add and assign
-=: Subtract and assign
*=: Multiply and assign
/=: Divide and assign
f. Increment/Decrement Operators
++: Increment
--: Decrement
g. Conditional (Ternary) Operator
?: A shorthand for an if-else statement.
Syntax: condition ? expr1 : expr2;
Example:
int result = (a > b) ? a : b; // result is assigned the value of a if a > b, else b
h. sizeof Operator
Returns the size (in bytes) of a data type or variable.
int x = 10;
printf("%lu", sizeof(x)); // Outputs the size of int
a. Types of Statements
Expression Statements: Perform a computation or operation.
x = a + b;
Compound Statements (Blocks): A group of statements enclosed in {}.
{
int a = 5;
a = a + 10;
}
Control Statements: Alter the flow of execution.
Conditional Statements: if, else if, else, switch.
Looping Statements: for, while, do-while.
Jump Statements: break, continue, goto.
a. printf()
Used for output: Displays formatted output on the console.
Example:
printf("The value of x is %d", x); // %d is used to print integers
b. scanf()
Used for input: Takes user input from the console.
Example:
int num;
scanf("%d", &num); // %d reads an integer
c. getch()
Reads a single character from the keyboard (without waiting for the Enter key).
Example:
char c;
c = getch(); // Reads a single character
d. getchar()
Reads a single character from the standard input (keyboard).
Example:
char c;
c = getchar(); // Waits for user to press a key and enter
e. putchar()
Outputs a single character to the console.
Example:
putchar('A'); // Prints 'A'
Header Files: These contain declarations of functions and macros used in programs.
Standard library header files:
#include <stdio.h>: Includes input/output functions (printf(), scanf(), etc.)
#include <stdlib.h>: Includes functions for memory allocation, process control, etc.
#include <math.h>: Includes mathematical functions.
Custom header files can also be included, typically with .h extension.
Example:
#include <stdio.h>
#include <math.h>
a. #include
Purpose: To include the contents of a file (library or custom header) in the program before actual compilation.
Syntax:
#include <stdio.h> // Standard library
#include "myheader.h" // Custom header file
b. #define
Purpose: Used to define constants or macros.
Syntax:
#define PI 3.14
#define MAX(a, b) ((a) > (b) ? (a) : (b))
Understanding operators, functions, statements, and I/O functions in C is essential for building effective programs. The preprocessor directives like #include and #define are useful for including libraries and defining constants or macros, improving code readability and maintainability.
Here’s a comprehensive example that covers operators, functions, statements, I/O operations, header files, and preprocessor directives in C:
#include <stdio.h> // Header file for I/O functions
#define PI 3.14 // Preprocessor directive to define a constant PI
// Function declaration
float calculateArea(float radius); // Function prototype
int main() {
float radius, area;
// Console-based input: Reading the radius of the circle
printf("Enter the radius of the circle: ");
scanf("%f", &radius); // Using scanf to take input
// Function call to calculate area
area = calculateArea(radius); // Calling the function
// Console-based output: Printing the area of the circle
printf("The area of the circle with radius %.2f is %.2f\n", radius, area);
// Demonstrating operators
int a = 10, b = 5;
printf("Addition: %d + %d = %d\n", a, b, a + b); // Arithmetic Operator (+)
printf("Comparison: %d > %d = %d\n", a, b, a > b); // Relational Operator (>)
printf("Logical: %d && %d = %d\n", a > b, b > 10, (a > b) && (b > 10)); // Logical Operator (&&)
// Demonstrating control statements
if (area > 50) { // Conditional statement (if)
printf("The circle has a large area.\n");
} else {
printf("The circle has a small area.\n");
}
// Demonstrating loop statement (for loop)
printf("The first 5 multiples of 2 are: ");
for (int i = 1; i <= 5; i++) {
printf("%d ", 2 * i); // Printing multiples of 2
}
printf("\n");
// Demonstrating increment and decrement operators
int x = 5;
printf("Initial value of x: %d\n", x);
printf("Post-increment: %d\n", x++);
printf("Value of x after post-increment: %d\n", x);
// Demonstrating getch() and getchar()
printf("Press any key to continue: ");
getchar(); // Wait for the user to press a key
printf("You pressed a key.\n");
return 0;
}
// Function definition to calculate area of a circle
float calculateArea(float radius) {
return PI * radius * radius; // Formula for area of a circle
}
Header Files:
#include <stdio.h>: Includes the standard I/O library, enabling the use of functions like printf() and scanf().
Preprocessor Directives:
#define PI 3.14: This defines a constant PI to be used in the calculation of the circle’s area.
Function:
float calculateArea(float radius) is a user-defined function to calculate the area of a circle. It uses the constant PI and the formula Area = PI * radius^2.
I/O Functions:
printf(): Used for output (displaying messages).
scanf(): Used for input (taking the radius of the circle from the user).
getchar(): Used to capture a key press from the user.
Operators:
Arithmetic operators: +, used to add two numbers.
Relational operator: >, used to compare two values.
Logical operator: &&, used to check if both conditions are true.
Control Flow Statements:
if-else statement: Used to check if the area of the circle is greater than 50 and print the appropriate message.
for loop: Used to print the first 5 multiples of 2.
Increment and Decrement Operators:
x++: Post-increment operator used to increment the value of x.
Enter the radius of the circle: 5
The area of the circle with radius 5.00 is 78.50
Addition: 10 + 5 = 15
Comparison: 10 > 5 = 1
Logical: 1 && 0 = 0
The circle has a large area.
The first 5 multiples of 2 are: 2 4 6 8 10
Initial value of x: 5
Post-increment: 5
Value of x after post-increment: 6
Press any key to continue: You pressed a key.
This example demonstrates various operators, functions, I/O operations, and control structures in C. It also illustrates how header files and preprocessor directives work together to make the code more modular and efficient. This is a practical example for understanding the core concepts of C programming.
1. Operators in C
Arithmetic Operators: Used to perform basic arithmetic operations.
+ (Addition), - (Subtraction), * (Multiplication), / (Division), % (Modulus)
Relational Operators: Used to compare two values.
== (Equal to), != (Not equal to), > (Greater than), < (Less than), >= (Greater than or equal to), <= (Less than or equal to)
Logical Operators: Used to perform logical operations.
&& (Logical AND), || (Logical OR), ! (Logical NOT)
Assignment Operators: Used to assign values to variables.
=, +=, -=, *=, /=
Increment/Decrement Operators: Used to increase or decrease a variable's value by one.
++ (Post-increment/Pre-increment), -- (Post-decrement/Pre-decrement)
Bitwise Operators: Used for bit-level operations.
&, |, ^, ~, <<, >>
2. Functions in C
Function Definition: A function is a block of code designed to perform a specific task.
Syntax: return_type function_name(parameters) { // body }
Function Call: A function is called to execute from the main program or other functions.
Syntax: function_name(arguments);
Function Types:
Void Function: A function that does not return any value.
Non-Void Function: A function that returns a value.
3. I/O Functions in C
printf(): Used for output (printing values).
Example: printf("Hello, World!");
scanf(): Used for input (taking user input).
Example: scanf("%d", &x);
getchar(): Reads a single character from the input buffer.
Example: ch = getchar();
putchar(): Prints a single character to the console.
Example: putchar('A');
getch(): Reads a character from the console without waiting for Enter key (used in consoled based input).
4. Preprocessor Directives
#include: Used to include standard or user-defined header files.
Example: #include <stdio.h>
#define: Used to define constants or macros.
Example: #define PI 3.14
5. Types of Statements in C
Expression Statements: Statements that evaluate an expression.
Example: a = b + c;
Conditional Statements:
if-else: Used to execute code based on a condition.
Example: if (x > 0) { printf("Positive"); } else { printf("Negative"); }
Looping Statements:
for: Used for repeating a block of code a specified number of times.
Example: for(int i = 0; i < 5; i++) { printf("%d", i); }
while: Executes as long as a condition is true.
Example: while (x > 0) { x--; }
do-while: Executes at least once before checking the condition.
Example: do { x++; } while (x < 10);
Jump Statements:
break: Exits from loops or switch cases.
continue: Skips the current iteration of the loop.
return: Exits a function and optionally returns a value.
6. Header Files
Header files contain definitions for functions, constants, and macros.
Example: #include <stdio.h> for input/output functions.
Example: #include <math.h> for mathematical functions like sqrt().
7. Console-based I/O
printf(): For formatted output.
Example: printf("Value is %d", value);
scanf(): For formatted input.
Example: scanf("%d", &value);
These notes provide a concise overview of C programming concepts like operators, functions, I/O functions, preprocessor directives, and statements.
Here are some sample questions based on the concepts you've studied in C programming:
Q1: What will be the output of the following code?
int x = 5, y = 3;
printf("%d", x++ + ++y);
Answer: The output will be 9. The value of x++ is 5 (post-increment), and the value of ++y is 4 (pre-increment), so 5 + 4 = 9.
Q2: What is the difference between ++x and x++?
Q1: Write a C function that takes two integers as parameters and returns their sum.
Answer:
int sum(int a, int b) {
return a + b;
}
Q2: What is the difference between a function that returns void and a function that returns a value?
Q1: Write a program that takes user input for an integer, then prints that integer.
Answer:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("You entered: %d\n", num);
return 0;
}
Q2: What is the difference between getchar() and getch()?
Q1: What is the purpose of the #include directive in C?
Answer: The #include directive is used to include header files that provide function declarations, macros, and constants.
Q2: Write a program that uses the #define directive to define a constant for the value of Pi and then prints it.
Answer:
#include <stdio.h>
#define PI 3.14159
int main() {
printf("The value of Pi is: %f\n", PI);
return 0;
}
Q1: Write a program using if-else to check whether a given number is positive, negative, or zero.
Answer:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("Positive\n");
} else if (num < 0) {
printf("Negative\n");
} else {
printf("Zero\n");
}
return 0;
}
Q2: What is the purpose of the break statement in a loop?
Q1: How would you read a single character using getchar() and print it using putchar()?
Answer:
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
ch = getchar();
printf("You entered: ");
putchar(ch);
return 0;
}
These questions will help you reinforce your understanding of operators, functions, I/O functions, preprocessor directives, and statements in C. Let me know if you need further details!