C Language Basics Quiz
8 Questions
1 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is an array in C language?

  • A fixed-size sequence of characters
  • A variable that holds a single value
  • An unordered set of values
  • A collection of data elements of the same type (correct)
  • Which operator in C language is used to perform logical AND?

  • !
  • &
  • && (correct)
  • ||
  • Which of the following is NOT a control statement in C language?

  • if statement
  • switch statement
  • for loop
  • define directive (correct)
  • What is the correct format specifier for a floating-point number in C?

    <p>%f</p> Signup and view all the answers

    In a do-while loop, what guarantees that the loop executes at least once?

    <p>The condition is evaluated after the loop body execution</p> Signup and view all the answers

    Which of the following correctly describes a switch statement?

    <p>It selects one of many code blocks to be executed based on a variable's value</p> Signup and view all the answers

    Which datatype in C language is used to store the largest integer value?

    <p>long</p> Signup and view all the answers

    What does the following statement do: 'continue'?

    <p>Skips the current iteration and moves to the next iteration</p> Signup and view all the answers

    Study Notes

    Computer Languages and Number Systems

    • Computer Languages are used to communicate with computers. They come in different levels:
      • Machine Language: Directly understood by computers, uses binary code (0s and 1s).
      • Assembly Language: Uses mnemonics for instructions, needs an assembler to translate to machine code.
      • High-Level Languages: Easier for humans to understand, closer to natural language, needs a compiler or interpreter to translate into machine code. Examples include C, Python, Java.
    • Number Systems are ways to represent numbers.
      • Decimal System (Base 10): Uses digits 0-9, each digit's position determines its value. Examples: 123 (100 + 20 + 3).
      • Binary System (Base 2): Uses digits 0 and 1, each digit's position determines its value (powers of 2). Examples: 101 (2² + 2⁰).
      • Octal System (Base 8): Uses digits 0-7, each digit's position determines its value (powers of 8). Examples: 123 (8² + 2 * 8¹ + 3 * 8⁰).
      • Hexadecimal System (Base 16): Uses digits 0-9 and letters A-F (A=10, B=11, etc.), each digit's position determines its value (powers of 16). Examples: 12AB (1 * 16³ + 2 * 16² + 10 * 16¹ + 11 * 16⁰).

    Operators in C Language

    • Arithmetic Operators perform mathematical operations:
      • + (addition), - (subtraction), * (multiplication), / (division), % (modulo - remainder after division).
    • Relational Operators compare values and return a boolean (true/false) result:
      • > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to), == (equal to), != (not equal to).
    • Logical Operators combine boolean expressions:
      • && (logical AND), || (logical OR), ! (logical NOT).
    • Bitwise Operators operate on individual bits of data:
      • ~ (bitwise NOT), & (bitwise AND), | (bitwise OR), ^ (bitwise XOR), << (left shift), >> (right shift).
    • Assignment Operators assign values to variables:
      • = (assignment), += (add and assign), -= (subtract and assign), *=, /=, %=, &=, |=, ^= (similar operations).
    • Increment and Decrement Operators increase or decrease the value of a variable by 1:
      • ++ (increment), -- (decrement).
    • Conditional Operator (ternary operator) provides a shorthand for if-else statements: condition ? expression1 : expression2.
    • Other Operators:
      • sizeof (returns the size of a variable or data type in bytes), & (address-of operator), * (dereference operator), . (member access operator), -> (pointer member access operator).

    Datatypes in C Language

    • Datatype determines the type of data a variable can store, and how it is stored in memory.
    • Basic Datatypes:
      • int: Stores whole numbers (integers). Size: 2 bytes (16 bits) or 4 bytes (32 bits) depending on the system. Range: -32768 to 32767 or -2147483648 to 2147483647. Format specifier: %d.
      • char: Stores single characters (letters, symbols, digits). Size: 1 byte (8 bits). Range: -128 to 127. Format specifier: %c.
      • float: Stores single-precision floating-point numbers (numbers with decimal points). Size: 4 bytes (32 bits). Range: approximately 3.4E-38 to 3.4E+38. Format specifier: %f.
      • double: Stores double-precision floating-point numbers (numbers with decimal points, more precision than float). Size: 8 bytes (64 bits). Range: approximately 1.7E-308 to 1.7E+308. Format specifier: %lf.
    • Derived Datatypes: Created using base datatypes.
      • Arrays: Collections of elements of the same datatype.
      • Pointers: Variables that store memory addresses.
      • Structures: User-defined datatypes that group variables of different datatypes under a single name.
      • Unions: Similar to structures but allow members to share the same memory location.
    • Void Datatype: Represents the absence of a type, often used for functions that don't return a value.

    Sum of Individual Digits and Syntax

    • Algorithm:
      • Input the number from the user.
      • Initialize a variable to store the sum (set it to 0).
      • Use a loop to iterate over the digits of the number.
      • Within the loop:
        • Find the last digit of the number using the modulo operator (%).
        • Add the last digit to the sum.
        • Remove the last digit from the number by dividing it by 10.
      • Print the sum of the digits.
    • C Program:
      #include <stdio.h>
      int main() {
      int num, sum = 0, digit;
      printf("Enter a number: ");
      scanf("%d", &num);
      while (num > 0) {
          digit = num % 10;
          sum += digit;
          num /= 10;
      }
      printf("Sum of digits: %d\n", sum);
      return 0;
      }
      
    • Syntax:
      • if Statement: Executes a block of code if a given condition is true.
        if (condition) {
        // code to execute if condition is true
        }
        
      • while Loop: Repeats a block of code as long as a given condition is true.
        while (condition) {
        // code to execute repeatedly
        } 
        
      • do-while Loop: Executes a block of code at least once, then repeats as long as a given condition is true.
        do {
        // code to execute
        } while (condition);
        

    Factorial and Switch Statements

    • Algorithm:
      • Input the number from the user.
      • Initialize a variable to store the factorial (set it to 1).
      • Use a loop to iterate from 1 to the number.
      • Within the loop, multiply the factorial by the current loop counter.
      • Print the calculated factorial.
    • C Program:
      #include <stdio.h>
      int main() {
      int num, i, factorial = 1;
      printf("Enter a number: ");
      scanf("%d", &num);
      for (i = 1; i <= num; i++) {
          factorial *= i;
      }
      printf("Factorial of %d is: %d\n", num, factorial);
      return 0;
      }
      
    • Syntax:
      • switch Statement: Executes a specific block of code based on the value of a variable.
        switch (expression) {
        case value1:
            // code to execute if expression matches value1
            break;
        case value2:
            // code to execute if expression matches value2
            break;
        default:
            // code to execute if no match is found
            break;
        }
        
      • nested if Statement: An if statement inside another if statement, used to evaluate multiple conditions.
        if (condition1) {
        // code to execute if condition1 is true
        if (condition2) {
            // code to execute if condition2 is also true
        }
        } 
        

    Finding the Biggest of Three Numbers

    • Algorithm:
      • Input three numbers from the user.
      • Compare the first two numbers.
      • If the first number is greater, compare it with the third number. Otherwise, compare the second number with the third number.
      • The largest number from these comparisons is the biggest number.
    • Flowchart:
      • [Insert flowchart image here]
    • C Program:
      #include <stdio.h>
      int main() {
      int num1, num2, num3, biggest;
      printf("Enter three numbers: ");
      scanf("%d %d %d", &num1, &num2, &num3);
      if (num1 > num2) {
          if (num1 > num3) {
              biggest = num1;
          } else {
              biggest = num3;
          }
      } else {
          if (num2 > num3) {
              biggest = num2;
          } else {
              biggest = num3;
          }
      }
      printf("Biggest number: %d\n", biggest);
      return 0;
      }
      

    Reverse of a Given Number and Differences between while and do-while

    • Algorithm:
      • Input the number from the user.
      • Initialize a variable to store the reversed number (set it to 0).
      • Use a loop to iterate over the digits of the number.
      • Within the loop:
        • Find the last digit of the number using the modulo operator (%).
        • Multiply the reversed number by 10 and add the last digit.
        • Remove the last digit from the number by dividing it by 10.
      • Print the reversed number.
    • C Program:
      #include <stdio.h>
      int main() {
      int num, reversed = 0, digit;
      printf("Enter a number: ");
      scanf("%d", &num);
      while (num > 0) {
          digit = num % 10;
          reversed = reversed * 10 + digit;
          num /= 10;
      }
      printf("Reversed number: %d\n", reversed);
      return 0;
      }
      
    • Differences between while and do-while Loops:
      • Execution Order: while loop checks the condition before executing the code, while do-while executes the code at least once before checking the condition.
      • Minimum Iterations: while loop may not execute at all if the condition is initially false. do-while loop will execute the code at least once.
      • Condition Check: while loop checks the condition after each iteration, while do-while checks it at the end of each iteration.
      • Use Cases: while loop is suitable for tasks where you need to repeat a block of code until a certain condition is met. do-while loop is useful for tasks that need to be executed at least once, regardless of the initial condition.
      • Example: In a program to read user input, a do-while loop is used to ensure that the user is prompted at least once to enter data, even if their initial input is invalid.

    Fibonacci Series and Even or Odd Check

    • Algorithm (Fibonacci Series):
      • Input the number N (upper limit) from the user.
      • Initialize two variables a and b to 0 and 1 respectively (first two Fibonacci numbers).
      • Use a loop to iterate until the calculated Fibonacci number exceeds N.
      • Within the loop:
        • Calculate the next Fibonacci number by adding a and b.
        • Print the current Fibonacci number.
        • Update a and b for the next iteration.
    • C Program (Fibonacci Series):
      #include <stdio.h>
      int main() {
      int N, a = 0, b = 1, nextTerm;
      printf("Enter the upper limit (N): ");
      scanf("%d", &N);
      printf("Fibonacci series up to %d:\n", N);
      while (a <= N) {
          printf("%d ", a);
          nextTerm = a + b;
          a = b;
          b = nextTerm;
      }
      printf("\n");
      return 0;
      }
      
    • Algorithm (Even or Odd Check):
      • Input the number from the user.
      • Use the modulo operator (%) to get the remainder of the number divided by 2.
      • If the remainder is 0, the number is even; if not, it's odd.
    • C Program (Even or Odd Check):
      #include <stdio.h>
      int main() {
      int num;
      printf("Enter a number: ");
      scanf("%d", &num);
      if (num % 2 == 0) {
          printf("%d is even\n", num);
      } else {
          printf("%d is odd\n", num);
      }
      return 0;
      }
      

    goto, continue, Checking Vowel

    • goto Statement: Unconditionally jumps to a labeled statement in the program.
      #include <stdio.h>
      int main() {
      int x = 10;
      goto label; // Unconditional jump to 'label'
      printf("This line won't be printed.\n");
      label:
          printf("This line will be printed.\n");
      return 0;
      }
      
    • continue Statement: Skips the remaining code in the current iteration of a loop and moves to the next iteration.
      #include <stdio.h>
      int main() {
      int i;
      for (i = 1; i <= 10; i++) {
          if (i == 5) {
              continue; // Skip to the next iteration when i is 5
          }
          printf("%d ", i);
      }
      return 0;
      }
      
    • Checking Vowel (C Program):
      #include <stdio.h>
      int main() {
      char ch;
      printf("Enter a character: ");
      scanf("%c", &ch);
      if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
          ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
          printf("%c is a vowel\n", ch);
      } else {
          printf("%c is not a vowel\n", ch);
      }
      return 0;
      }
      

    Control Statements Explained

    • Selection Statements: Choose which code block to execute based on a condition.
      • if Statement: Executes a block of code if a given condition is true.
      • if-else Statement: Executes one block of code if a given condition is true and another block if the condition is false.
      • if-else-if Statement: Evaluates multiple conditions and executes the code block corresponding to the first true condition.
    • Looping Statements: Repeat a block of code until a specific condition is met.
      • while Loop: Executes a block of code as long as a given condition is true. The condition is checked before each iteration.
      • do-while Loop: Executes a block of code at least once, then repeats as long as a given condition is true. The condition is checked after each iteration.
      • for Loop: Executes a block of code a specified number of times. It initializes a counter, checks a condition, and increments the counter after each iteration.
    • Jumping Statements: Modify the normal flow of execution.
      • break Statement: Exits the innermost loop or switch statement.
      • continue Statement: Skips the remaining code within the current iteration of a loop and moves on to the next iteration.
      • goto Statement: Unconditionally jumps to a labeled statement in the program. (Note: It is generally discouraged due to code readability issues and its potential to create difficult-to-debug code.)

    Arrays

    • Array: A collection of elements of the same data type, stored contiguously in memory. Elements are accessed using an index (starting from 0).
    • Syntax (1D Array):
      • Declaration: data_type array_name[size];
      • Initialization: data_type array_name[size] = {value1, value2, ..., valueN};
      • Accessing Elements: array_name[index]
    • Syntax (2D Array):
      • Declaration: data_type array_name[rows][columns];
      • Initialization: data_type array_name[rows][columns] = {{value11, value12, ..., value1N}, {value21, value22, ..., value2N}, ..., {valueM1, valueM2, ..., valueMN}};
      • Accessing Elements: array_name[row_index][column_index]
    • Syntax (Multidimensional Array): Extension of 2D arrays with more dimensions. Declaration and initialization follow similar patterns.
    • C Program (Finding Largest and Smallest Element):
      #include <stdio.h>
      int main() {
      int arr[100], n, i, largest, smallest;
      printf("Enter the number of elements: ");
      scanf("%d", &n);
      printf("Enter the elements: ");
      for (i = 0; i < n; i++) {
          scanf("%d", &arr[i]);
      }
      largest = arr[0];
      smallest = arr[0];
      for (i = 1; i < n; i++) {
          if (arr[i] > largest) {
              largest = arr[i];
          }
          if (arr[i] < smallest) {
              smallest = arr[i];
          }
      }
      printf("Largest element: %d\n", largest);
      printf("Smallest element: %d\n", smallest);
      return 0;
      }
      

    Matrix Operations

    • Adding Two Matrices:
      • Create two matrices of the same size.
      • Use nested loops to iterate through rows and columns of both matrices.
      • Add corresponding elements of the two matrices and store the result in a new matrix of the same size.
    • C Program (Matrix Addition):
      #include <stdio.h>
      int main() {
      int A[10][10], B[10][10], C[10][10], rows, cols, i, j;
      printf("Enter the number of rows and columns: ");
      scanf("%d %d", &rows, &cols);
      printf("Enter elements of matrix A:\n");
      for (i = 0; i < rows; i++) {
          for (j = 0; j < cols; j++) {
              scanf("%d", &A[i][j]);
          }
      }
      printf("Enter elements of matrix B:\n");
      for (i = 0; i < rows; i++) {
          for (j = 0; j < cols; j++) {
              scanf("%d", &B[i][j]);
          }
      }
      // Matrix addition
      for (i = 0; i < rows; i++) {
          for (j = 0; j < cols; j++) {
              C[i][j] = A[i][j] + B[i][j];
          }
      }
      printf("Resultant matrix C:\n");
      for (i = 0; i < rows; i++) {
          for (j = 0; j < cols; j++) {
              printf("%d ", C[i][j]);
          }
          printf("\n");
      }
      return 0;
      }
      
    • Multiplying Two Matrices:
      • Create two matrices where the number of columns of the first matrix is equal to the number of rows of the second matrix.
      • Use nested loops to iterate through rows and columns of the resulting matrix.
      • For each element of the resulting matrix, multiply the elements of the corresponding row of the first matrix with the elements of the corresponding column of the second matrix, and sum the products.
    • C Program (Matrix Multiplication):
      #include <stdio.h>
      int main() {
      int A[10][10], B[10][10], C[10][10], rows1, cols1, rows2, cols2, i, j, k;
      
      printf("Enter the number of rows and columns of matrix A: ");
      scanf("%d %d", &rows1, &cols1);
      printf("Enter elements of matrix A:\n");
      for (i = 0; i < rows1; i++) {
          for (j = 0; j < cols1; j++) {
              scanf("%d", &A[i][j]);
          }
      }
      printf("Enter the number of rows and columns of matrix B: ");
      scanf("%d %d", &rows2, &cols2);
      // Check if multiplication is valid:
      if (cols1 != rows2) {
          printf("Matrix multiplication is not possible.\n");
          return 0;
      } 
      printf("Enter elements of matrix B:\n");
      for (i = 0; i < rows2; i++) {
          for (j = 0; j < cols2; j++) {
              scanf("%d", &B[i][j]);
          }
      }
      // Matrix multiplication
      for (i = 0; i < rows1; i++) {
          for (j = 0; j < cols2; j++) {
              C[i][j] = 0;
              for (k = 0; k < cols1; k++) {
                  C[i][j] += A[i][k] * B[k][j];
              }
          }
      }
      printf("Resultant matrix C:\n");
      for (i = 0; i < rows1; i++) {
          for (j = 0; j < cols2; j++) {
              printf("%d ", C[i][j]);
          }
          printf("\n");
      }
      return 0;
      }
      

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    Description

    Test your knowledge on the fundamentals of C programming with this quiz. It covers arrays, operators, control statements, format specifiers, and loop mechanisms. Perfect for beginners looking to enhance their understanding of C language concepts.

    More Like This

    Python Programming Basics
    10 questions
    C Programming Basics Quiz
    8 questions

    C Programming Basics Quiz

    IrresistibleFrenchHorn avatar
    IrresistibleFrenchHorn
    Use Quizgecko on...
    Browser
    Browser