Posted in: E Learn

Programming C-1 | Q&A

Certainly! Here are 10 short questions along with their answers on programming in C:

  1. Question: What is the purpose of the #include <stdio.h> directive in C?
    Answer: The #include <stdio.h> directive is used to include the standard input-output library in C, which provides functions like printf() and scanf() for input and output operations.
  2. Question: Explain the difference between printf() and scanf() functions in C.
    Answer: printf() is used for formatted output, allowing you to display data on the screen, while scanf() is used for formatted input, allowing you to read data from the user or a file.
  3. Question: What is the significance of the main() function in a C program?
    Answer: The main() function serves as the entry point of a C program. It is where the execution of the program begins and ends. Every C program must have exactly one main() function.
  4. Question: How are comments written in C? Provide examples.
    Answer: Comments in C can be written using // for single-line comments or /* */ for multi-line comments. For example:
// This is a single-line comment

/* This is a
   multi-line comment */
  1. Question: What is the purpose of the sizeof operator in C?
    Answer: The sizeof operator is used to determine the size of a variable or data type in bytes. It helps in writing portable code by providing a way to calculate the memory requirements of variables.
  2. Question: How do you declare a constant in C?
    Answer: Constants in C can be declared using the const keyword. For example:
const int MAX_VALUE = 100;
  1. Question: Explain the difference between ++i and i++ in C.
    Answer: Both ++i and i++ increment the value of i by 1. However, ++i is the pre-increment operator, which increments i and then returns the incremented value, while i++ is the post-increment operator, which returns the current value of i and then increments it.
  2. Question: What is a pointer in C?
    Answer: A pointer in C is a variable that stores the memory address of another variable. It allows direct manipulation of memory and enables dynamic memory allocation and efficient access to data structures.
  3. Question: How do you dynamically allocate memory in C?
    Answer: Memory can be dynamically allocated in C using functions like malloc(), calloc(), and realloc(), which allocate memory on the heap at runtime and return a pointer to the allocated memory.
  4. Question: What is the purpose of the return statement in a function?
    Answer: The return statement is used to exit a function and return a value to the caller. It can also be used to return control to the caller before the end of the function body.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top