In C programming, input and output (I/O) are fundamental for interacting with the user and displaying results. The standard library provides several functions for input and output operations. The most common ones are printf() for output and scanf() for input.
printf()The printf() function is used to print data to the screen (standard output). It can print variables, literals, and formatted text.
Syntax:
printf("format string", arguments);
format string: A string that specifies how the output should be formatted.arguments: The values to be printed.Example:
#include <stdio.h>
int main() {
int age = 25;
float height = 5.9;
printf("Age: %d, Height: %.1f\n", age, height);
return 0;
}
Here:
%d is used to print integers.%.1f is used to print floating-point numbers with one decimal place.scanf()The scanf() function is used to take input from the user. It reads formatted data from the standard input (keyboard).
Syntax:
scanf("format string", &variable);
format string: A string that specifies the type of data expected.&variable: The memory address of the variable where the input will be stored (the & symbol is used for this purpose).Example:
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num); // Reading an integer input
printf("You entered: %d\n", num);
return 0;
}
Here:
%d is used to read an integer.Note: Always ensure that the correct format specifier is used when reading inputs. For instance, %f is used for floating-point numbers, %c for characters, and %s for strings.
An arithmetic expression in C is a combination of constants, variables, operators, and parentheses that computes a value. The arithmetic operators in C include addition, subtraction, multiplication, division, and modulus.
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
#include <stdio.h>
int main() {
int a = 20, b = 10;
int result;
result = (a + b) * (a - b); // Arithmetic expression
printf("Result: %d\n", result);
return 0;
}
In this example, the arithmetic expression (a + b) * (a - b) involves addition, subtraction, and multiplication.
An assignment statement in C is used to assign a value to a variable. The assignment operator is =.
variable = expression;
variable: The variable to which a value is assigned.expression: The value or calculation that will be assigned to the variable.#include <stdio.h>
int main() {
int x = 10; // Assigning value 10 to variable x
int y;
y = x + 5; // Assigning the result of x + 5 to y
printf("x = %d, y = %d\n", x, y);
return 0;
}
In this example:
x = 10 assigns 10 to x.y = x + 5 calculates x + 5 and assigns the result (15) to y.C also provides compound assignment operators, which are shorthand for operations that involve an assignment. For example:
+= adds a value to the variable.-= subtracts a value from the variable.*= multiplies the variable by a value./= divides the variable by a value.%= takes the modulus and assigns the result.Example:
#include <stdio.h>
int main() {
int a = 5;
a += 3; // Equivalent to a = a + 3
printf("a = %d\n", a);
return 0;
}
Here, a += 3 increases a by 3, so the result will be a = 8.
Operator precedence defines the order in which operators are evaluated in an expression. In C, some operators have higher precedence than others, meaning they are evaluated first. Parentheses () are used to explicitly specify the order of evaluation.
Parentheses (): Used to group expressions and alter the natural precedence.
int result = (a + b) * c;
Unary Operators: Operators that work on a single operand, such as increment (++), decrement (--), and logical NOT (!).
int x = 5;
x++; // Increment x
Multiplication, Division, and Modulus (*, /, %): These operators have higher precedence than addition and subtraction.
int result = a * b + c; // Multiplies first, then adds
Addition and Subtraction (+, -): These have lower precedence than multiplication, division, and modulus.
int result = a + b - c; // Adds a and b first, then subtracts c
Relational Operators (<, >, <=, >=, ==, !=): These are used for comparisons and have lower precedence than arithmetic operators.
if (a > b) { // Compares a and b
printf("a is greater than b");
}
Logical Operators (&&, ||): These operators have the lowest precedence among the common operators.
if (a > b && b > c) { // Logical AND operator
printf("Condition is true");
}
#include <stdio.h>
int main() {
int a = 5, b = 10, c = 2;
int result = a + b * c; // Multiplication happens before addition
printf("Result: %d\n", result); // Output will be 25 (10 * 2 + 5)
return 0;
}
In this example, b * c is evaluated first due to its higher precedence, and then a is added to the result.
To alter the natural order, parentheses can be used to explicitly specify the desired order of operations:
#include <stdio.h>
int main() {
int a = 5, b = 10, c = 2;
int result = (a + b) * c; // Addition happens before multiplication
printf("Result: %d\n", result); // Output will be 30 ((5 + 10) * 2)
return 0;
}
printf() is used to display output, and scanf() is used to read input.+, -, *, /, and %.= operator is used to assign values to variables, and compound assignment operators like +=, -=, etc., can be used for shorthand.Understanding these concepts is crucial for writing clear and efficient programs in C.
Open this section to load past papers