Lecture 3 Introduction to C Programming
43 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

Which of the following is NOT a characteristic of the C programming language?

  • Consistent and cohesive
  • Designed by a committee (correct)
  • Low-level language
  • Small and simple

C hides the underlying system and machine workings from the programmer.

False (B)

Which of the following languages are derived from C?

  • Java
  • Python
  • C++
  • All of the above (correct)

In C programming, where does the execution of a program begin?

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

A header file is included in a C code file using the ______ statement.

<p>#include</p> Signup and view all the answers

What is the primary purpose of the include statement in header files in C programming?

<p>To include the content of the header file at compile time (B)</p> Signup and view all the answers

Header files can only include standard library header files.

<p>False (B)</p> Signup and view all the answers

What are the two main types of files that compose a C program?

<p>Code files and header files (B)</p> Signup and view all the answers

What is contained in header files pertaining to compile-time artifacts?

<p>Type definitions</p> Signup and view all the answers

Header files are ______ by other files in C programming.

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

Why are header files necessary in C programming?

<p>Both A and D (B)</p> Signup and view all the answers

The C compiler processes multiple files at the same time.

<p>False (B)</p> Signup and view all the answers

What should a code file include to ensure that the manifest matches the code?

<p>No other files (B)</p> Signup and view all the answers

Match the following C file types with their descriptions:

<p>.c files = Code files containing implementation code .h files = Header files containing declarations and type definitions</p> Signup and view all the answers

Which preprocessor command is used to insert the contents of a header file into a C source file?

<p>#include (B)</p> Signup and view all the answers

In C, it is acceptable to define functions and variables in header files.

<p>False (B)</p> Signup and view all the answers

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

<p>combines object files</p> Signup and view all the answers

The C compiler created object files, but the ______ creates executable files.

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

Which of the following functionalities is provided by libraries in C?

<p>All of the above (D)</p> Signup and view all the answers

In Java, linking is performed at compile time, similar to C.

<p>False (B)</p> Signup and view all the answers

Which of the following is a key difference between C and Java concerning type information?

<p>Both languages handle type information the same way (B)</p> Signup and view all the answers

In the C build process, what is the purpose of the preprocessor?

<p>process directives</p> Signup and view all the answers

The C compiler translates source code into ______ code.

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

What is the correct order of steps in the C build process?

<p>Compile, Preprocess, Assemble, Link (C)</p> Signup and view all the answers

Every C code file must have a corresponding header file.

<p>False (B)</p> Signup and view all the answers

What does the extern keyword indicate?

<p>A function definition (D)</p> Signup and view all the answers

What is the file extension typically used for C header files?

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

In C, a struct is the rough equivalent of a ______ in other languages.

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

Given a program that reads N numbers and outputs them in reverse order, what is the correct order of output if the input is '5 42 36 97 7 13'?

<p>5 42 36 97 7 13 (B)</p> Signup and view all the answers

Header files are compiled independently of code files.

<p>False (B)</p> Signup and view all the answers

What is the role of the assembler in the C build process?

<p>Translates source code into assembly language (C)</p> Signup and view all the answers

What is the command to compile two c files named 'main.c' and 'stack.c' and create an executable named 'reverse'?

<p>gcc -Wall -o reverse main.c stack.c</p> Signup and view all the answers

The program revstr reads a specified number of ______ and outputs them in reverse order.

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

Which of the following is NOT a learning outcome mentioned for the 'Number Reverse' problem?

<p>Basic input/output using scanf and printf (D)</p> Signup and view all the answers

Header files should contain the implementation (definition) of functions, rather than just the declaration.

<p>False (B)</p> Signup and view all the answers

What would happen if you included a header file in multiple code files, and that header file contained a variable definition?

<p>The program would compile without errors. (B)</p> Signup and view all the answers

What does 'stdout' represent in the context of C programming?

<p>standard output</p> Signup and view all the answers

The best practice dictates that a header file should have the same name as its corresponding ______ file.

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

Initially, 'The C Programming Language' was described in a book consisting of how many pages?

<p>261 pages (B)</p> Signup and view all the answers

The assembly (.s) files are created during the typical build process.

<p>False (B)</p> Signup and view all the answers

Which of the following is the most accurate description of the steps performed by a C compiler?

<p>It translates source code into assembly language. (B)</p> Signup and view all the answers

Why was C created originally when computers were slow and with small memories?

<p>compile individually</p> Signup and view all the answers

______ should be declared(not defined) in the corresponding header file.

<p>all functions and variables</p> Signup and view all the answers

Flashcards

What is C?

A low-level language ideal for manipulating data at the hardware level, commonly used for device drivers, O/S Modules, and embedded systems.

Why learn C?

Understanding how the system and machine operate, implementing data structures while being aware of trade-offs, and looking "under the hood".

What are the two kinds of files in C programs?

Code files (.c) and header files (.h).

What is a declaration in C?

Specify characteristics without creating it; no space allocated, no function body.

Signup and view all the flashcards

What is a definition in C?

Specifies characteristics of a function/variable, allocates space, includes the function body.

Signup and view all the flashcards

What is the purpose of the #include statement?

To include the content of a header file at compile time.

Signup and view all the flashcards

What happens during the C compilation process with #include?

During compilation, the contents of the header file replace the #include statement.

Signup and view all the flashcards

What are the two types of include statements?

#include includes standard library header files. #include "file" includes the program's own header files.

Signup and view all the flashcards

Why use header files?

Avoid duplicate type definitions across files and provide a manifest of runtime artifacts defined in a code file.

