C Programming: Functions and Header Files - Exercises with Solutions, Exams of Nursing

A comprehensive set of exercises and solutions related to functions and header files in c programming. It covers key concepts such as function prototypes, static functions, header guards, and the use of the -wall flag in gcc. The exercises are designed to reinforce understanding and provide practical application of these concepts.

Typology: Exams

2023/2024

Available from 01/15/2025

Toperthetop
Toperthetop 🇬🇧

3

(6)

27K documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
L4 COMP 348 Functions (C) questions
with complete solutions
What type of language is C - correct answer ✔✔Imperative and procedural
Imperative languages use what and what can they do - correct answer ✔✔Series of statements, which
can modify the state of the program
Difference between imperative and declarative languages - correct answer ✔✔Declarative languages
indicate what output you would like to see, and imperative languages tell the computer how to produce
its output
What do procedural languages do - correct answer ✔✔Indicate that programming logic can be encoded
as procedures or sub-routines that can be called from other locations in the code
Type of language that is the most basic form of structured orogramming - correct answer ✔✔Procedural
programming
What do OO languages have more than procedural languages - correct answer ✔✔They couple data with
the methods that operate on them
Can imperative languages couple data with the methods that operate on them - correct answer ✔✔No
Is C an OOP - correct answer ✔✔No
Does main in C belong to a class - correct answer ✔✔No
True or false: C source code is typically distributed across multiple source files - correct answer ✔✔True
pf3
pf4
pf5
pf8

Partial preview of the text

Download C Programming: Functions and Header Files - Exercises with Solutions and more Exams Nursing in PDF only on Docsity!

L4 COMP 348 Functions (C) questions

with complete solutions

What type of language is C - correct answer ✔✔Imperative and procedural Imperative languages use what and what can they do - correct answer ✔✔Series of statements, which can modify the state of the program Difference between imperative and declarative languages - correct answer ✔✔Declarative languages indicate what output you would like to see, and imperative languages tell the computer how to produce its output What do procedural languages do - correct answer ✔✔Indicate that programming logic can be encoded as procedures or sub-routines that can be called from other locations in the code Type of language that is the most basic form of structured orogramming - correct answer ✔✔Procedural programming What do OO languages have more than procedural languages - correct answer ✔✔They couple data with the methods that operate on them Can imperative languages couple data with the methods that operate on them - correct answer ✔✔No Is C an OOP - correct answer ✔✔No Does main in C belong to a class - correct answer ✔✔No True or false: C source code is typically distributed across multiple source files - correct answer ✔✔True

The code in C is organized around what - correct answer ✔✔Logical functionality Functions in C associated with a certain task are organized into what - correct answer ✔✔A single source file Examples in C of functions associated with a certain task that are organized into a single source file - correct answer ✔✔IO functions, database functions, graphics functions Are source files compiled separately? Then what? - correct answer ✔✔Yes, then linked together into a single executable What is the advantage of having source files compiled separately and then linked together into a single executable? - correct answer ✔✔This allows changes to large programs to be made much more efficiently Use of makefiles - correct answer ✔✔When large applications require a lot of files, it is a very complex build environment. Makefiles use a special syntax to define how C/C++ source files should be compiled and linked into a single application or library Gcc invocation to compile several source files into a single executable - correct answer ✔✔gcc file1.c file2.c myApp.c Can you use this invocation with large or smaller orograms? And why? gcc file1.c file2.c myApp.c - correct answer ✔✔Inly smaller programs, bc it compiles several source files into a single executable. Large programs have too many source files. Which of the three files contains a main function in this invocation? gcc file1.c file2.c myApp.c - correct answer ✔✔myApp.c True or false: with multiple source files, functions in one file will be invoked from functions in other source files - correct answer ✔✔True

  1. If the API changes you would have to update every one of the calling source files Example of API changing - correct answer ✔✔The function name is modified The proper way to manual inclusion - correct answer ✔✔Create header files that expose the API of the functions that are going to be used elsewhere How do you do your own version of the standard header file like <stdio.h> - correct answer ✔✔By creating header files that expose the API of the functions that are going to be used elsewhere How do you create header files that expose the API of the functions that are going to be used elsewhere
  • correct answer ✔✔Create a new file and add the prototypes to this file. What type of files end with a .h suffix in C - correct answer ✔✔Header file What is the header file's name - correct answer ✔✔The same as the source file it supports Application source file example for creating a header file - correct answer ✔✔#include <stdio.h> #include "file1.h" void my_method () { int y = do_something(4); } What should have been done if we didn't assume that all files are in the same folder as the source file - correct answer ✔✔Additional path info would need to be added with the #include directive What does providing the compiler with a list pf additional include directories do - correct answer ✔✔It allows the compiler to find user-defined header files, just like it finds the standard headers

What should a header file look like - correct answer ✔✔#ifndef file1_H_ #define file1_h_ Int do_something(int x) #endif What do conditional cpp directives do - correct answer ✔✔They ensure that only one copy of this header file gets included in your source file Examples of conditional cpp directive - correct answer ✔✔#endif #ifndef file1_H_ What is a header guard - correct answer ✔✔Only one copy of the header file gets included in your source file Why is conditional inclusion important - correct answer ✔✔Bc multiple inclusions of the same header file into a single source file can cause compilation problems True or false: multiple inclusion is hard to do - correct answer ✔✔False Can header files be included in many different source files, including other header files? - correct answer ✔✔Yes The header guards are used for - correct answer ✔✔To create a cpp flag that says this header has been included during the current compilation phase What would happen if the same header is encountered again by the header guard - correct answer ✔✔Its inclusion would be skipped

What is the static specifier for in C - correct answer ✔✔To modify visibility, used to qualify function definitions What happens when we use a static function - correct answer ✔✔It tells the compiler that this function can only be referenced/invoked from the source file in which it is written Can you make a static function externally visible, how - correct answer ✔✔Yes, by associating its return value to a "public" data type Is there an order that we should put functions in C - correct answer ✔✔No Can you invoke a function in the same source file if the comoiler has not seen the function declaration already - correct answer ✔✔No Can you invoke a function in the same source file if the comoiler has seen the function declaration already - correct answer ✔✔Yes What would happen if you try to invoke a function if the compiler has not seen the function declaration already - correct answer ✔✔You will get standard warning about implicit declaration Should the helper function be first - correct answer ✔✔Yes, Should you include a prototype for helper() at the top of the source file - correct answer ✔✔Yes Where are header files included in - correct answer ✔✔The source file associated to it A header file uses what to include its own prototypes - correct answer ✔✔A cpp directive Should static functions be included in the header file - correct answer ✔✔No

Why shouldn't static functions be included in the header file - correct answer ✔✔Bc these functions are not part of the API What would happen if we put a static function on the header file, why - correct answer ✔✔Function not defined warning bc the compiler will fail to find a function to match the prototype What should be done when you add a prototype for a static function in the current source file - correct answer ✔✔Both the prototype and the full function definition should include the static specifier What heppens if you add a prototype for a static function in the current source file but the prototype and the full function definition don't include the static specifier - correct answer ✔✔The compiler will consider them to be different functions and warnings/errors will occur