Lecture Notes Of Class 1: Introduction to C Programming

 

Lecture Notes Of Class 1: Introduction to C Programming

Objective:

This class aims to introduce students to the C programming language, its structure, compilers, and Integrated Development Environments (IDEs). Students will gain an understanding of how to write and execute basic C programs.


Theory:

1. What is C Programming?

C is a powerful, general-purpose, and high-level programming language developed by Dennis Ritchie in 1972. It was initially designed for system programming and operating systems, especially the UNIX operating system. Over the years, C has become one of the most widely used programming languages due to its efficiency, flexibility, and wide application in developing software for a variety of platforms.

C allows for both low-level memory manipulation (hardware interaction) and high-level logic, making it suitable for system software, application software, and embedded systems development.


2. Structure of a C Program

A C program has a well-defined structure that needs to be followed in order for it to execute correctly. Here's a breakdown of the basic structure of a C program:

c

CopyEdit

#include <stdio.h> // Preprocessor directive

 

// Main function

int main() {

    // Code inside the main function

    printf("Hello, World!"); // Prints text to the screen

    return 0; // Exit the program

}

Let's go over each component:

1.  Preprocessor Directives:

o    The #include <stdio.h> is a preprocessor directive that tells the compiler to include the Standard Input Output (I/O) library. This library is needed to use functions like printf() and scanf() for input and output operations.

2.  Main Function:

o    Every C program has a main() function. This is where the execution of the program starts. When a program is run, the operating system calls the main() function first.

o    The int before main() indicates that the function will return an integer value (in this case, 0 signifies successful execution).

3.  Code Inside the Main Function:

o    The code inside {} (curly braces) is the body of the main() function. This is where the actual program logic is written.

o    In the example, the statement printf("Hello, World!"); is used to output text to the screen.

4.  Return Statement:

o    The return 0; statement at the end of the main() function is used to indicate the successful termination of the program to the operating system.


3. Compilers: What are They?

A compiler is a software tool that translates the source code written in a high-level programming language (like C) into machine code (binary code) that the computer can execute. The process of translating the source code is called compilation.

When you write a C program, you write in human-readable form (i.e., in C syntax). The compiler takes this code and converts it into a format that the computer can understand and execute.

Steps in Compilation:

1.  Preprocessing: Handles directives like #include, #define, etc.

2.  Compiling: Converts the code into assembly language.

3.  Assembling: Converts the assembly code into machine language (binary code).

4.  Linking: Combines object code and libraries into a complete executable program.

Common C compilers include:

  • GCC (GNU Compiler Collection)
  • Turbo C (older but still in use)
  • MinGW (Minimalist GNU for Windows)

4. IDEs: Integrated Development Environments

An IDE (Integrated Development Environment) is a software application that provides tools and features to help programmers write, test, and debug their programs efficiently. For C programming, IDEs make it easier to work with source code by offering features like syntax highlighting, auto-completion, debugging, and compiling within a single platform.

Some popular IDEs for C programming include:

  • Code::Blocks: A free, open-source C/C++ IDE that provides an easy-to-use interface and built-in compiler.
  • Dev C++: A simple and user-friendly IDE for C and C++ programming.
  • Eclipse: Although initially designed for Java, Eclipse also supports C programming with the help of plugins.
  • Visual Studio: A powerful IDE for many languages, including C, with advanced features like debugging tools, IntelliSense, and more.

5. How to Write, Compile, and Run a C Program

Steps to create and run a C program:

1.  Writing the Program:

o    Open your IDE or a text editor (such as Notepad++) and write your C code.

o    Save the file with a .c extension, such as hello.c.

2.  Compiling the Program:

o    If using an IDE, you can typically click on a "Build" or "Compile" button to compile the code.

o    If you're using a terminal or command line, navigate to the folder where the C file is saved and use a command like:

CopyEdit

gcc hello.c -o hello

This will create an executable file named hello.

3.  Running the Program:

o    After compiling, you can run the program by typing:

bash

CopyEdit

./hello

This will output the result of the program, in this case, printing Hello, World! to the screen.


Conclusion:

In this class, we introduced the C programming language, the basic structure of a C program, the role of a compiler, and how to write, compile, and run a simple C program. Understanding this foundational knowledge is essential as we move forward into more complex C programming concepts.

In the next class, we will dive deeper into data types, variables, and operators in C, allowing us to build more complex and useful programs.

4o mini

You said:

Write 10 assignments with solution and explanation in detail on above concept

ChatGPT said:

ChatGPT

Assignments on Introduction to C Programming

These assignments are designed to help students practice the fundamental concepts of C programming such as program structure, writing basic C programs, and compiling and executing code.


Assignment 1: Hello World Program

Objective: Write a C program that prints "Hello, World!" to the console.

