C programming
C programming
manipulating individual bits of the operands. Examples include AND (&), OR (|), XOR (^), left
shift (<<), and right shift (>>).
Constants in C: Constants are fixed values that do not change during the execution of the
program. They are used to store values that remain constant. Constants can be of different
types, such as integer constants, floating-point constants, character constants, etc.
getchar() and putchar(): getchar(): A function that reads a single character from the
standard input (keyboard). Syntax: char ch = getchar(); It returns the next character input by
the user.
putchar(): A function that writes a single character to the standard output (screen). Syntax:
putchar(ch); It outputs the character ch to the console.
fscanf() is a function in C used to read formatted input from a file. It works like scanf() but
reads data from a file instead of standard input.
Syntax: int fscanf(FILE *stream, const char *format, ...);
Example: #include <stdio.h> int main() { FILE *file = fopen("data.txt", "r");
int num; fscanf(file, "%d", &num); // Reads a number from the file
printf("Number: %d\n", num); fclose(file); return 0; }
A storage class in C defines the scope, lifetime, and visibility of variables or functions. The
four types are:
auto: Default for local variables. register: Suggests storing the variable in a CPU register for
faster access. static: Preserves the value of a variable between function calls. extern: Allows
sharing a variable across multiple files.
A union in C is a user-defined data type that allows storing different types of data in the
same memory location. It shares memory for all its members, meaning only one member
can hold a value at a time.
Syntax: union UnionName { dataType1 member1;
dataType2 member2; // ... };
getch() is a function in C used to read a single character from the keyboard without echoing
it on the screen. Syntax: char getch(void);
Example: #include <stdio.h> int main() { char ch; printf("Enter a character: ");
ch = getch(); // Reads a single character without echoing it
printf("\nYou entered: %c\n", ch); return 0; }
A preprocessor in C is a tool that processes the code before the actual compilation starts. It
handles directives that are used to modify the code, such as including files, defining
constants, and conditional compilation.
Common Preprocessor Directives:
#define: Defines constants or macros. #include: Includes header files. #if, #else, #endif:
Conditional compilation. #undef: Undefines a macro. #ifdef, #ifndef: Checks if a macro is
defined or not.
1. break and continue statements in loops: break: Exits the loop immediately. continue:
Skips the current iteration and moves to the next one. Example: #include <stdio.h> int
main() {
for (int i = 1; i <= 10; i++) { if (i == 6) break; // Exit loop when i is 6
printf("%d ", i); } printf("\n"); for (int i = 1; i <= 10; i++) {
if (i == 6) continue; // Skip 6 printf("%d ", i); } return 0; }
Ternary Operator in C: The ternary operator is a shorthand for the if-else statement and
allows for concise conditional expressions. Syntax: condition ? expression_if_true :
expression_if_false; How it works: If the condition is true, the expression_if_true is
executed. If the condition is false, the expression_if_false is executed. It is
typically used for simple conditions where an if-else statement would be unnecessarily
lengthy. The ternary operator helps reduce the amount of code, making it more readable
and compact.
Q. Write a C program to print Fibonacci series for 'n' terms using recursion.
#include <stdio.h> int fibonacci(int n) { if (n <= 1) {
return n; } else { return fibonacci(n - 1) + fibonacci(n - 2); } }
int main() { int n; printf("Enter the number of terms: ");
scanf("%d", &n); printf("Fibonacci series: "); for (int i = 0; i < n; i++) {
printf("%d ", fibonacci(i)); } printf("\n"); return 0; }
Q. Write a C program to accept 60 students record in an array of structure with fields Roll
No., Name, Marks. Print student name who is having highest marks.
#include <stdio.h> #include <string.h> struct Student {
int rollNo; char name[50]; float marks; };
int main() { struct Student students[60]; int i; int highestIndex = 0;
printf("Enter details of 60 students:\n"); for (i = 0; i < 60; i++) {
printf("Student %d:\n", i + 1); printf("Roll No: ");
scanf("%d", &students[i].rollNo); printf("Name: ");
scanf(" %[^\n]%*c", students[i].name); printf("Marks: ");
scanf("%f", &students[i].marks); printf("\n"); }
for (i = 1; i < 60; i++) { if (students[i].marks > students[highestIndex].marks) {
highestIndex = i; } } printf("Student with highest marks:\n");
printf("Name: %s\n", students[highestIndex].name); printf("Roll No: %d\n",
students[highestIndex].rollNo); printf("Marks: %.2f\n", students[highestIndex].marks);
return 0; }
Q. Write a C program to count and display total number of alphabets, numeric characters,
special characters, number of words and number of lines in a file.
#include <stdio.h> #include <ctype.h> int main() { FILE *file; char ch;
int alphabets = 0, digits = 0, specialChars = 0, words = 0, lines = 0; int inWord = 0;
file = fopen("file.txt", "r"); if (file == NULL) { printf("Could not open file.\n"); return
1; }
while ((ch = fgetc(file)) != EOF) { if (isalpha(ch)) { alphabets++; }
else if (isdigit(ch)) { digits++; } else if (!isspace(ch)) { specialChars++; }
if (isspace(ch) || ch == '\n' || ch == '\t') { if (inWord) { words++; inWord = 0; }
} else { inWord = 1; } if (ch == '\n') { lines++; } } if (inWord) { words++; }
fclose(file); printf("Total alphabets: %d\n", alphabets);
printf("Total numeric characters: %d\n", digits); printf("Total special characters: %d\n",
specialChars);
printf("Total words: %d\n", words); printf("Total lines: %d\n", lines); return 0; }
Q. Write a C program to store n elements in an array and print the elements using pointer.
#include <stdio.h> int main() { int n, i; printf("Enter the number of elements: ");
scanf("%d", &n); int arr[n]; printf("Enter %d elements:\n", n); for (i = 0; i < n; i++) {
scanf("%d", &arr[i]); } printf("Elements in the array are:\n"); int *ptr = arr;
for (i = 0; i < n; i++) { printf("%d ", *(ptr + i)); } return 0; }
Q. Write a C program to declare two dimension array 4x3, accept element for the same.
give explanation too.
#include <stdio.h> int main() { int arr[4][3]; int i, j;
printf("Enter elements for the 4x3 array:\n"); for (i = 0; i < 4; i++) {
for (j = 0; j < 3; j++) { printf("Enter element for arr[%d][%d]: ", i, j);
scanf("%d", &arr[i][j]); } }
printf("\nElements of the 4x3 array are:\n"); for (i = 0; i < 4; i++) {
for (j = 0; j < 3; j++) { printf("%d ", arr[i][j]); } printf("\n"); } return 0; }
Explanation: Array Declaration: The statement int arr[4][3]; declares a two-dimensional
array arr with 4 rows and 3 columns. This means the array can store 12 elements in total.
Q. Write a C program to find the square of any number using the function.
#include <stdio.h> int square(int num) { return num * num; }
int main() { int number, result; printf("Enter a number: ");
scanf("%d", &number); result = square(number);
printf("The square of %d is %d\n", number, result); return 0; }
Q. Write a C program to enter the string, display and count the length of string.
#include <stdio.h> #include <string.h> int main() {
char str[100]; int length; printf("Enter a string: "); fgets(str, sizeof(str), stdin);
printf("You entered: %s", str); length = strlen(str) - 1;
printf("Length of the string is: %d\n", length); return 0; }
Q. Write a C program to enter elements of 4x4 matrix, display matrix and count the sum of
all elements.
#include <stdio.h> int main() { int matrix[4][4];
int i, j, sum = 0; printf("Enter the elements of the 4x4 matrix:\n");
for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) {
printf("Enter element at matrix[%d][%d]: ", i, j); scanf("%d", &matrix[i][j]); } }
printf("\nThe entered 4x4 matrix is:\n"); for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) { printf("%d ", matrix[i][j]); } printf("\n"); }
for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) { sum += matrix[i][j]; } }
printf("\nSum of all elements in the matrix is: %d\n", sum); return 0; }
Q. Write a C program to accept string from the console and count number of characters,
words, special characters, and spaces in the entered string.
#include <stdio.h> #include <ctype.h> int main() { char str[100];
int i, characters = 0, words = 0, spaces = 0, special_characters = 0;
int in_word = 0; printf("Enter a string: "); fgets(str, sizeof(str), stdin);
for (i = 0; str[i] != '\0'; i++) { if (isalpha(str[i])) { characters++; in_word = 1;
} else if (isdigit(str[i])) { characters++; in_word = 1; } else if (isspace(str[i])) {
spaces++; if (in_word) { words++; in_word = 0; } } else { special_characters++;
} } if (in_word) { words++; } printf("\nTotal characters: %d\n", characters);
printf("Total words: %d\n", words); printf("Total spaces: %d\n", spaces);
printf("Total special characters: %d\n", special_characters); return 0; }
Q. Write a C program to make a simple calculator by using switch statement minimum five
basic arithmetic operations.
#include <stdio.h> int main() { float num1, num2, result; int choice;
printf("Simple Calculator\n"); printf("Select an operation:\n");
printf("1. Addition\n"); printf("2. Subtraction\n"); printf("3. Multiplication\n");
printf("4. Division\n"); printf("5. Modulo (Remainder)\n"); printf("Enter your choice (1-
5): ");
scanf("%d", &choice); printf("Enter two numbers: "); scanf("%f %f", &num1, &num2);
switch(choice) { case 1: result = num1 + num2; printf("Result: %.2f\n", result); break;
case 2: result = num1 - num2; printf("Result: %.2f\n", result); break;
case 3: result = num1 * num2; printf("Result: %.2f\n", result); break;
case 4: if(num2 != 0) { result = num1 / num2; printf("Result: %.2f\n", result);
} else { printf("Error: Division by zero is not allowed.\n"); } break;
case 5: if((int)num2 != 0) { result = (int)num1 % (int)num2;
printf("Result: %.0f\n", result); } else { printf("Error: Modulo by zero is not allowed.\
n"); }
break; default: printf("Invalid choice. Please select a valid option between 1 and 5.\
n");
} return 0; }