C Programming Week 2
69 Questions
2 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

Which of the following is NOT a basic data type in C?

  • double
  • const (correct)
  • float
  • char
  • What is the primary characteristic of a pointer in C?

  • It holds the memory address of another variable. (correct)
  • It is used exclusively for string data.
  • It stores the value of variables directly.
  • It automatically manages memory allocation.
  • What is the typical size of an 'int' data type in C?

  • 2 bytes
  • 8 bytes
  • 16 bytes
  • 4 bytes (correct)
  • Which data type allows multiple members to share the same memory location?

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

    Which of the following correctly defines a double data type variable?

    <p>double area = 50.0;</p> Signup and view all the answers

    How does the const keyword affect a variable in C?

    <p>It prevents the variable's value from being changed after initialization.</p> Signup and view all the answers

    What does the keyword 'void' signify in a function's return type?

    <p>The function does not return any value.</p> Signup and view all the answers

    Which statement about arrays in C is accurate?

    <p>The size of an array is fixed after definition.</p> Signup and view all the answers

    Which statement about local variables is true?

    <p>They are accessible only within the block they are declared.</p> Signup and view all the answers

    Which of the following describes derived data types?

    <p>They include types like arrays, pointers, and structures.</p> Signup and view all the answers

    What is the maximum size for a 'short' data type typically in C?

    <p>2 bytes</p> Signup and view all the answers

    What is the purpose of the #define directive in C?

    <p>To define macros or constants at the preprocessing stage.</p> Signup and view all the answers

    What is the purpose of the 'const' keyword in variable declaration?

    <p>To declare a variable that cannot be modified.</p> Signup and view all the answers

    What is a key requirement of the members of a structure in C?

    <p>They can be of different data types.</p> Signup and view all the answers

    Which of the following statements about signed and unsigned types is true?

    <p>Signed types can hold both negative and positive values, while unsigned types can only hold non-negative values.</p> Signup and view all the answers

    Which data type would be appropriate for storing the height of a person in meters?

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

    What is the main difference between variable definition and declaration?

    <p>Definition allocates memory, while declaration does not.</p> Signup and view all the answers

    What is the scope of a global variable in a C program?

    <p>Visible throughout the entire translation unit.</p> Signup and view all the answers

    How are variables declared in C?

    <p>With a data type and an optional initialization.</p> Signup and view all the answers

    Which of the following pairs of identifiers would be treated as different by C due to case sensitivity?

    <p>number and Number</p> Signup and view all the answers

    Which data type would be best for storing a single character?

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

    What is the lifetime of a local variable?

    <p>Only during the execution of the function or block.</p> Signup and view all the answers

    What must the main function return at the end of program execution?

    <p>an integer</p> Signup and view all the answers

    Which of the following correctly describes the purpose of 'int argc' in the main function?

    <p>Represents the number of command-line arguments</p> Signup and view all the answers

    What does the escape sequence '\n' do in a C program?

    <p>Moves the cursor to a new line</p> Signup and view all the answers

    What is the correct syntax to declare a main function that accepts command-line arguments?

    <p>int main(int argc, char* argv[])</p> Signup and view all the answers

    Which comment style allows for multiple lines in C code?

    <p>/* This is a comment */</p> Signup and view all the answers

    What will be the output of the following code: printf("Welcome\nto\nFallTerm!\n");?

    <p>Welcome to FallTerm!</p> Signup and view all the answers

    What is the role of the backslash '' in escape sequences?

    <p>It acts as an escape character</p> Signup and view all the answers

    Which of the following is NOT a valid escape sequence?

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

    What does the declaration of a variable in C specify?

    <p>The type of the variable without allocating memory.</p> Signup and view all the answers

    Which of the following correctly describes the scope of global variables?

    <p>They exist for the entire program's execution time.</p> Signup and view all the answers

    When a local variable is created, when is its memory allocated?

    <p>When the function or block is entered.</p> Signup and view all the answers

    What is the primary distinction between variable definition and declaration in C?

    <p>Definition allocates memory; declaration specifies type and indicates implementation elsewhere.</p> Signup and view all the answers

    Which of the following types of variables is accessible throughout the entire program?

    <p>Global variables.</p> Signup and view all the answers

    What happens to the lifetime of a local variable once the function it belongs to exits?

    <p>The variable's memory is deallocated.</p> Signup and view all the answers

    What is the purpose of specifying a data type when declaring a variable in C?

    <p>To indicate how much memory to allocate for the variable.</p> Signup and view all the answers

    How does case sensitivity in C affect variable identifiers?

    <p>It allows different usage of similar names with differing cases to represent different variables.</p> Signup and view all the answers

    What does the variable 'institutionID' represent in the example provided?

    <p>A global variable that can be accessed in any function</p> Signup and view all the answers

    Which statement correctly describes 'studentID' in the resizeClass function?

    <p>It is a local variable that exists only within the while loop.</p> Signup and view all the answers

    What is the significance of data types in C programming?

    <p>They dictate the memory allocation and operations allowed on a variable.</p> Signup and view all the answers

    In C, how many bytes does a float typically occupy?

    <p>4 bytes (32 bits)</p> Signup and view all the answers

    Which data type is used to represent real numbers with greater precision in C?

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

    What does the keyword 'void' indicate when used in a function declaration?

    <p>The function does not return any value.</p> Signup and view all the answers

    Which of the following statements about local variables is incorrect?

    <p>They can be accessed outside of their defining function or block.</p> Signup and view all the answers

    Which statement about basic data types is true regarding their size?

    <p>A float generally occupies less memory than a double.</p> Signup and view all the answers

    What is the default precision for the %f format specifier in the printf function?

    <p>6 decimal places</p> Signup and view all the answers

    What would be the output of the following printf function: printf("x=%4.1f, y=%7.2f", 15.231, 65.875948);?

    <p>x=15.2, y=65.88</p> Signup and view all the answers

    In the example with scanf("%3f%4f", &a, &b), what value will be stored in variable 'a' if the input is 5.965.87?

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

    What determines the output's right justification in printf when using field width?

    <p>The specified field width</p> Signup and view all the answers

    How many characters will the output 'b' have if it is formatted using printf with a field width of 4 (e.g., b=5)?

    <p>4 characters</p> Signup and view all the answers

    Which of the following printf format specifiers correctly formats a floating-point number to display one digit after the decimal point and minimum width of 4?

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

    If the scanf function scans the input string '1234.56', what value will be assigned to 'b' when using scanf("%3f%4f", &a, &b)?

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

    Which aspect of control flow in C does not involve looping?

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

    What is the primary characteristic of a struct in C?

    <p>Groups variables of different types in a single unit</p> Signup and view all the answers

    What does the 'long long' data type guarantee in C?

    <p>At least 8 bytes of storage</p> Signup and view all the answers

    Which of the following statements about pointers is accurate?

    <p>They can point to any data type</p> Signup and view all the answers

    What is a characteristic feature of a union in C?

    <p>Only one member can be accessed at a time</p> Signup and view all the answers

    What is the effect of using the 'const' keyword in a declaration?

    <p>It defines a variable that cannot be altered after initialization</p> Signup and view all the answers

    How are array sizes determined in C?

    <p>By the number of elements and the size of each element</p> Signup and view all the answers

    What type of variable can a pointer in C hold?

    <p>Addresses of any variable type</p> Signup and view all the answers

    What is the result of using the '#define' directive incorrectly, as in '#define MAX 10;' with a semicolon?

    <p>It generates an error during the compilation process.</p> Signup and view all the answers

    What does the 'scanf' function require to store input values in the variables specified?

    <p>The address of each variable involved.</p> Signup and view all the answers

    What will be the value of the variable 'term' after executing 'scanf("%d:%f", &marks, &term);' with the input '1500:20.5'?

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

    How are macros defined with arguments expanded in C?

    <p>They replace arguments with passed values during compilation.</p> Signup and view all the answers

    What value will 'a' and 'b' hold after executing 'scanf("%2d%3d", &a, &b);' with input '6903901'?

    <p>a = 69, b = 39</p> Signup and view all the answers

    Which of the following statements is true regarding the usage of the 'const' keyword?

    <p>Using 'const' improves code readability, indicating values should remain constant.</p> Signup and view all the answers

    What is the result of evaluating the macro 'SUM(4, 5)' using the definition '#define SUM(x, y) ((x) + (y))'?

    <p>It results in 9.</p> Signup and view all the answers

    What will the statement 'printf("Max students: %d\n", MAX_STUDENTS);' output given 'MAX_STUDENTS' is defined as 500?

    <p>Max students: 500</p> Signup and view all the answers

    Study Notes

    Course Information

    • Course Title: C Programming
    • Course Code: CST8234
    • Week: 2
    • Topic: Basics of the language

    Objectives

    • Write simple C programs
    • Use I/O statements
    • Understand fundamental data types
    • Write simple decision-making statements
    • Learn about control specifiers
    • Understand macros
    • Introduction to arrays

    Basics of the C Language

    • The main function is the entry point of a C program.
    • Using other files and libraries (e.g., #include <stdio.h>).
    • C program structure: preprocessor directives, function definitions, statements and expressions.
    • Variables
    • Control Flow (conditionals and loops)
    • Functions

    Typical C Program Structure

    • Preprocessor directives: lines beginning with # (e.g., #include).
    • Function definitions: including the main function and other functions.
    • Statements and expressions: actual code to perform operations.

    Example of a Typical C Program

    #include <stdio.h>
    int add(int a, int b) {
      return a + b;
    }
    int main() {
      int result = add(5, 3);
      printf("The result is %d\n", result);
      return 0;
    }
    

    #include Directive

    • Used to include the contents of a file within another file.
    • Useful for including libraries (e.g., stdio.h) and user-defined header files.
    • Syntax:
      • #include <filename.h>: include from a standard library.
      • #include "filename.h": include from a local directory.

    How #include Works

    • Preprocessor stage: replaces the #include directive with the contents of the specified file before compilation.
    • Inclusion of content: the compiler processes included content as part of the original file.

    Avoiding Multiple Inclusions

    • Use include guards (e.g., #ifndef, #define, #endif) to prevent multiple inclusions of the same header file.

    The main Function (Example)

    #include <stdio.h>
    int main() {
      printf("Welcome to C!\n");
      return 0;
    }
    

    Variables

    • Variables in C store data.
    • They must be declared before use with a data type.
    • Example: int age = 25;, char initial = 'A';.
    • Different data types: int, char, float, double.

    Variables Definition vs Declaration

    • Definition: allocates memory and initializes a variable.
    • Declaration: declares a variable without allocating memory.

    Scope and Lifetime of Variables

    • Global variables: accessible throughout the entire program.
    • Local variables: accessible only within the function or block where they are declared.

    Data Types

    • int: whole numbers
    • char: single characters
    • float: single-precision floating-point numbers
    • double: double-precision floating-point numbers

    Derived Data Types

    • void: absence of type
    • array: collection of elements of the same type.
    • pointer: holds memory address of another variable.
    • struct: groups variables of different types.
    • union: similar to structure, but members share memory.

    Modified Data Types

    • signed/unsigned int: specify whether integers can be negative or not.
    • short/long int: modify integer size.
    • long long: an extended version of long int.

    const Keyword

    • const variables have values that cannot be changed after initialization.
    • Useful for defining constants (e.g., configuration parameters).

    Macro

    • Used to define macros or constants during the preprocessing stage.
    • The #define directive is used to substitute text in the code with a literal value.
    • Example: macro #define MAX_VALUE 100.

    const vs #define

    • const: has type safety, allocates memory.
    • #define: no type safety, no memory allocation.

    Control Specifiers in scanf and printf

    • scanf reads input from the user, printf displays output.
    • Control strings specify input/output formats.
    • Use format specifiers (e.g., %d for integers, %f for floats, %s for strings).

    Functions

    • Functions are fundamental building blocks in a C program.
    • Function syntax: <return type> <function name>(<parameters>).
    • Functions are reusable code blocks.

    For Loop:

    • Used for fixed iterations

    While Loop:

    - Used for iterations based on a condition
    

    Do-while:

     - The loop body is executed at least once.
    

    Logical Operators

    • &&: logical AND, ||: logical OR, !: logical NOT

    Relational Operators

    - `==`, `!=`, `<`, `>`, `<=`, `>=`: Used to compare values and create conditions.
    

    Ternary Operator

    • A shorthand for if-else statements.

    Studying That Suits You

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

    Quiz Team

    Related Documents

    Description

    This quiz covers the fundamentals of C programming language, focusing on writing simple C programs, understanding I/O statements, and fundamental data types. It also introduces control flow and arrays, providing essential skills for coding in C. Test your knowledge on these basic concepts with this quiz.

    Use Quizgecko on...
    Browser
    Browser