Solution:

c

CopyEdit

#include <stdio.h> // Preprocessor directive to include the standard input-output library

 

int main() {

    printf("Hello, World!"); // Prints the message to the console

    return 0; // Indicates successful execution

}

Explanation:

  • The #include <stdio.h> is a preprocessor directive that includes the standard input-output library.
  • The main() function is where the program starts execution.
  • printf("Hello, World!"); is used to print text to the screen.
  • return 0; indicates successful execution.

Assignment 2: Simple Addition Program

Objective: Write a C program that takes two numbers from the user and prints their sum.

Solution:

c

CopyEdit

#include <stdio.h>

 

int main() {

    int num1, num2, sum;

   

    printf("Enter first number: ");

    scanf("%d", &num1); // Reads the first number from the user

   

    printf("Enter second number: ");

    scanf("%d", &num2); // Reads the second number from the user

   

    sum = num1 + num2; // Adds the two numbers

   

    printf("The sum of %d and %d is: %d", num1, num2, sum); // Prints the sum

   

    return 0;

}

Explanation:

  • scanf("%d", &num1); and scanf("%d", &num2); are used to take user input.
  • The sum of the two numbers is stored in the sum variable.
  • printf("The sum of %d and %d is: %d", num1, num2, sum); displays the result.

Assignment 3: Program to Find the Larger of Two Numbers

Objective: Write a C program that takes two numbers and prints the larger of the two.

Solution:

c

CopyEdit

#include <stdio.h>

 

int main() {

    int num1, num2;

   

    printf("Enter first number: ");

    scanf("%d", &num1);

   

    printf("Enter second number: ");

    scanf("%d", &num2);

   

    if (num1 > num2) {

        printf("The larger number is: %d", num1);

    } else {

        printf("The larger number is: %d", num2);

    }

   

    return 0;

}

Explanation:

  • The if statement compares the two numbers. If num1 is greater than num2, it prints num1; otherwise, it prints num2.

Assignment 4: Program to Calculate the Factorial of a Number

Objective: Write a C program that calculates the factorial of a given number.

Solution:

c

CopyEdit

#include <stdio.h>

 

int main() {

    int num, i;

    unsigned long long factorial = 1;

   

    printf("Enter a number: ");

    scanf("%d", &num);

   

    for (i = 1; i <= num; ++i) {

        factorial *= i; // Multiplies the factorial by i

    }

   

    printf("Factorial of %d is %llu", num, factorial);

   

    return 0;

}

Explanation:

  • The for loop runs from 1 to the input number num, multiplying each value of i with the current factorial.
  • The result is stored in the factorial variable and printed.

Assignment 5: Program to Check Whether a Number is Even or Odd

Objective: Write a C program to check whether a given number is even or odd.

Solution:

c

CopyEdit

#include <stdio.h>

 

int main() {

    int num;

   

    printf("Enter a number: ");

    scanf("%d", &num);

   

    if (num % 2 == 0) {

        printf("%d is even.", num);

    } else {

        printf("%d is odd.", num);

    }

   

    return 0;

}

Explanation:

  • The if statement uses the modulus operator (%) to check if the number is divisible by 2. If the result is 0, it is even; otherwise, it is odd.

Assignment 6: Program to Calculate the Area of a Circle

Objective: Write a C program to calculate the area of a circle based on the radius provided by the user.

Solution:

c

CopyEdit

#include <stdio.h>

#define PI 3.14159 // Defining the value of Pi

 

int main() {

    float radius, area;

   

    printf("Enter the radius of the circle: ");

    scanf("%f", &radius);

   

    area = PI * radius * radius; // Formula for area of a circle

   

    printf("The area of the circle with radius %.2f is: %.2f", radius, area);

   

    return 0;

}

Explanation:

  • #define PI 3.14159 defines a constant for the value of Pi.
  • The area is calculated using the formula area = PI * radius * radius;.

Assignment 7: Program to Calculate Simple Interest

Objective: Write a C program to calculate the simple interest for a given principal amount, rate of interest, and time period.

Solution:

c

CopyEdit

#include <stdio.h>

 

int main() {

    float principal, rate, time, interest;

   

    printf("Enter principal amount: ");

    scanf("%f", &principal);

   

    printf("Enter rate of interest: ");

    scanf("%f", &rate);

   

    printf("Enter time period in years: ");

    scanf("%f", &time);

   

    interest = (principal * rate * time) / 100; // Formula for simple interest

   

    printf("The simple interest is: %.2f", interest);

   

    return 0;

}

Explanation:

  • The formula for simple interest is SI = (P * R * T) / 100, where P is the principal, R is the rate, and T is the time.

Assignment 8: Program to Reverse a Number

