C Programming: Variables and extern Keyword
82 Questions
0 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

In what year did the American National Standards Institute (ANSI) establish a committee to standardize the C programming language?

  • 1972
  • 1983 (correct)
  • 2000
  • 1978
  • Which feature does C language provide that contributes to its portability?

  • Complex syntax
  • Isolation of machine-specific features (correct)
  • Lack of built-in functions
  • Direct reliance on hardware architecture
  • Who proposed the C programming language?

  • Ken Thompson
  • Dennis M. Ritchie (correct)
  • Martin Richard
  • Brian Kernighan
  • What is the earlier form of the C language that was written for the DEC PDP-7?

    <p>B language</p> Signup and view all the answers

    Which standard is recognized as the core standard for the C language?

    <p>ANSI C</p> Signup and view all the answers

    What is one reason why C is considered a robust programming language?

    <p>It has a rich set of built-in functions and data types.</p> Signup and view all the answers

    Which programming language is most frequently associated with UNIX?

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

    What is the primary characteristic of a static variable in C?

    <p>It retains its value between multiple function calls.</p> Signup and view all the answers

    Which storage class in C is associated with variables allocated in CPU registers?

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

    Which of the following statements is true regarding automatic variables in C?

    <p>Their visibility is limited to the block in which they are defined.</p> Signup and view all the answers

    What happens when you declare an external variable in C?

    <p>It tells the compiler that the variable exists and is defined elsewhere.</p> Signup and view all the answers

    Which storage class in C will initialize its variables to a garbage value by default?

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

    What is the range of the 'Short int' data type?

    <p>-32768 to 32767</p> Signup and view all the answers

    What does the keyword 'typedef' achieve in C?

    <p>It creates a synonym for an existing data type.</p> Signup and view all the answers

    Which of the following is a valid character constant in C?

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

    What is the maximum value for an 'Unsigned int' data type?

    <p>4,294,967,295</p> Signup and view all the answers

    Why are enumeration constants beneficial in C?

    <p>They allow for easy association of constant values with names.</p> Signup and view all the answers

    Which statement accurately defines a local variable?

    <p>A variable declared within a function or block.</p> Signup and view all the answers

    What suffix is used to denote a long constant in C?

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

    Which of the following is considered an invalid variable name in C?

    <p>int 2a;</p> Signup and view all the answers

    What kind of data is stored in pointer data types?

    <p>Address of variables</p> Signup and view all the answers

    What describes a string literal correctly in C?

    <p>It is an array terminated by NULL.</p> Signup and view all the answers

    What is the purpose of the documentation section in a C program?

    <p>To provide an overview of the program and its details</p> Signup and view all the answers

    Which statement correctly describes the preprocessor section of a C program?

    <p>It links necessary header files for compilation</p> Signup and view all the answers

    What is the correct way to declare the main function that returns an integer in C?

    <p>int main()</p> Signup and view all the answers

    What types of data types can be declared in the global declaration section of a C program?

    <p>Any data types including float, int, and char</p> Signup and view all the answers

    What does the return statement in a function do?

    <p>It ends the execution of the function and returns control</p> Signup and view all the answers

    What file extension is required for a source file in C?

    <p>.c</p> Signup and view all the answers

    What is the last major file created in the C compilation process?

    <p>The executable file</p> Signup and view all the answers

    Which of the following sections must a C program include to be executable?

    <p>Main function</p> Signup and view all the answers

    Which definition correctly describes a user-defined function?

    <p>A function defined according to the user's requirements</p> Signup and view all the answers

    What is the primary role of the loader in a C program execution?

    <p>It loads the executable file into RAM and informs the CPU of its starting address.</p> Signup and view all the answers

    Which of the following statements about identifiers in C is correct?

    <p>Identified names must be unique within the same scope.</p> Signup and view all the answers

    What does the function pow(base, exponent) return in C?

    <p>The base raised to the power of exponent.</p> Signup and view all the answers

    What is the output of the following code snippet: printf("%f", ceil(2.3));?

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

    Which data type does C consider for storing floating-point numbers?

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

    How would you declare multiple variables of the same data type in C?

    <p>Data-type id1, id2, id3;</p> Signup and view all the answers

    What is the purpose of the printf function in C?

    <p>To print output to the console</p> Signup and view all the answers

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

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

    In a C program, what does the return(0); statement indicate?

    <p>The program finished successfully.</p> Signup and view all the answers

    Which keyword in C is used to define a conditional loop structure?

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

    What was the main objective of the ANSI committee established in 1983 regarding the C programming language?

    <p>To standardize the definition of the language C</p> Signup and view all the answers

    Which statement accurately describes C's relationship with UNIX?

    <p>The majority of UNIX code is written in C</p> Signup and view all the answers

    What distinguishes ANSI C from the original C language often referred to as K&R?

    <p>ANSI C standardizes the grammar and libraries</p> Signup and view all the answers

    In which way is C considered to bridge assembly and high-level languages?

    <p>It has low-level features along with high-level capabilities</p> Signup and view all the answers

    Which of the following statements is NOT true regarding C's portability?

    <p>C relies heavily on hardware architecture</p> Signup and view all the answers

    What was one of the early forms of C language developed for the DEC PDP-7?

    <p>B Language</p> Signup and view all the answers

    Which publication is recognized as the first book about the C programming language?

    <p>The C Programming Language</p> Signup and view all the answers

    What is a characteristic of a static variable in C?

    <p>It retains its value between multiple function calls.</p> Signup and view all the answers

    Which storage class allows a variable to be shared across multiple source files in C?

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

    What is the default initial value of an automatic variable in C?

    <p>Garbage value</p> Signup and view all the answers

    What is a unique feature of register variables in C?

    <p>They may not actually be stored in a CPU register.</p> Signup and view all the answers

    Which statement correctly describes the scope of static local variables?

    <p>They are visible only to the function in which they are defined.</p> Signup and view all the answers

    What does the loader do when a program is executed?

    <p>It loads the program into RAM and informs the CPU of its starting address.</p> Signup and view all the answers

    Which statement accurately describes the role of the Program Counter?

    <p>It holds the next instruction to be executed by the CPU.</p> Signup and view all the answers

    What will the pow(2, 3) function return in C?

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

    Which of the following is a valid declaration for multiple variables of the same data type in C?

    <p>int a, b, c;</p> Signup and view all the answers

    What is the value of int_amt after executing the following code with inputs p=1000, r=10, and t=5?

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

    Which is NOT a basic data type available in C?

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

    Which statement about keywords in C is correct?

    <p>Keywords have predefined meanings and cannot be used as variable names.</p> Signup and view all the answers

    What will be the output of the function sqrt(25) in C?

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

    What is the correct syntax for defining a starting point in a C program?

    <p>int main()</p> Signup and view all the answers

    What does the abs(-12) function return in C?

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

    Which data type has a range of 0 to 255?

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

    What will the declaration 'typedef int Integer;' accomplish in C?

    <p>Create a new data type Integer as int</p> Signup and view all the answers

    Which of the following represents a valid variable name in C?

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

    What is the correct way to denote an unsigned long constant in C?

    <p>1234567899UL</p> Signup and view all the answers

    What is the purpose of escape sequences in C?

    <p>To represent special characters</p> Signup and view all the answers

    Which of these data types has a length of 80 bits?

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

    A character constant is defined as:

    <p>A single character enclosed in single quotes</p> Signup and view all the answers

    Which of the following is NOT a type of variable in C?

    <p>Graphical variable</p> Signup and view all the answers

    What range does a 'Float' data type represent?

    <p>$-3.4<em>10^{38}$ to $3.4</em>10^{38}$</p> Signup and view all the answers

    Which of the following is a valid way to declare multiple integer variables in C?

    <p>int a,b,c;</p> Signup and view all the answers

    What is the purpose of the main function in a C program?

    <p>It is the first function executed when a program runs.</p> Signup and view all the answers

    Which section of a C program contains header files?

    <p>Preprocessor section</p> Signup and view all the answers

    What does the #define directive accomplish in a C program?

    <p>It defines constants used throughout the program.</p> Signup and view all the answers

    What determines the beginning of the documentation section in a C program?

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

    Which statement accurately describes the relationship between the compiler and object files?

    <p>The compiler checks for errors and generates object files which are not executable.</p> Signup and view all the answers

    Which of the following describes the sequence necessary in the basic structure of a C program?

    <p>Documentation section, Preprocessor section, Main function</p> Signup and view all the answers

    What is the result of using void with the main function in C?

    <p>It indicates the program does not return any value.</p> Signup and view all the answers

    What is produced when the C compiler successfully compiles a program without errors?

    <p>An object file with .obj extension.</p> Signup and view all the answers

    What is the role of the linker in the C compilation process?

    <p>It links object files with necessary library functions.</p> Signup and view all the answers

    Study Notes

    History and Development of C

    • C language was proposed by Dennis M. Ritchie at Bell Laboratories in 1972.
    • ANSI established a committee in 1983 for creating an unambiguous and machine-independent definition of C.
    • C is predominantly associated with UNIX; much of the UNIX operating system is written in C.
    • C was designed for the DEC PDP-11 computers and evolved from BCPL and the B language.

    Significance of C

    • C is a robust language with a rich set of built-in functions, data types, and operators, enabling the creation of complex programs.
    • Programs written in C are efficient due to its diverse data types and operators.
    • Combines low-level and high-level language features, making it suitable for system and application software.
    • Highly portable; code can be easily transferred across different hardware architectures.
    • Supports direct memory access using pointers, allowing efficient resource management.
    • User-friendly syntax resembling the English language.

    Basic Structure of a C Program

    • A C program typically includes these sections:
      • Documentation Section: Overviews the program's purpose and details.
      • Preprocessor Section: Includes header files needed for the program.
      • Definition Section: Declares constants using the #define keyword.
      • Global Declaration: Declares global variables accessible throughout the program.
      • Main Function: Entry point of the program, required for execution.
      • User-defined Functions: Additional functions created as per user requirements.

    Main Function

    • The main() function is invoked first during program execution and must be included.
    • Variants include int main() (returns an integer) or void main() (does not return a value).
    • Contains local declarations, statements (e.g., control structures), and expressions (calculations performed).

    Program Execution Process

    • C program files are saved with a .c extension and compiled to create various output files.
    • Compilation involves creating an object file (.obj), which is linked to library functions to produce an executable file (.exe).
    • The loader loads the executable into RAM for execution, with the CPU following a defined instruction order.

    Input/Output Examples

    • Simple greeting:
    #include <stdio.h>
    main() {
        printf("welcome to c programming");
    }
    
    • Adding two numbers:
    #include <stdio.h>
    int main() {
        int a, b, sum;
        printf("\nEnter two no: ");
        scanf("%d %d", &a, &b);
        sum = a + b;
        printf("Sum : %d", sum);
        return 0;
    }
    
    • Interest calculation:
    #include <stdio.h>
    int main() {
        int p, r, t, int_amt;
        printf("Input principle, Rate of interest & time:\n");
        scanf("%d%d%d", &p, &r, &t);
        int_amt = (p * r * t) / 100;
        printf("Simple interest = %d", int_amt);
        return 0;
    }
    

    Mathematical Functions in C

    • Math operations (e.g., ceil(), floor(), sqrt(), pow(), abs()) are available through included headers.
    • Provides functions for common mathematical tasks like rounding, calculating square roots, and raising numbers to powers.

    Fundamental Concepts

    • C Tokens: Basic building blocks of a C program, including keywords, identifiers, constants, operators, and special symbols.
    • Keywords: Reserved words with predefined meanings (e.g., int, void, if, else, for).
    • Identifiers: Names for variables and functions, must begin with a letter or underscore, cannot be keywords.
    • Data Types: C includes basic, derived, user-defined, and pointer data types, essential for defining variable types and operations.

    Constants and Variables

    • Constants: Non-varying quantities during execution, categorized as numeric (e.g., integers, floating-point) or character constants.
    • Variables: Named memory locations that store data, must be declared with a data type before use. Types include local (within functions), global (accessible by all), and static variables.

    Example of Variable Declaration

    int a = 10, b = 20;           // Integer variables
    float f = 20.5;               // Float variable
    char c = 'A';                 // Character variable
    

    Rules for Variables

    • Can include letters, digits, and underscores but must start with a letter or underscore.
    • Cannot use spaces or reserved keywords as names.

    Program Execution Workflow

    • Source file (.c) is compiled into an object file (.obj) and linked to create an executable (.exe).
    • Loader loads the program into memory, and CPU executes the instructions sequentially.

    Conclusion

    • Understanding C's history, structure, syntax, and features is foundational for programming in C and developing efficient applications.### Types of Variables in C
    • Global Variable: Declared outside of any function and can be accessed throughout the program.
    • Local Variable: Declared within a function or block, only accessible within that scope.

    Static Variables

    • Declared with the static keyword.
    • Retains value across multiple function calls.
    • Local static variables maintain their last assigned value, while initial value is zero.
    • Example output of increasing static variable: 11, 12, 13 for consecutive calls.

    Automatic Variables

    • Default type for variables declared inside a block.
    • Memory is automatically allocated at runtime.
    • Scope is limited to the block they are defined in, initialized to garbage values.

    External Variables

    • Allows sharing of a variable across multiple C source files.
    • Declared using the extern keyword.
    • Memory is not allocated upon declaration; points to an existing variable defined elsewhere in the program.

    Storage Classes in C

    • Four types: Automatic, External, Static, Register.
    • Automatic:
      • Stored in RAM with garbage values by default.
      • Lifetime ends when the block exits.
    • External:
      • Shared globally, initialized to zero, exists until the end of the main program.
    • Static:
      • Localized within the block but retains value across calls, initialized to zero.
    • Register:
      • Potentially stored in CPU registers, optimized for faster access.
      • Cannot use the address operator (&) on register variables.

    Characteristics of Storage Classes

    • Automatic Variables:
      • Visibility confined to their defining block.
      • Auto keyword can be used explicitly.
    • Static Variables:
      • Initialization occurs only once, retaining values between calls.
      • Declared using the static keyword, visibility is confined to the block.
    • Register Variables:
      • Fast access due to CPU storage but may not be guaranteed.
    • External Variables:
      • Only one definition allowed in the program with potential multiple declarations.

    Common Errors

    • Extern variables cannot be initialized; doing so may generate compile-time errors.
    • An undefined reference error occurs if the extern variable isn't defined anywhere in the code.

    Examples

    • Example of automatic variables printing garbage values due to lack of initialization.
    • Static variables initial value and its retention across function calls displayed via output.
    • extern variable handling demonstrated through multiple examples showing valid and invalid usages.

    History and Development of C

    • C language was proposed by Dennis M. Ritchie at Bell Laboratories in 1972.
    • ANSI established a committee in 1983 for creating an unambiguous and machine-independent definition of C.
    • C is predominantly associated with UNIX; much of the UNIX operating system is written in C.
    • C was designed for the DEC PDP-11 computers and evolved from BCPL and the B language.

    Significance of C

    • C is a robust language with a rich set of built-in functions, data types, and operators, enabling the creation of complex programs.
    • Programs written in C are efficient due to its diverse data types and operators.
    • Combines low-level and high-level language features, making it suitable for system and application software.
    • Highly portable; code can be easily transferred across different hardware architectures.
    • Supports direct memory access using pointers, allowing efficient resource management.
    • User-friendly syntax resembling the English language.

    Basic Structure of a C Program

    • A C program typically includes these sections:
      • Documentation Section: Overviews the program's purpose and details.
      • Preprocessor Section: Includes header files needed for the program.
      • Definition Section: Declares constants using the #define keyword.
      • Global Declaration: Declares global variables accessible throughout the program.
      • Main Function: Entry point of the program, required for execution.
      • User-defined Functions: Additional functions created as per user requirements.

    Main Function

    • The main() function is invoked first during program execution and must be included.
    • Variants include int main() (returns an integer) or void main() (does not return a value).
    • Contains local declarations, statements (e.g., control structures), and expressions (calculations performed).

    Program Execution Process

    • C program files are saved with a .c extension and compiled to create various output files.
    • Compilation involves creating an object file (.obj), which is linked to library functions to produce an executable file (.exe).
    • The loader loads the executable into RAM for execution, with the CPU following a defined instruction order.

    Input/Output Examples

    • Simple greeting:
    #include <stdio.h>
    main() {
        printf("welcome to c programming");
    }
    
    • Adding two numbers:
    #include <stdio.h>
    int main() {
        int a, b, sum;
        printf("\nEnter two no: ");
        scanf("%d %d", &a, &b);
        sum = a + b;
        printf("Sum : %d", sum);
        return 0;
    }
    
    • Interest calculation:
    #include <stdio.h>
    int main() {
        int p, r, t, int_amt;
        printf("Input principle, Rate of interest & time:\n");
        scanf("%d%d%d", &p, &r, &t);
        int_amt = (p * r * t) / 100;
        printf("Simple interest = %d", int_amt);
        return 0;
    }
    

    Mathematical Functions in C

    • Math operations (e.g., ceil(), floor(), sqrt(), pow(), abs()) are available through included headers.
    • Provides functions for common mathematical tasks like rounding, calculating square roots, and raising numbers to powers.

    Fundamental Concepts

    • C Tokens: Basic building blocks of a C program, including keywords, identifiers, constants, operators, and special symbols.
    • Keywords: Reserved words with predefined meanings (e.g., int, void, if, else, for).
    • Identifiers: Names for variables and functions, must begin with a letter or underscore, cannot be keywords.
    • Data Types: C includes basic, derived, user-defined, and pointer data types, essential for defining variable types and operations.

    Constants and Variables

    • Constants: Non-varying quantities during execution, categorized as numeric (e.g., integers, floating-point) or character constants.
    • Variables: Named memory locations that store data, must be declared with a data type before use. Types include local (within functions), global (accessible by all), and static variables.

    Example of Variable Declaration

    int a = 10, b = 20;           // Integer variables
    float f = 20.5;               // Float variable
    char c = 'A';                 // Character variable
    

    Rules for Variables

    • Can include letters, digits, and underscores but must start with a letter or underscore.
    • Cannot use spaces or reserved keywords as names.

    Program Execution Workflow

    • Source file (.c) is compiled into an object file (.obj) and linked to create an executable (.exe).
    • Loader loads the program into memory, and CPU executes the instructions sequentially.

    Conclusion

    • Understanding C's history, structure, syntax, and features is foundational for programming in C and developing efficient applications.### Types of Variables in C
    • Global Variable: Declared outside of any function and can be accessed throughout the program.
    • Local Variable: Declared within a function or block, only accessible within that scope.

    Static Variables

    • Declared with the static keyword.
    • Retains value across multiple function calls.
    • Local static variables maintain their last assigned value, while initial value is zero.
    • Example output of increasing static variable: 11, 12, 13 for consecutive calls.

    Automatic Variables

    • Default type for variables declared inside a block.
    • Memory is automatically allocated at runtime.
    • Scope is limited to the block they are defined in, initialized to garbage values.

    External Variables

    • Allows sharing of a variable across multiple C source files.
    • Declared using the extern keyword.
    • Memory is not allocated upon declaration; points to an existing variable defined elsewhere in the program.

    Storage Classes in C

    • Four types: Automatic, External, Static, Register.
    • Automatic:
      • Stored in RAM with garbage values by default.
      • Lifetime ends when the block exits.
    • External:
      • Shared globally, initialized to zero, exists until the end of the main program.
    • Static:
      • Localized within the block but retains value across calls, initialized to zero.
    • Register:
      • Potentially stored in CPU registers, optimized for faster access.
      • Cannot use the address operator (&) on register variables.

    Characteristics of Storage Classes

    • Automatic Variables:
      • Visibility confined to their defining block.
      • Auto keyword can be used explicitly.
    • Static Variables:
      • Initialization occurs only once, retaining values between calls.
      • Declared using the static keyword, visibility is confined to the block.
    • Register Variables:
      • Fast access due to CPU storage but may not be guaranteed.
    • External Variables:
      • Only one definition allowed in the program with potential multiple declarations.

    Common Errors

    • Extern variables cannot be initialized; doing so may generate compile-time errors.
    • An undefined reference error occurs if the extern variable isn't defined anywhere in the code.

    Examples

    • Example of automatic variables printing garbage values due to lack of initialization.
    • Static variables initial value and its retention across function calls displayed via output.
    • extern variable handling demonstrated through multiple examples showing valid and invalid usages.

    Studying That Suits You

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

    Quiz Team

    Description

    Explore the usage and behavior of the 'extern' keyword in C programming through various examples. This quiz covers how global variables are handled and the implications of using 'extern' in different scenarios. Test your understanding of variable declaration and initialization in C.

    More Like This

    C Programming
    3 questions

    C Programming

    UnquestionableMoldavite1696 avatar
    UnquestionableMoldavite1696
    Karel Programming Unit 1 Flashcards
    29 questions
    Use Quizgecko on...
    Browser
    Browser