C Programming Basics Quiz
21 Questions
0 Views

C Programming Basics Quiz

Created by
@DiligentComplex

Podcast Beta

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the maximum value that an 'int' type variable can hold in C?

  • -2,147,483,648
  • 1,073,741,823
  • 2,147,483,647 (correct)
  • 4,294,967,295
  • Which of these statements about variable names in C is incorrect?

  • Underscores are allowed in variable names.
  • Uppercase letters can be used in variable names.
  • Variable names must not contain spaces.
  • Variable names can start with a digit. (correct)
  • Which C data type is typically used to store a single character?

  • char (correct)
  • float
  • short
  • int
  • What is the purpose of using 'unsigned' with a variable type in C?

    <p>It forces the variable to take only positive values.</p> Signup and view all the answers

    What is the bit size of a 'float' data type in C?

    <p>32 bits</p> Signup and view all the answers

    Which preprocessor directive is mandatory for the functioning of a C program?

    <p>#include &lt;stdio.h&gt;</p> Signup and view all the answers

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

    <p>To remove the definition of a constant</p> Signup and view all the answers

    Which of the following correctly indicates the structure of the main() function in C?

    <p>int main() { instructions; }</p> Signup and view all the answers

    Where must all variables be declared in a C program?

    <p>Before being used in the program</p> Signup and view all the answers

    Which of the following is considered an integer constant in C?

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

    What is the function of the #ifdef directive?

    <p>To check if a macro is defined</p> Signup and view all the answers

    Which data type is NOT represented by a constant in C?

    <p>Logical constants</p> Signup and view all the answers

    What character must end each instruction within the main() function?

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

    What does the declaration 'float zx, zy;' indicate?

    <p>Both zx and zy are floating-point numbers.</p> Signup and view all the answers

    Which of the following correctly initializes an integer variable in C?

    <p>int x = 5;</p> Signup and view all the answers

    In C, what is the result of the operation '5 % 2'?

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

    What does the statement 'a = b + 1;' illustrate?

    <p>The assignment operator in use.</p> Signup and view all the answers

    Which of the following variable types allows for negative values in C?

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

    What will the following statement 'j = i++;' accomplish in C if 'int i = 1;'?

    <p>i becomes 2 and j becomes 1.</p> Signup and view all the answers

    What will the expression 'int i = 1, j;' followed by 'j = ++i;' result in?

    <p>i = 2, j = 2</p> Signup and view all the answers

    What does the declaration 'char zz = 'a';' specify?

    <p>zz is a character variable initialized with the value 'a'.</p> Signup and view all the answers

    Study Notes

    Integers for Boolean Tests

    • Integers are used for boolean tests in C:
      • 0 is equivalent to false
      • Any non-zero value is considered true

    Variable Names in C

    • C differentiates between uppercase and lowercase letters.
    • Variable names are written in lowercase for clarity.
    • Uppercase letters are used for symbolic constants defined by #define.
    • Variable names must start with a letter and cannot contain spaces.
    • Only an underscore is allowed as a special character.
    • Reserved keywords like while, if, case, and function names cannot be used as variable names.

    Variable Declarations in C

    • To declare a variable, prefix its name with its data type.
    • There are 6 basic data types in C:
      • char: Character, occupies one byte (8 bits), with a range of -128 to 127.
      • short: Integer, occupies two bytes, with a range of -32,768 to 32,767.
      • int: Integer, occupies four bytes, with a range of -2,147,483,648 to 2,147,483,647.
      • long: Integer, occupies 8 bytes, with a range of -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
      • float: Real number, occupies four bytes, with a range of approximately -10^38 to 10^38.
      • double: Real number, occupies eight bytes, with a range of approximately -10^308 to 10^308.
    • Each type can be prefixed with unsigned, forcing the variable to hold only positive values.

    Preprocessor Directives

    • #include: Allows the use of functions like printf() and scanf().
    • #include <math.h>: Allows the use of mathematical functions.
    • #define PI 3.14159: Defines the constant PI.
    • #undef PI: Undefines the constant PI from this point onwards.
    • #ifdef PI: Compiles instructions 1 if PI is defined, otherwise compiles instructions 2.
    • #else: Provides alternative instructions if PI is not defined.
    • #endif: Ends the conditional compilation block.

    General Structure of a C Program

    • Only one preprocessor directive is mandatory: #include <stdio.h>.
    • This directive enables functions like printf() for screen output and scanf() for keyboard input.

    The main() Function in C

    • The main() function is the starting point of every C program.
    • It's defined using the syntax: main() { ... }.
    • Instructions within the main() function are enclosed in curly braces and end with semicolons.
    • All variables must be declared before they can be used within main().

    Examples of Variable Declarations in C

    • int a;: Declares an integer variable named a.
    • int z = 4;: Declares an integer variable named z and initializes it with the value 4.
    • unsigned int x;: Declares an unsigned integer variable named x.
    • float zx, zy;: Declares two float variables named zx and zy.
    • float zx = 15.15;: Declares a float variable named zx and initializes it with the value 15.15.
    • double z;: Declares a double-precision real number variable named z.
    • char zz;: Declares a character variable named zz.
    • char zz = 'a';: Declares a character variable named zz and initializes it with the character 'a'.

    Basic Operators in C

    • The assignment operator (=) assigns the value on the right-hand side to the variable on the left-hand side.
    • Basic arithmetic operators:
      • +: Addition
      • -: Subtraction
      • *: Multiplication
      • /: Division
      • %: Modulo (remainder of division)

    Advanced Operators in C

    • ++: Increments a variable by 1.
    • --: Decrements a variable by 1.
    • These operators are not used with floating-point numbers.
    • i++: Increments i after using its current value.
    • ++i: Increments i before using its value.
    • 'a' + 1: Valid operation in C, it yields the next character in the ASCII sequence after 'a'.

    Studying That Suits You

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

    Quiz Team

    Related Documents

    Programming_workshop.pdf

    Description

    Test your knowledge of fundamental C programming concepts, including boolean tests with integers, variable naming conventions, and variable declarations. This quiz covers important rules and data types essential for writing clear and correct C code.

    More Like This

    Introduction to Variables in Programming
    13 questions
    Use Quizgecko on...
    Browser
    Browser