Objective: Write a C program to reverse a given number.

Solution:

c

CopyEdit

#include <stdio.h>

 

int main() {

    int num, reversed = 0, remainder;

   

    printf("Enter a number: ");

    scanf("%d", &num);

   

    while (num != 0) {

        remainder = num % 10; // Get the last digit

        reversed = reversed * 10 + remainder; // Build the reversed number

        num /= 10; // Remove the last digit

    }

   

    printf("Reversed number is: %d", reversed);

   

    return 0;

}

Explanation:

  • The while loop extracts each digit from the number, starting from the last digit, and builds the reversed number by multiplying the current reversed by 10 and adding the extracted digit.

Assignment 9: Program to Find Prime Numbers Between Two Numbers

Objective: Write a C program that prints all the prime numbers between two given numbers.

Solution:

c

CopyEdit

#include <stdio.h>

 

int main() {

    int start, end, i, j, isPrime;

   

    printf("Enter the start number: ");

    scanf("%d", &start);

   

    printf("Enter the end number: ");

    scanf("%d", &end);

   

    printf("Prime numbers between %d and %d are:\n", start, end);

   

    for (i = start; i <= end; i++) {

        isPrime = 1;

       

        for (j = 2; j <= i / 2; j++) {

            if (i % j == 0) {

                isPrime = 0; // i is not prime

                break;

            }

        }

       

        if (isPrime == 1 && i > 1) {

            printf("%d ", i);

        }

    }

   

    return 0;

}

Explanation:

  • The outer loop iterates over each number between the given range.
  • The inner loop checks if a number is divisible by any number other than 1 and itself. If divisible, the number is not prime.

Assignment 10: Program to Calculate the Fibonacci Series

Objective: Write a C program that prints the Fibonacci series up to a given term.

Solution:

c

CopyEdit

#include <stdio.h>

 

int main() {

    int n, i;

    int first = 0, second = 1, next;

   

    printf("Enter the number of terms: ");

    scanf("%d", &n);

   

    printf("Fibonacci Series: ");

   

    for (i = 0; i < n; i++) {

        if (i <= 1) {

            next = i; // First two terms are 0 and 1

        } else {

            next = first + second; // Subsequent terms are sum of the previous two

            first = second;

            second = next;

        }

       

        printf("%d ", next);

    }

   

    return 0;

}

Explanation:

  • The first two terms of the Fibonacci series are 0 and 1. Each subsequent term is the sum of the previous two terms.
  • The loop continues until the specified number of terms is printed.

These assignments aim to build a strong foundation in C programming and familiarize students with essential programming concepts.

4o mini

You said:

write 100 MCQs for the concept above with explanation

ChatGPT said:

ChatGPT

Here is a set of 100 multiple-choice questions (MCQs) related to the concepts covered in the introduction to C programming. Each question includes an explanation to help understand the correct answers.

1. What is the correct syntax for a C program?

A) int main() { printf("Hello, World!"); return 0; }
B) main() { printf("Hello, World!"); return 0; }
C) void main() { printf("Hello, World!"); return 0; }
D) main() { printf("Hello, World!"); }

Answer: A
Explanation: The correct syntax requires int main() to define the main function, followed by return 0; indicating successful execution.


2. Which header file is required for input/output operations in C?

A) #include <iostream>
B) #include <stdio.h>
C) #include <stdlib.h>
D) #include <math.h>

Answer: B
Explanation: The #include <stdio.h> header file is required for input and output operations such as printf() and scanf().


3. What is the value of x after the execution of the following code?

c

CopyEdit

int x = 5;

x = x + 3;

A) 3
B) 5
C) 8
D) 15

Answer: C
Explanation: x = x + 3; adds 3 to the initial value of x (which is 5), so x becomes 8.


4. Which function is used to read input in C?

A) write()
B) scanf()
C) getchar()
D) input()

Answer: B
Explanation: The scanf() function is used to read input from the user.


5. What does the return 0; statement do in the main function?

A) Ends the program execution
B) Exits the function with a value of 0
C) Declares a function
D) Indicates an error

Answer: B
Explanation: return 0; indicates the successful termination of the program with a return value of 0.


6. What is the correct syntax for a comment in C?

A) // comment
B) /* comment */
C) <!-- comment -->
D) Both A and B

Answer: D
Explanation: Both // comment (single-line comment) and /* comment */ (multi-line comment) are valid comment syntaxes in C.


7. What is the default value of an uninitialized local variable in C?

A) 0
B) NULL
C) Undefined
D) Garbage value

Answer: C
Explanation: The value of an uninitialized local variable is undefined and could contain a garbage value.


8. Which operator is used to divide two integers in C?

A) +
B) -
C) /
D) %

Answer: C
Explanation: The / operator is used to divide two integers in C.


