C Programming
C Programming
o printf() is used to print the output to the standard output (usually the
console), while sprintf() is used to store the formatted output as a string.
2. What is the difference between malloc() and calloc()?
o Both malloc() and calloc() are used to dynamically allocate memory in C.
The difference lies in how they initialize the memory. malloc() allocates
uninitialized memory, while calloc() allocates memory and initializes all
bits to zero.
3. Explain the difference between char *ptr and char ptr[].
o char *ptr declares a pointer to a character, whereas char ptr[] declares an
array of characters.
4. What is the difference between ++i and i++?
o Both ++i and i++ increment the value of i, but ++i increments the value
before the current expression is evaluated, while i++ increments the value
after the current expression is evaluated.
5. What is the purpose of typedef keyword in C?
o The typedef keyword is used to create aliases for data types in C. It improves
code readability and makes it easier to change data types in the future.
6. How do you swap two variables without using a temporary variable?
o You can swap two variables without using a temporary variable using XOR
bitwise operator or arithmetic operations. For example:
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
7. What is a pointer in C?
o A pointer is a variable that stores the memory address of another variable. It
allows for dynamic memory allocation and manipulation of data structures.
8. Explain the const keyword in C.
o The const keyword is used to declare constants in C. It tells the compiler that
the value of the variable cannot be changed once it has been initialized.
9. What is the purpose of the #include directive in C?
o The #include directive is used to include the contents of another file (usually
header files) into the current file during compilation. It allows for modular
programming and reusability of code.
10. What is the output of the following code snippet?
#include <stdio.h>
int main() {
int x = 10;
int *ptr = &x;
printf("%d", *ptr);
return 0;
}
o This code snippet will output 10, as it prints the value pointed to by the pointer
ptr, which is the value of x.
1. Reverse a string in C.
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "hello";
int len = strlen(str);
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
printf("Reversed string: %s", str);
return 0;
}
#include <stdio.h>
int main() {
int arr[] = {3, 9, 2, 7, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
printf("Largest element: %d", max);
return 0;
}
#include <stdio.h>
int isPrime(int num) {
if (num <= 1) return 0; // Not prime
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) return 0; // Not prime
}
return 1; // Prime
}
int main() {
int num = 17;
if (isPrime(num)) {
printf("%d is prime.", num);
} else {
printf("%d is not prime.", num);
}
return 0;
}
4. Find factorial of a number.
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1) return 1;
else return n * factorial(n - 1);
}
int main() {
int num = 5;
printf("Factorial of %d is %d", num, factorial(num));
return 0;
}
#include <stdio.h>
int countWords(char str[]) {
int count = 1;
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == ' ' && str[i + 1] != ' ') {
count++;
}
}
return count;
}
int main() {
char str[] = "Hello world, welcome to coding!";
printf("Number of words: %d", countWords(str));
return 0;
}
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
printf("Sum of elements: %d", sum);
return 0;
}
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "madam";
int len = strlen(str);
int isPalindrome = 1;
for (int i = 0; i < len / 2; i++) {
if (str[i] != str[len - i - 1]) {
isPalindrome = 0;
break;
}
}
if (isPalindrome) {
printf("%s is a palindrome.", str);
} else {
printf("%s is not a palindrome.", str);
}
return 0;
}
#include <stdio.h>
int main() {
int n = 10;
int first = 0, second = 1, next;
printf("Fibonacci series: ");
for (int i = 0; i < n; i++) {
printf("%d, ", first);
next = first + second;
first = second;
second = next;
}
return 0;
}
#include <stdio.h>
int main() {
int n = 5;
int factorial = 1;
for (int i = 1; i <= n; i++) {
factorial *= i;
}
printf("Factorial of %d is %d", n, factorial);
return 0;
}
#include <stdio.h>
int main() {
char operator;
double num1, num2, result;
printf("Enter operator (+, -, *, /): ");
scanf("%c", &operator);
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
printf("Invalid operator");
return 1;
}
printf("Result: %.2lf", result);
return 0;
}