Signup and view all the flashcards

Why are header files necessary for compilation?

The C compiler can only process one file at a time, so header files are used.

Signup and view all the flashcards

What does the 'extern' keyword indicate?

A global declaration that is included by user code and defined in another file.

Signup and view all the flashcards

What are the steps in the C build process?

Preprocessor, Compiler, Assembler, Linker.

Signup and view all the flashcards

How are C files compiled?

Each C file is compiled individually, without reading other C files (except included header files).

Signup and view all the flashcards

What does the Linker do?

Builds the executable from object files and libraries.

Signup and view all the flashcards

How does Java differ from C in linking?

Linking happens at runtime in Java, not during compilation.

Signup and view all the flashcards

What is a C commandment regarding header files?

Never put function or variable definitions in header files to avoid multiple definitions.

Signup and view all the flashcards

What is another C commandment regarding header files?

Every code file (except the one with main()) should have a corresponding header file.

Signup and view all the flashcards

Where should external functions/variables be declared?

Functions and variables used outside a code file must by declared in the corresponding header file.

Signup and view all the flashcards

Study Notes

Why Learn C?

  • C is a low-level language suited for data and hardware manipulation (device drivers, O/S modules, embedded systems).
  • It's small, simple, consistent, and cohesive, designed by Denis Ritchie at Bell Labs, initially described in a 261-page book.
  • C features a huge code-base and a standard tool-chain, making it the language of choice for system programming.

Why Learn C? (Really)

  • C requires understanding of the system and machine operations and doesn't conceal anything.
  • Implementation of data structures is required, so awareness of all trade-offs is gained.
  • C forces the user to look "under the hood".

Where to Start Learning C?

  • C has similarities with languages like Java, as many languages are derived from C.
  • The syntax is nearly identical in loops, if statements, local variables, expressions, etc and execution starts in main().
  • Operations, such as output are very similar to Java.

Alternative Start To Learning C

  • There is existing knowledge of C code due to familiarity with Java.
  • Study should focus on organization of C programs (code/header files), the build process, and C commandments.

C File Composition

  • C programs consist of code files (.c) and header files (.h).
  • Header files contain definitions and information about run-time artifacts (function and variable declarations).
  • Code files contain both compile-time and run-time artifacts, being those used when the program is compiled or running, global or static functions and variables.
  • Header files are included by other files.

Header Files and #include

  • A header file is "included" via the #include statement, inserting the header file content at compile time.
  • #include includes standard library header files, while #include "file" includes program-specific header files.
  • Header files may include other header files as well.

Why Header Files?

  • Necessary because the C build process compiles each C file independently.
  • They avoid duplicate type definitions across files and provide a manifest of runtime artifacts.

Avoiding Duplicate Type Declarations

  • Type declarations are used at compile time.
  • Defining a type in multiple code files leads to errors.
  • Use header files to define a type used by multiple code files.

Manifest of a Code File

  • The C compiler can only process one file at a time, and cannot do error checking if a function or variable is used from another file.
  • Header files act as manifests containing type definitions, function declarations, and variable declarations.
  • The header file containing the "manifest" is included by code files that use those types, functions and variables.
  • It's a best practice for header file and code file names to match, and for code files to include their corresponding header files (e.g., stack.c includes stack.h).

The C Build Process

  • The build process includes preprocessing, compiling, assembling, and linking.

Larger Programs

  • Compiling commands will look like: gcc –Wall –o reverse main.c stack.c
  • The code base of a larger project consist of: main.c, stack.c, stack.h and node.h

Individually Compiled C Files

  • Key concept is that each C file is compiled individually without reading other C files except for included header files during preprocessing.

Linker Role

  • The linker builds the executable from object files and standard libraries (stdio, stdlib).
  • The linker expects a main function as the starting point.

Key Differences From Java

  • Java performs linking at runtime.
  • Java (.java) files are compiled into class (.class) files individually.
  • Type information is stored and used, as a runtime artifact, in the class files.
  • Class files run to link classes at runtime.

The C Commandments

  • Never put function or variable definitions in header files to avoid multiple definitions.
  • Every code file (besides the one with main()) should have a corresponding header file.
  • Declare all functions and variables used outside of a code file in the corresponding header file.

Learning Through Coding in C

  • Knowledge of Java can accelerate learning C.
  • Writing programs to identify and address the differences promotes learning.
  • Initial topics: Basic I/O, arrays, strings, references (pointers).

Number Reversal Program

  • Goal: Create a program (revnum) that reads a specified number of integers and outputs them in reverse order, using stdin/stdout.
  • Input:
    • An integer N (followed by N integers on separate whitespace).
  • Output:
    • N integers in reverse order, one per line.
  • Learning:
    • Setting up main() function
    • Using scanf and printf
    • Defining and using arrays

String Reversal Program

  • Goal: Create a program (revstr) that reads a specified number of words and outputs them in reverse order.
  • Input:
    • An integer N, followed by N strings separated by white-space (strings are at most 49 characters).
  • Output:
    • The N strings in reverse order, one per line (stdout).
  • Learning:
    • String representation in C
    • Reading, storing, and writing strings
    • References in C [maybe]

Studying That Suits You

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

Quiz Team

Related Documents

Description

Explore the fundamentals of C programming, a low-level language ideal for system programming and hardware manipulation. Understand its significance, system interaction, syntax similarities with languages like Java. Start with a foundational understanding of C and its applications.

More Like This

Use Quizgecko on...
Browser
Browser