9. How is a string defined in C?

A) char str[] = "Hello";
B) char str[5] = "Hello";
C) string str = "Hello";
D) char str = "Hello";

Answer: A
Explanation: In C, a string is defined as an array of characters with a null terminator (\0) at the end.


10. What is the purpose of the #include directive in C?

A) To include a source file
B) To include a function
C) To include a library or header file
D) To define a constant

Answer: C
Explanation: The #include directive is used to include a library or header file in the C program, allowing the use of functions and macros defined in that file.


11. Which of the following is not a valid variable name in C?

A) int number;
B) float 2value;
C) char name;
D) double value123;

Answer: B
Explanation: Variable names cannot begin with a digit, so 2value is an invalid variable name.


12. Which of the following is the correct way to declare a constant in C?

A) const int x = 10;
B) int const x = 10;
C) #define x 10
D) All of the above

Answer: D
Explanation: All of the above methods are valid for declaring constants in C. The const keyword, #define, and using int const are all valid ways to define a constant.


13. What is the output of the following code?

c

CopyEdit

int a = 10;

int b = 5;

printf("%d", a / b);

A) 2
B) 5
C) 10
D) 15

Answer: A
Explanation: The division of 10 by 5 gives 2, which is printed as the output.


14. What is the result of the following code?

c

CopyEdit

int x = 5;

x += 2;

A) 2
B) 5
C) 7
D) 10

Answer: C
Explanation: The += operator adds 2 to the value of x, so x becomes 7.


15. What is the size of the int data type in C on most systems?

A) 4 bytes
B) 2 bytes
C) 8 bytes
D) 1 byte

Answer: A
Explanation: On most systems, the size of an int data type is 4 bytes.


16. Which of the following is used to check whether a number is prime in C?

A) Loop through all numbers less than the number and check divisibility
B) Check divisibility only by 2
C) Use the sqrt() function
D) Use the prime() function

Answer: A
Explanation: A prime number is only divisible by 1 and itself, so a loop is used to check divisibility by numbers less than the given number.


17. Which of the following is a correct statement regarding the for loop in C?

A) It executes a block of code at least once
B) It can only be used for iterating over arrays
C) It consists of three parts: initialization, condition, and update
D) It does not need a condition

Answer: C
Explanation: A for loop in C has three parts: initialization, condition, and update, all of which control the loop's execution.


18. What is the output of the following code?

c

CopyEdit

int x = 3, y = 4;

printf("%d", x > y);

A) 0
B) 1
C) 3
D) 4

Answer: A
Explanation: x > y evaluates to false because 3 is not greater than 4, and false is represented as 0 in C.


19. Which of the following statements is true about arrays in C?

A) Arrays can hold elements of different data types
B) Array indices start from 1
C) Arrays in C are of fixed size
D) Arrays are dynamic in C

Answer: C
Explanation: In C, arrays are of fixed size and can only hold elements of the same data type.


20. Which of the following is a valid declaration for a character array in C?

A) char arr[] = "Hello";
B) char arr[5] = "Hello";
C) string arr[] = "Hello";
D) char arr = "Hello";

Answer: A
Explanation: char arr[] = "Hello"; correctly declares a character array and initializes it with a string literal, which automatically includes a null terminator.


21. Which data type is used for storing decimal numbers in C?

A) int
B) float
C) char
D) double

Answer: B
Explanation: The float data type is used to store decimal (floating-point) numbers in C.


22. What is the result of the following code?

c

CopyEdit

int a = 5, b = 3;

printf("%d", a % b);

A) 2
B) 1
C) 0
D) 3

Answer: A
Explanation: The modulus operator (%) gives the remainder when a is divided by b. The remainder of 5 divided by 3 is 2.


23. How do you declare a function in C?

A) void functionName();
B) function void functionName();
C) void functionName[];
D) function void functionName[]();

Answer: A
Explanation: The correct syntax for declaring a function in C is void functionName();, where void is the return type.


24. What is the correct way to declare a pointer in C?

A) int *p;
B) pointer int p;
C) int p*;
D) int p&;

Answer: A
Explanation: In C, pointers are declared by placing the * symbol before the pointer variable, e.g., int *p;.


25. Which keyword is used to allocate memory dynamically in C?

A) alloc
B) malloc
C) new
D) allocate

Answer: B
Explanation: The malloc() function is used to dynamically allocate memory in C.


26. Which of the following is a valid way to handle errors in C?

A) Using try-catch block
B) Using assert()
C) Using error()
D) None of the above

Answer: B
Explanation: In C, the assert() macro is used to handle errors and check conditions.


(To be continued in the next response due to length)

4o mini

You said:

yes

ChatGPT said:

ChatGPT

Here are more MCQs to continue from the previous list:


