C Programming Concepts and Examples
What is an Expression and Operator? Discuss Operator Associativity in C. Write a program to find the factorial of a given integer using recursion.
In programming, an expression is a combination of variables, constants, and operators that yields a value. Operators are symbols that perform operations on operands (variables or values).
In C, operator associativity determines the order in which operators of the same precedence are evaluated in an expression. Operators can be left-associative (evaluated from left to right) or right-associative (evaluated from right to left).
For example, in the expression ‘a + b – c’, the ‘+’ and ‘-‘ operators have the same precedence, and they are left-associative. So, ‘a + b’ is evaluated first, then the result is subtracted by ‘c’.
C Program to Find Factorial Using Recursion
#include <stdio.h>
// Function to calculate factorial using recursion
unsigned long long factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n – 1);
}
int main() {
int num;
printf(“Enter a non-negative integer: “);
scanf(“%d”, &num);
if (num < 0) {
printf(“Factorial is not defined for negative numbers.\n”);
} else {
unsigned long long fact = factorial(num);
printf(“Factorial of %d is: %llu\n”, num, fact);
}
return 0;
}
What is an Identifier and Keyboard? Explain the rules for naming valid identifiers in C with examples. Write a program to convert a given string to lowercase without using any library functions.
In C programming, an identifier is a name given to a variable, function, or any other user-defined item. It’s essentially a label that helps identify and reference that particular entity in the code.
A keyboard is a hardware input device used to enter characters and commands into a computer.
Rules for Naming Valid Identifiers in C
1. It can consist of letters (both uppercase and lowercase), digits, and underscores.
2. The first character must be a letter or an underscore.
3. Identifiers are case-sensitive.
4. They cannot be a keyword (reserved word) in C.
Examples of Valid Identifiers
int myVariable;
float _myFloat;
char userName;
C Program to Convert a String to Lowercase
#include <stdio.h>
void toLower(char *str) {
int i = 0;
while (str[i] != ‘\0’) {
if (str[i] >= ‘A’ && str[i] <= ‘Z’) {
str[i] += 32; // converting uppercase to lowercase
}
i++;
}
}
int main() {
char input[100];
printf(“Enter a string: “);
scanf(“%[^ ]”, input); // Reading input until newline character
toLower(input); // Converting to lowercase
printf(“String in lowercase: %s\n”, input);
return 0;
}
What different data types are available in C along with their respective range? Write a program to check whether a given integer is a palindrome or not.
Data Types in C
1. char: Typically 1 byte, representing characters from -128 to 127 or 0 to 255 (if unsigned).
2. int: Usually 4 bytes on most systems, with a range of -2,147,483,648 to 2,147,483,647.
3. float: Typically 4 bytes, representing single-precision floating-point numbers.
4. double: Usually 8 bytes, representing double-precision floating-point numbers.
5. long: At least 4 bytes, with a range greater than or equal to that of int.
6. long long: At least 8 bytes, with a range greater than or equal to that of long.
C Program to Check for Palindrome Integer
#include <stdio.h>
// Function to check if a number is palindrome
int isPalindrome(int num) {
int reverse = 0, originalNum = num;
// Reversing the number
while (num > 0) {
int remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
// Checking if the reversed number is equal to the original number
if (originalNum == reverse) {
return 1; // Palindrome
} else {
return 0; // Not palindrome
}
}
int main() {
int num;
printf(“Enter a number: “);
scanf(“%d”, &num);
if (isPalindrome(num)) {
printf(“%d is a palindrome.\n”, num);
} else {
printf(“%d is not a palindrome.\n”, num);
}
return 0;
}
Write a program to create a file “duplicate” that contains the exact copy of file “original”.
( 5 marks)
Sure, here’s a simple Python program to achieve that:
def duplicate_file(original_file, duplicate_file):
try:
with open(original_file, ‘rb’) as original:
with open(duplicate_file, ‘wb’) as duplicate:
duplicate.write(original.read())
print(“File duplicated successfully!”)
except FileNotFoundError:
print(“File not found. Please check the file path.”)
# Example usage:
original_file = “original.txt”
duplicate_file = “duplicate.txt”
duplicate_file(original_file, dup
licate_file)
Explain any four input/output function used in C language with suitable examples. ( 5 marks)
Sure, here are four commonly used input/output functions in C language:
1.printf(): This function is used to print formatted output to the standard output (usually the console). It takes a format string followed by optional arguments. For example:
int num = 10;
printf(“The value of num is %d\n”, num)
2.scanf(): scanf() is used to read formatted input from the standard input (usually the keyboard). It takes format specifiers for the variables to be read as arguments. For example:
int num;
printf(“Enter a number: “);
scanf(“%d”, &num)
3.getchar(): This function is used to read a single character from the standard input (usually the keyboard). For example
char ch;
printf(“Enter a character: “);
ch = getchar()
4.puts(): puts() is used to print a string to the standard output followed by a newline character. For example
char str[] = “Hello, World!”;
puts(str)
int main() {
int arr[ ] = {9, 5, 7, 3, 1, 8};
int size = sizeof(arr) / sizeof(arr[0]);
int smallest = findSmallestElement(arr, size);
printf(“The smallest element in the array is: %d\n”, smallest);
return 0;
}