27. Which function in C is used to deallocate memory allocated using malloc()?

A) free()
B) delete()
C) deallocate()
D) dispose()

Answer: A
Explanation: The free() function is used to deallocate memory that was previously allocated using malloc().


28. What is the output of the following code?

c

CopyEdit

int x = 2, y = 3;

x *= y;

printf("%d", x);

A) 2
B) 3
C) 5
D) 6

Answer: D
Explanation: The *= operator multiplies x by y, so x becomes 2 * 3 = 6.


29. What is the correct syntax for defining a function that returns an integer and takes two integer parameters?

A) int function(int a, int b);
B) int function(a, b) int a, int b;
C) function int(int a, int b);
D) int function(int, int);

Answer: A
Explanation: The correct syntax to define a function with two integer parameters is int function(int a, int b);.


30. Which operator is used for logical AND in C?

A) &&
B) &
C) ||
D) |

Answer: A
Explanation: The && operator is used for logical AND in C.


31. What is the correct format specifier to print a float value in C?

A) %f
B) %d
C) %lf
D) %lf for double and %f for float

Answer: A
Explanation: The correct format specifier to print a float value is %f.


32. What is the correct way to declare a structure in C?

A) struct person { int age; char name[20]; };
B) structure person { int age; char name[20]; };
C) struct person (int age, char name[20]);
D) person { struct int age; struct char name[20]; };

Answer: A
Explanation: The correct syntax for declaring a structure in C is struct person { int age; char name[20]; };.


33. What is the purpose of the break statement in C?

A) To continue the next iteration of the loop
B) To exit from the current function
C) To exit from the current loop or switch statement
D) To skip the current iteration of the loop

Answer: C
Explanation: The break statement is used to exit from the current loop or switch statement.


34. Which of the following is true about NULL in C?

A) It is a keyword used to define an empty string.
B) It is used to initialize integer variables.
C) It is a constant that represents a null pointer.
D) It is used to assign zero to integers.

Answer: C
Explanation: NULL is a constant used to represent a null pointer, which is typically used to indicate that a pointer is not pointing to any valid memory.


35. What is the output of the following code?

c

CopyEdit

int x = 10, y = 20;

if(x > y)

    printf("Yes");

else

    printf("No");

A) Yes
B) No
C) 10
D) 20

Answer: B
Explanation: Since x is not greater than y, the else block is executed, and "No" is printed.


36. Which of the following is a valid identifier in C?

A) int 2value;
B) float my_value;
C) char return;
D) int double;

Answer: B
Explanation: my_value is a valid identifier as it starts with a letter or underscore. Identifiers cannot start with a number and cannot use C keywords like return or double.


37. What does the sizeof() operator do in C?

A) It returns the size of the memory allocated to a pointer
B) It returns the size of a data type or variable
C) It returns the length of a string
D) It calculates the number of elements in an array

Answer: B
Explanation: The sizeof() operator is used to return the size (in bytes) of a data type or variable.


38. Which of the following is the correct way to initialize a variable with a value in C?

A) int x = 10;
B) int x == 10;
C) x = 10;
D) int x : 10;

Answer: A
Explanation: The correct syntax for initializing a variable in C is int x = 10;.


39. What is the output of the following code?

c

CopyEdit

int x = 3, y = 4;

x = x + y;

printf("%d", x);

A) 3
B) 4
C) 7
D) 12

Answer: C
Explanation: The value of x is updated to x + y which is 3 + 4 = 7.


40. What is the correct syntax for a while loop in C?

A) while (condition) { statements }
B) while condition { statements }
C) while (statements) { condition }
D) while condition; statements

Answer: A
Explanation: The correct syntax for a while loop is while (condition) { statements }.


41. What will happen if the return statement is omitted from the main() function in C?

A) The program will terminate with an error
B) The program will compile but may give undefined behavior
C) The program will return 0 by default
D) The program will run infinitely

Answer: C
Explanation: If the return statement is omitted from the main() function, the program will return 0 by default in C.


42. What is the result of the following code?

c

CopyEdit

int a = 7, b = 4;

printf("%d", a / b);

A) 1
B) 1.75
C) 2
D) 3

Answer: A
Explanation: Since both a and b are integers, the division will truncate the decimal part, and the result will be 1.


43. Which data type is used to store a single character in C?

A) char
B) int
C) float
D) double

Answer: A
Explanation: The char data type is used to store a single character in C.


44. What will be the value of a after the following code?

c

CopyEdit

int a = 10;

a++;

A) 9
B) 10
C) 11
D) Undefined

Answer: C
Explanation: The ++ operator increments the value of a by 1, so a becomes 11.


45. Which function is used to compare two strings in C?

A) strcmp()
B) stringCompare()
C) compare()
D) strCompare()

Answer: A
Explanation: The strcmp() function is used to compare two strings in C.


46. What is the correct way to define a constant integer in C?

A) const int x = 100;
B) int const x = 100;
C) #define x 100
D) All of the above

Answer: D
Explanation: All three methods are valid ways to define a constant integer in C.


47. What is the result of the following code?

c

CopyEdit

int x = 5;

x *= 3;

A) 5
B) 8
C) 15
D) 10

Answer: C
Explanation: The *= operator multiplies x by 3, so x becomes 15.


48. Which of the following is used to declare a function prototype in C?

A) int functionName();
B) void functionName(void);
C) int functionName(int, int);
D) All of the above

Answer: D
Explanation: All of the options are valid ways to declare function prototypes in C.


49. Which of the following operators is used to check equality in C?

A) =
B) ==
C) ===
D) ==`

Answer: B
Explanation: The == operator is used to check equality in C.


50. What will be the output of the following code?

c

CopyEdit

int x = 4, y = 2;

if(x > y)

    printf("True");

else

    printf("False");

A) True
B) False
C) 4
D) 2

Answer: A
Explanation: Since x is greater than y, "True" will be printed.


Let me know if you need more MCQs!

4o mini

You said:

yes...more 50

ChatGPT said:

ChatGPT

Here are the next 50 MCQs to continue:


51. Which of the following is used to terminate a for loop in C?

A) exit()
B) break
C) return
D) continue

Answer: B
Explanation: The break statement is used to terminate a loop prematurely in C.


52. What is the size of an int in C on a 32-bit system?

A) 2 bytes
B) 4 bytes
C) 8 bytes
D) 16 bytes

Answer: B
Explanation: On most 32-bit systems, the size of an int is typically 4 bytes.


53. Which of the following is used to accept user input in C?

A) get()
B) scanf()
C) input()
D) read()

Answer: B
Explanation: The scanf() function is used to take user input in C.


54. Which of the following functions is used to print output to the console in C?

A) echo()
B) printf()
C) write()
D) output()

Answer: B
Explanation: The printf() function is used to print output to the console in C.


55. What is the value of x after the following code executes?

c

CopyEdit

int x = 10;

x += 5;

A) 10
B) 15
C) 5
D) Undefined

Answer: B
Explanation: The += operator adds 5 to x, so x becomes 15.


56. Which of the following is true about #include in C?

A) It is used to include external files or libraries.
B) It is used to define constants.
C) It is used to define variables.
D) It is used to declare functions.

Answer: A
Explanation: The #include directive is used to include external files or libraries in the program.


57. What is the default value of an uninitialized local variable in C?

A) 0
B) Undefined
C) NULL
D) Garbage value

Answer: B
Explanation: Local variables that are uninitialized in C have an undefined value.


58. What is the purpose of the continue statement in C?

A) To exit the current function
B) To exit the loop entirely
C) To skip the remaining statements in a loop and start the next iteration
D) To jump to a specific part of the program

Answer: C
Explanation: The continue statement is used to skip the remaining statements in the current iteration of a loop and move to the next iteration.


59. What will happen if you try to dereference a NULL pointer in C?

A) The program will crash
B) The pointer will automatically be assigned a valid memory location
C) The program will run without any issue
D) The value of the pointer will be printed as 0

Answer: A
Explanation: Dereferencing a NULL pointer leads to undefined behavior and typically causes the program to crash.


60. What will be the output of the following code?

c

CopyEdit

int x = 5;

int y = 3;

printf("%d", x % y);

A) 2
B) 1
C) 3
D) 0

Answer: A
Explanation: The modulus operator % returns the remainder of the division, so 5 % 3 is 2.


61. Which of the following is true about the if statement in C?

A) It can have multiple conditions separated by logical operators.
B) It must always have an else block.
C) It can only have one condition.
D) It is not used for conditional branching.

Answer: A
Explanation: The if statement in C can have multiple conditions separated by logical operators like && (AND) and || (OR).


62. What will be the output of the following code?

c

CopyEdit

int x = 5;

x--;

printf("%d", x);

A) 6
B) 4
C) 5
D) Undefined

Answer: B
Explanation: The -- operator decrements the value of x by 1, so x becomes 4.


63. Which of the following is a correct function definition in C?

A) function int(int x);
B) void function(int x);
C) int function(int x, y);
D) int function(int, y);

Answer: B
Explanation: The correct syntax for defining a function with an integer parameter x is void function(int x);.


64. Which of the following is used to declare an array in C?

A) int arr[5];
B) array int arr[5];
C) arr[5] int;
D) int[5] arr;

Answer: A
Explanation: The correct syntax to declare an array of integers with 5 elements is int arr[5];.


65. What is the default return type of the main() function in C?

A) int
B) void
C) char
D) double

Answer: A
Explanation: The main() function in C typically returns an int, and return 0; is used to indicate successful execution.


66. What will be the output of the following code?

c

CopyEdit

int x = 5, y = 10;

if(x > y) {

    printf("True");

} else {

    printf("False");

}

A) True
B) False
C) 5
D) 10

Answer: B
Explanation: Since x is not greater than y, "False" is printed.


67. Which of the following statements is correct about the while loop in C?

A) It is an entry-controlled loop.
B) It is an exit-controlled loop.
C) It can only execute once.
D) It cannot be used for infinite loops.

Answer: A
Explanation: The while loop is an entry-controlled loop, meaning the condition is checked before each iteration.


68. What is the purpose of the void keyword in C?

A) To indicate the return type of a function that does not return any value
B) To define an empty array
C) To declare an empty function
D) To define a function that can take any type of argument

Answer: A
Explanation: The void keyword is used to specify that a function does not return any value.


69. Which of the following operators is used for bitwise AND operation in C?

A) &&
B) &
C) |
D) ^

Answer: B
Explanation: The & operator is used for bitwise AND operations in C.


70. What does the following code print?

c

CopyEdit

int x = 7;

printf("%d", x >> 1);

A) 3
B) 4
C) 7
D) 1

Answer: B
Explanation: The >> operator is the right shift operator. Shifting 7 (binary: 111) one bit to the right gives 3 (binary: 011).


71. What is the result of the following code?

c

CopyEdit

int x = 5;

x = x + ++x;

printf("%d", x);

A) 10
B) 11
C) 5
D) 6

Answer: B
Explanation: The ++x increments x before the addition, so x + ++x becomes 5 + 6 = 11.


72. Which of the following is the correct format specifier for printing a double in C?

A) %f
B) %lf
C) %d
D) %lf and %f are the same for printing double

Answer: B
Explanation: The correct format specifier for printing a double in C is %lf.


73. What will be the output of the following code?

c

CopyEdit

int x = 10, y = 20;

if(x > y) {

    printf("True");

} else {

    printf("False");

}

A) True
B) False
C) 10
D) 20

Answer: B
Explanation: Since x is not greater than y, "False" is printed.


74. What does the sizeof() operator return?

A) The size of a variable in bytes
B) The length of an array
C) The number of elements in a string
D) The maximum value a variable can hold

Answer: A
Explanation: The sizeof() operator returns the size of a variable or data type in bytes.


75. What will the following code output?

c

CopyEdit

int x = 5;

printf("%d", x > 3 && x < 10);

A) 1
B) 0
C) 5
D) True

Answer: A
Explanation: The condition x > 3 && x < 10 is true, so 1 will be printed.


Let me know if you'd like the next 25 MCQs!

4o mini

You said:

yes

ChatGPT said:

ChatGPT

Here are the final 25 MCQs to complete the set of 100:


76. Which of the following statements is true about pointers in C?

A) A pointer stores the value of a variable.
B) A pointer stores the address of a variable.
C) A pointer is used to define constants.
D) A pointer cannot be initialized.

Answer: B
Explanation: A pointer stores the address of a variable, not the value itself.


77. What is the correct way to declare a pointer in C?

A) int *ptr;
B) ptr int;
C) *ptr int;
D) pointer int;

Answer: A
Explanation: The correct syntax to declare a pointer to an integer is int *ptr;.


78. Which of the following will cause a segmentation fault in C?

A) Dereferencing a NULL pointer
B) Assigning a value to an uninitialized variable
C) Using the scanf() function incorrectly
D) Using the printf() function incorrectly

Answer: A
Explanation: Dereferencing a NULL pointer leads to undefined behavior, which usually causes a segmentation fault.


79. What is the correct way to access a member of a structure in C?

A) structure.member
B) structure->member
C) Both A and B
D) structure::member

Answer: C
Explanation: The correct way to access a structure member is either structure.member (for direct access) or structure->member (when accessing via a pointer).


80. What will be the output of the following code?

c

CopyEdit

int x = 5;

x *= 3;

printf("%d", x);

A) 5
B) 8
C) 15
D) 3

Answer: C
Explanation: The *= operator multiplies x by 3, so x becomes 15.


81. What is the correct syntax to define a constant in C?

A) const int x = 10;
B) constant int x = 10;
C) define x 10
D) int const x = 10;

Answer: A
Explanation: The correct syntax to define a constant is const int x = 10;.


82. What will be the output of the following code?

c

CopyEdit

int x = 5, y = 5;

if(x == y)

    printf("Equal");

else

    printf("Not equal");

A) Equal
B) Not equal
C) 5
D) Undefined

Answer: A
Explanation: Since x is equal to y, "Equal" will be printed.


83. Which of the following is the correct declaration for a function that takes two integers as parameters and returns an integer in C?

A) int function(int, int);
B) function int(int, int);
C) void function(int, int);
D) int function(int x, int y);

Answer: D
Explanation: The correct way to declare a function that takes two integers and returns an integer is int function(int x, int y);.


84. What is the result of the following expression in C?

c

CopyEdit

5 + 3 * 2

A) 16
B) 11
C) 13
D) 23

Answer: B
Explanation: According to operator precedence, 3 * 2 is calculated first (giving 6), and then 5 + 6 is evaluated, resulting in 11.


85. Which of the following is not a valid C data type?

A) int
B) float
C) string
D) char

Answer: C
Explanation: string is not a valid C data type. In C, strings are represented as arrays of characters (char[]).


86. What is the output of the following code?

c

CopyEdit

int x = 5;

printf("%d", ++x);

A) 5
B) 6
C) 10
D) Undefined

Answer: B
Explanation: The ++x increments x before printing, so the output is 6.


87. Which of the following is the correct way to define a function in C?

A) int add(int a, int b) { return a + b; }
B) int add(int a, b) { return a + b; }
C) function int add(int a, int b) { return a + b; }
D) add(int a, int b) { return a + b; }

Answer: A
Explanation: The correct syntax for defining a function with parameters a and b is int add(int a, int b) { return a + b; }.


88. What is the value of x after the following code executes?

c

CopyEdit

int x = 10;

x = x - 5;

A) 5
B) 10
C) 15
D) Undefined

Answer: A
Explanation: The value of x is reduced by 5, so x becomes 5.


89. Which of the following is used for dynamic memory allocation in C?

A) malloc()
B) free()
C) new()
D) delete()

Answer: A
Explanation: The malloc() function is used to allocate dynamic memory in C.


90. Which operator is used to assign a value to a variable in C?

A) ==
B) =
C) :=
D) ->

Answer: B
Explanation: The = operator is used for assignment in C.


91. What is the correct syntax for a single-line comment in C?

A) /* This is a comment */
B) // This is a comment
C) # This is a comment
D) ; This is a comment

Answer: B
Explanation: In C, single-line comments are written using //.


92. What will be the output of the following code?

c

CopyEdit

int x = 4, y = 2;

printf("%d", x / y);

A) 2
B) 1
C) 4
D) 0

Answer: A
Explanation: The division x / y results in 2 since 4 / 2 = 2.


93. What is the purpose of the return statement in a C function?

A) It terminates the function and returns a value (if specified).
B) It stops the program.
C) It pauses the function until further input is provided.
D) It creates an infinite loop.

Answer: A
Explanation: The return statement is used to terminate the function and optionally return a value.


94. Which of the following data types are used to store a single character in C?

A) int
B) float
C) char
D) string

Answer: C
Explanation: The char data type is used to store a single character in C.


95. Which of the following can be used to compare two variables in C?

A) =
B) ==
C) !=
D) Both B and C

Answer: D
Explanation: Both == (equal to) and != (not equal to) can be used to compare variables in C.


96. What does the following code print?

c

CopyEdit

int x = 10;

int y = 2;

printf("%d", x % y);

A) 0
B) 1
C) 2
D) 5

Answer: A
Explanation: The modulus operator % returns the remainder of x % y, which is 10 % 2 = 0.


97. What will the following code output?

c

CopyEdit

int x = 8;

printf("%d", x << 1);

A) 16
B) 4
C) 2
D) 8

Answer: A
Explanation: The << operator is the left shift operator. Shifting 8 (binary: 1000) one bit to the left gives 16 (binary: 10000).


98. Which of the following functions is used to get the length of a string in C?

A) length()
B) strlength()
C) strlen()
D) size()

Answer: C
Explanation: The strlen() function is used to get the length of a string in C.


99. What will be the result of the following code?

c

CopyEdit

int x = 5, y = 5;

x = y++;

printf("%d", x);

A) 5
B) 6
C) 4
D) Undefined

Answer: A
Explanation: The post-increment operator y++ increments y after assigning its value to x. Thus, x becomes 5, and y becomes 6.


100. What is the result of the following operation?

c

CopyEdit

int x = 5;

x = x & 3;

printf("%d", x);

A) 1
B) 3
C) 7
D) 5

Answer: A
Explanation: The bitwise AND operator & performs bitwise comparison. 5 & 3 is 1 (binary: 101 & 011 = 001).


This completes the set of 100 MCQs on the C programming language, covering various topics including variables, operators, control structures, functions, arrays, pointers, and more!

 

Comments

Popular posts from this blog

Lecture Notes Of Class 12: Functions - Basics