C/C++: Data Types, Memory Management and Make
41 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

When modifying code that uses float to now use double for storage, which of the following is most accurate regarding the difference between the two solutions presented in the image?

  • Solution 2 requires changing multiple lines of code, while Solution 1 only requires changing one line.
  • Neither solution requires any changes, as `float` and `double` are interchangeable.
  • Solution 1 requires changing multiple lines of code, while Solution 2 only requires changing one line. (correct)
  • Both solutions require changing the same number of lines of code.

Which statement best describes how enumerations are represented internally in C and closely related languages?

  • Enumerations are represented as floating-point numbers.
  • Enumerations are represented as boolean values.
  • Enumerations are represented as strings.
  • Enumerations are represented as integers. (correct)

Consider the following enumeration: enum Status {PENDING, RUNNING, FAILED = 5, SUCCESS};. What integer value will be assigned to SUCCESS?

  • 7
  • 4
  • 6 (correct)
  • 3

Why is reduced manual memory management beneficial for code maintainability?

<p>It generally leads to fewer errors and reduces debugging efforts. (D)</p> Signup and view all the answers

Which scenario would be most suitable for using an enumeration?

<p>Representing the different states of a traffic light ( রেড, yellow, green). (D)</p> Signup and view all the answers

What is the consequence of invoking make without specifying a target in a directory containing both Makefile and makefile?

<p>It builds the first target found in <code>Makefile</code>, ignoring <code>makefile</code>. (D)</p> Signup and view all the answers

What is the primary purpose of using macros in a Makefile?

<p>To avoid repetitive typing of text strings within the <code>Makefile</code>. (D)</p> Signup and view all the answers

If you want to examine the commands that make would execute without actually running them, which flag should you use?

<p><code>-n</code> (C)</p> Signup and view all the answers

How are multi-line commands specified within a Makefile?

<p>By separating each command with a semicolon <code>;</code> and using a backslash <code>\</code> at the end of each line to indicate continuation. (D)</p> Signup and view all the answers

What is the result if you reference a macro in a Makefile that has not been defined?

<p>The undefined macro is treated as an empty string. (A)</p> Signup and view all the answers

Which of the following scenarios best illustrates a potential issue arising from the use of global variables?

<p>A function modifies a global variable used by another seemingly unrelated function, leading to unexpected behavior that is difficult to trace. (C)</p> Signup and view all the answers

Consider a variable x declared inside a for loop within a function. What is the scope of x?

<p><code>x</code> is accessible only within the <code>for</code> loop block. (C)</p> Signup and view all the answers

What is the primary risk associated with returning a pointer to an automatic (local) variable from a function?

<p>The value pointed to may become invalid after the function returns, leading to undefined behavior. (C)</p> Signup and view all the answers

How does using the static keyword affect a local variable within a function in C?

<p>It changes the variable's storage class from automatic to static, preserving its value between function calls. (A)</p> Signup and view all the answers

In the context of variable scope and storage class, what is the key distinction between them?

<p>Scope determines where a variable can be accessed in the code, while storage class determines how long the variable persists in memory. (D)</p> Signup and view all the answers

Which of the following best describes the role of the C preprocessor?

<p>Interpreting preprocessor directives such as <code>#include</code> and <code>#define</code>. (C)</p> Signup and view all the answers

What is the purpose of using include guards (e.g., #ifndef HEADER_H, #define HEADER_H, #endif) in header files?

<p>To ensure that the header file is included only once, preventing multiple definitions. (B)</p> Signup and view all the answers

What is a primary advantage of using dynamic libraries over static libraries in software development?

<p>Dynamic libraries allow for easier updates and version management without recompiling the main application, as long as the API remains consistent. (A)</p> Signup and view all the answers

When using the #include <file> directive, where does the compiler primarily search for the specified file?

<p>In the compiler's standard list of system directories. (D)</p> Signup and view all the answers

Which file extension is commonly associated with dynamic libraries on Linux systems?

<p>.so (A)</p> Signup and view all the answers

What is the significance of Position Independent Code (PIC) when creating dynamic libraries?

<p>PIC allows the library to be loaded at any memory address, making it suitable for use in multiple programs without conflict. (D)</p> Signup and view all the answers

What is the function of a C compiler?

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

What is the correct way to include a header file named myheader.h that is located in the same directory as the current source file?

<p><code>#include &quot;myheader.h&quot;</code> (B)</p> Signup and view all the answers

A software development team is deciding between static and dynamic libraries for their project. They anticipate frequent updates to certain library functions. Which type of library would be more suitable and why?

<p>Dynamic libraries, because updates can be applied without recompiling the main application, provided the API remains consistent. (A)</p> Signup and view all the answers

Consider the compilation process: source code -> preprocessor -> compiler -> assembler -> linker. At which stage are .i files generated?

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

A dynamic library libexample.so.2.1 has symbolic links libexample.so.2 and libexample.so pointing to it. What is the 'real name' of the library?

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

In C/C++, what is the purpose of passing a variable by reference to a function?

<p>To modify the original variable directly from within the function. (C)</p> Signup and view all the answers

When should you use the -I option with a C compiler, and what does it accomplish?

<p>To add a directory to the list of locations the preprocessor searches for header filfes. (D)</p> Signup and view all the answers

Why is it generally discouraged to include the full path to a header file directly in the #include statement (e.g., #include "/path/to/myheader.h")?

<p>It makes the code less portable and harder to maintain if the header file location changes. (D)</p> Signup and view all the answers

You are using Valgrind to debug a C program and it reports a block of memory as 'definitely lost'. What does this indicate?

<p>The program has leaked memory because it allocated memory but did not free it before losing all references to it. (A)</p> Signup and view all the answers

Before using Valgrind to check for memory leaks in a C program, what compiler flag is essential to include during compilation?

<p>-g (D)</p> Signup and view all the answers

In C programming, what is the primary effect of using the static keyword on a global variable within a file?

<p>It prevents the variable's definition from being passed to the linker, limiting its scope to the current file. (A)</p> Signup and view all the answers

Which of the following describes the most significant difference between scope and access control?

<p>Scope defines where a variable is visible, while access control determines who can access a symbol that is visible. (A)</p> Signup and view all the answers

When should you generally prefer to employ file scope for a function in C?

<p>When the function is intended for internal use only within a specific file. (C)</p> Signup and view all the answers

What is the potential outcome if an external symbol has multiple definitions (extdef) within a linked executable?

<p>The linker will produce an error indicating a multiply defined external symbol. (D)</p> Signup and view all the answers

In the context of program scope in C, what is the purpose of an external reference (extref)?

<p>To declare a variable or function that is defined in another file. (C)</p> Signup and view all the answers

Given the principle of defining a symbol in the narrowest scope that works, which of the following scenarios best exemplifies its application?

<p>Defining a loop counter variable inside the loop's initialization statement instead of at the beginning of the function. (A)</p> Signup and view all the answers

How does Valgrind categorize memory leaks where the pointer to the allocated memory is lost, and that allocated memory is not reachable via any existing pointers?

<p>Definitely lost. (D)</p> Signup and view all the answers

What does 'possibly lost' signify when reported by Valgrind in the context of memory management?

<p>The program might be leaking memory if unusual pointer operations are not handled correctly. (B)</p> Signup and view all the answers

In C programming, what is the difference between a declaration and a definition of a variable?

<p>A definition allocates memory for the variable, while a declaration provides the variable's type and name without allocating memory. (D)</p> Signup and view all the answers

Why is it generally recommended to avoid using global variables that are accessible program-wide by the end user?

<p>To enhance security by preventing unintended modifications and reducing the risk of naming conflicts. (D)</p> Signup and view all the answers

Flashcards

Typing just 'make'

Executes the first target in the Makefile. Looks for Makefile or makefile in the current directory.

Typing 'make '

Builds the specific target you name. For example, 'make myprogram'.

'make clean' target

A target that performs actions like deleting generated files (executables, object files, etc.).

'make -n'

Shows the commands that 'make' would execute without actually running them.

Signup and view all the flashcards

Makefile Macros

Variables in a Makefile that store text strings, avoiding repetition.

Signup and view all the flashcards

Tool Chain

Sequence of software tools converting source code to binary code.

Signup and view all the flashcards

IDE (Interactive Development Environment)

Software application providing comprehensive facilities to computer programmers for software development.

Signup and view all the flashcards

C Compiler

Software that translates C code into assembly language.

Signup and view all the flashcards

Intermediate Files

Files generated during compilation, including .i, .s, and .o files.

Signup and view all the flashcards

C Preprocessor (cpp)

A program that interprets preprocessor directives before compilation.

Signup and view all the flashcards

Preventing Multiple Includes

Technique using preprocessor directives to prevent multiple inclusions of header files.

Signup and view all the flashcards

#include

Used for system header files, searches standard system directories.

Signup and view all the flashcards

#include "file"

Used for program-specific header files; searches the current and 'quote' directories.

Signup and view all the flashcards

Memory Allocation Efficiency

Multiple calls to malloc and free can be less efficient than a single allocation.

Signup and view all the flashcards

Readability and Memory Management

Less is often more readable and reduces debugging time.

Signup and view all the flashcards

What is an Enumeration (Enum)?

An enum is a data type with a fixed set of named integer values.

Signup and view all the flashcards

Enum Default Values

By default, the first element is 0, and subsequent values increment by one unless overridden.

Signup and view all the flashcards

Purpose of Enumerations

Enums can improve code readability and maintainability by representing symbolic information with named constants instead of raw integer values.

Signup and view all the flashcards

Function Scope

Accessible only within the function it's defined in.

Signup and view all the flashcards

Block (Local) Scope

Accessible from its declaration point to the end of the code block (curly braces) it's declared in, including loop counters.

Signup and view all the flashcards

Storage Class

Where and how long a variable is stored (not the same as scope).

Signup and view all the flashcards

Automatic Storage

Variables created temporarily on the stack when a function is called, and destroyed when the function returns.

Signup and view all the flashcards

Static Storage

Only one instance of the variable exists throughout the program's execution.

Signup and view all the flashcards

Static Library

A library where the executable is self-contained, but can become large and is inflexible for updates.

Signup and view all the flashcards

Dynamic Library

A library that is flexible, allowing linking to different versions and keeping the executable size smaller, but requires more experience to use.

Signup and view all the flashcards

Library Naming Conventions

All library names start with 'lib'. Static libraries end with '.a'. Dynamic libraries end with '.so' (Linux) or '.dll' (Windows).

Signup and view all the flashcards

Symbolic Links for Libraries

Multiple names for the same library; for example: liba.so.3.2 may have links liba.so.3 and liba.so which point to it.

Signup and view all the flashcards

Valgrind

Memory debugging tool for detecting memory leaks and other errors. Compile with the '-g' flag to use.

Signup and view all the flashcards

Modifying Variables in Functions

To modify a variable inside a function, its memory address must be passed by reference.

Signup and view all the flashcards

Allocating Memory within a Function

If memory is allocated to a pointer inside a function, the memory will be successfully allocated.

Signup and view all the flashcards

Definitely Lost Memory (Valgrind)

Memory is leaking in a non-pointer based structure.

Signup and view all the flashcards

Definitely Lost Memory

A memory leak where the program loses track of the pointer to allocated memory.

Signup and view all the flashcards

Possibly Lost Memory

A memory leak where the program may still have a pointer to the memory, but it's unusable due to pointer manipulation

Signup and view all the flashcards

Variable Scope

The region of the program where a variable can be accessed by its name.

Signup and view all the flashcards

Access Control

Controls who can access a variable or function, even if it's within their scope.

Signup and view all the flashcards

Program Scope

Variables and functions are accessible by all source files within the executable.

Signup and view all the flashcards

Definition (Symbol)

The actual memory location of data or a function.

Signup and view all the flashcards

Reference (Symbol)

A use of a named thing (variable, function) in the code.

Signup and view all the flashcards

Declaration (Symbol)

Informs the compiler about the name and type of a variable or function.

Signup and view all the flashcards

File Scope

Variables and functions are accessible from their definition point to the end of the file in which they are defined.

Signup and view all the flashcards

Study Notes

  • Tool Chain: Sequence of software tools converting source code to binary.

IDE (Interactive Development Environment)

  • Windows: Visual Studio
  • MacOS: Xcode
  • Multi-Platform: Eclipse, IntelliJ, Codelite, CodeBlocks

Compilers

  • C compilers: GNU Compiler Collection (GCC), Clang

Intermediate Files

  • Consist of .i, .s, and .o files

C Preprocessor

  • Purpose: Interprets #directives in .h and .c files before the compiler processes the source code.
  • #include: Merges header files.
  • #define: Creates macros, with or without arguments.
  • #ifdef, #if, #else, #endif: Handles conditional compilation.
  • Exception: #pragma is passed to the compiler and ignored if not recognized.
  • The C preprocessor is abbreviated as cpp.

Preventing Multiple/Circular Includes

  • Best practice .h pattern:
#ifndef HEADERNAME_H
#define HEADERNAME_H
...body of .h file...
#endif
  • HEADERNAME_H gets defined on the first #include and file content is skipped on subsequent includes.

Headers and Paths

  • #include <file>: Used for system header files; searches standard system directories.
  • #include "file": Used for program header files; searches current and "quote" directories first.
  • Do not include the path to the file in the #include statement.
  • Include the file itself, e.g., #include "Parser.h", and let the compiler toolchain handle the paths.
  • The -I option passes a directory containing a header file to the compiler.
  • Paths for the -I option can be relative or absolute, and multiple directories can be included.

Compiler

  • Compiles C language source code .i files into assembly language .s.
  • Diagnoses abuses of language and issues warnings/errors.
  • Intermediate .i and .s files are normally deleted after assembly.

Assembler

  • Assembles assembly code .s from the compiler into object code .o.
  • Tool setup is normally transparent.
  • There are options for controlling it.
  • Assembly statements can be inserted into C programs.

Linker

  • Stitches together object files into a single binary program file (executable or library).
  • Includes all .o files of the user program and referenced system libraries.
  • Creates static (.a, .lib) or dynamic (.so, .dll) libraries inserted in the executable file or linked at runtime.
  • Undefined reference errors typically arise from the linker being unable to find symbol definitions.

What the Linker Does

  • Searches object files for tables of external references (e.g., printf).
  • Reads libraries to find matching external definitions (e.g., printf function in stdio.h).
  • Libraries can be static or dynamic.
  • Static Linking: Includes definitions (code, global variables) in the program file and resolves references.
  • Dynamic Linking: Notes library files; definition is determined at runtime.

Linking and Paths

  • The linker automatically links standard C libraries, excluding the math library.
  • By default, it searches a limited number of system library directories.
  • The -L flag explicitly gives the linker additional paths.
  • The -l option provides library names for the linker to link with.

Loader

  • Invoked by the command shell or program to execute a program file.
  • OS opens a fresh process; loader copies program file segments into memory.
  • OS creates a stack and heap, then transfers control to the program's first instruction.
  • Errors while loading shared libraries are handled by the loader.

Loader (dlopen)

  • Used by programs with dynamically linked libraries.
  • ld.so loads these when the program starts.
  • Referenced calls are fixed up on demand.
  • dlopen loads shared object plugins on demand at runtime.

Makefiles

  • The make utility runs commands from a description file to create executables and perform other tasks.
  • Tasks include removing files, reporting project status, packaging and installing files, and building libraries.
  • The make utility is not unique; ant is a similar utility for Java.
  • Cmake is an improved make that examines file dependencies.
  • If files don't exist, it attempts to build them; recompiles if dependent files are newer.

Makefile and Compiler Toolchain

  • make utility and compiler toolchain are separate but often used together to automate code building.

Makefile Structure

  • Each entry in a makefile has three parts:
    • Target: The file to be built.
    • Prerequisites: Files that must exist for the target to be built.
    • Command Line: Command to build the target using the dependencies.

Invoking Make

  • Typing make builds the first target in a file named Makefile or makefile.
  • Typing make <target> builds the specified target.

Other Common Targets

  • Targets can be non-files, e.g., make clean to delete *.o files and the core file.

Checking Commands

  • You can view the commands that make would execute using the -n flag.

Multiline Command Lines

  • Use a semicolon to separate multiple command lines.
libawesome.a: awesome.c
	gcc awesome.c -o awesome.o -c; \
	ar cr libawesome.a awesome.o
  • The backslash indicates continuation to the next line.

Makefile Macros

  • Macros avoid repetitive typing in makefiles.
  • Defined using the = sign, e.g., LIBS = -L/usr/local/lib -lm -llibname.
  • Referenced using $(LIBS) or ${LIBS}.
  • Undefined macros are replaced with a null string.

Predefined Macros

  • Macro CC is predefined as command cc (symbolic link to the default C compiler, usually GNU or LLVM).
  • Macro LD is predetermined as command ld.
  • These can be used or replaced. E.g. CC = gcc.

Macro Strings and Substitutions

  • Allows string substitution in macros, e.g., SRCS = a.c b.c c.c and $(SRCS:.c=.o) translates to a.o b.o c.o.
  • $(SRCS:.c=) translates to a b c; executable names are derived from source file names.

Suffix Rules

  • Tell the system how to compile different types of files.
  • By convention .c files, and make has built-in rules to compile source into executable files.
  • The -p option shows all predefined macros.

Comments

  • Start with # and continue to the end of the line.

Flags

  • Flags control options for the tools and can be controlled using makefiles.

Preprocessor Flags

  • CPPFLAGS = -I<include_file_dir> adds to the include paths.
  • -Dsymbol[=value] is equivalent to #define symbol value; -DNDEBUG disables assertions.

Compiler Flags

  • CFLAGS=
    • -g: Save symbols for debugger
    • -On: Optimization level (0,1,2,3)
    • -Wall -std=c11: All warnings, C11 standard
    • -fpic: Position-independent code (for shared object library)
    • -c: Compile and assemble, but do not link

Linker Flags

  • LDFLAGS=
    • -L<library_dir> pass a library path to the linker
    • -l<library> link in library
    • -shared: Create shared object lib.
  • -o filename: Creates an output file with a specific name, instead of the default.

Creating Libraries

  • Libraries consist of precompiled functions and data.

Library API

  • (Application Programming Interface): Interface for the user of a library.
  • It is made of all the public components of your interface – functions, constants, custom types etc.

Library Design

  • Programmer decides the functionality of library components, function specifics (arguments, return, errors), and clear naming.
  • In C, libraries of functions are created; in OOP languages, libraries of classes are created.

Library Design Public Aspects

  • Crucial due to API exposure.
  • The decision process for the API is similar to deciding on public methods of a class.

Library Design Private Aspects

  • Achieved via information hiding: hiding implementation details from the user.
  • C lacks tools for making library internals truly inaccessible.

Libraries in Practice

  • System-wide libraries on Linux are in /lib and /usr/lib, and headers are in /usr/include.
  • Math library file is libm.so, with header file math.h (names not necessarily the same).

Static and Dynamic Libraries

  • Static Libraries: Library contents copied and stored in the final executable at compile time.
  • Dynamic Libraries: Linked at runtime; copy not stored in executable.
  • Dynamic libraries, also known as shared libraries, are .so in Unix/Linux and .dll in Windows..

Static Libs

  • Pros: Self-contained executable, beginner friendly.
  • Cons: Large and inflexible executable, library cannot be updated without recompile.

Dynamic Libs

  • Pros: Executable can link to different library versions; executable is not bloated.
  • Cons: Single executable is less convenient; requires more experience.
  • Dynamic libraries are common in real-world applications.

Naming Libraries

  • All names begin with lib.
  • Static libraries end with .a.
  • Dynamic libraries end with .so (Linux) or .dll (Windows); .dylib possible on macOS.
  • Shared objects can have version numbers after .so: libm.so.3.
  • Create multiple names for the same library with symbolic links.
  • Library itself is called “a”: liba.so.3.2 may have links liba.so.3 and liba.so.

Creating a Dynamic (Shared) Library

  • Easy to do with gcc.
  • Enable Position Independent Code (PIC) for memory location independence.
  • Appropriate compiler/linker arguments:
    • gcc -c -fpic record.c: Creates .o file.
    • gcc -shared -o librecord.so record.o: Creates .so file.

Using a Library

  • Compile step: Convert source code into object file using -c with gcc.
  • Link step: Links the library to the program using -lxyz (lowercase L followed by library name).
  • Provide the unique part of the library name (not entire name).

Allocating Memory in a function

  • Modify something in a function by passing its address (i.e., a pointer).
  • Modify a value in a function by passing it by reference.
  • Modifications will only change a copy of the value otherwise.
  • The value of a pointer variable p is a memory address. Can be initially null.

Valgrind

  • A memory debugging tool that can check for memory leaks and diagnose various other memory errors.
  • Need the -g compiler flag to use.

Valgrind and Memory Leaks

  • Marks memory blocks as four types:
    • Definitely lost: Leaking memory.
    • Indirectly lost: Pointer-based structure leaking memory (e.g., tree with lost root).
    • Possibly lost: Program may be leaking memory due to pointer manipulation.
  • Detect uninitialized values and writes to unallocated memory.

Scope

  • Region over which you can access a variable by name.
  • Defines where a variable is visible.
  • Storage class determines symbol lifetime.
  • Access control determines who can access a visible symbol.

Scope Vs Access Control

  • Java has 4 access levels for methods and variables: private, protected, package level, and public.
  • A class variable with class-wide scope is visible to all class/objects, but access control can restrict access.

Scope in C

  • No access control, but some control over variable visibility.

Four Types of Scope

  • Program scope
  • File scope
  • Function scope
  • Block scope

Scoping Principle

  • Always define symbols in the narrowest scope possible.
  • This prevents errors and improves security.

Program Scope

  • The variable is accessible by all source files that make up the executable like functions and global extern variables.

Program Symbol Concepts

  • Definition: Where a named thing lives (memory location).
  • Reference: Use of the thing by name (must be resolved to location). Declaration: Informs compiler about a name, compiler verifies reference correctness.

External Symbols

  • Program scope symbols are passed to the linker in .o files.
    • External definition: "extdef"
    • External reference: “extref"

Externals

  • Assembly allows linking of programs via program scope, with each language having its own convention for extdef and extref.

Using Program Scope in C: Function

  • extdef: Is a defined function. Definition appears in one .c file.
  • declaration: Occurs in multiple .c files with header files

Using Program Scope in C: Variable

  • extdef: definition only appears in one .c outside any function, may or may not be initialized
  • declaration: appears anywhere in a file, usually in a header file.

Using Program Scope

  • Decide when to use program scope.
  • Variables should not be globally accessible by users.
  • Program scope functions should be part of the program’s public interface.

File Scope

  • Variables or functions accessible from declaration point to the end of the file.
  • In C, static globals are within a file, not the whole program.
  • Static avoids linker inclusion, preventing external status.

Using File Scope

  • Sits between private and package level in Java.
  • Perfect for internal-use functions or variables.
  • File Global variables lead to hard to find errors

Function Scope

  • Labels following goto have function scope and mean you can jump ahead.

Block(local scope)

  • Variables are accessible after the declaration point to the end of the block with is anything in curly braces. This includes params, variables, and loop counters.
  • Avoid using the same variable names in overlapping scopes.

Scope Vs Storage Class

  • Storage class is where and how long variables are kept.
  • Different from scope
  • Static affects both storage and scope.

Automatic Storage

  • Associated with functions.
  • Fresh temporary initialized copy on stack for each call, enabling recursion.

Automatic Storage Warning

  • Never return pointers to automatic variables, as they disappear when function returns.

Static Storage

  • Only one instance of variable in executable program
  • Includes program scope, static file/local variables.
  • static restricts variable to file scope and changes local variables from automatic to static storage.

Global/External Storage

  • Program Scope variables exist as long as program is running.

Dynamic Storage

  • A storage class that create storage temporarily on the heap via malloc calloc and realloc. They must be freed via free. Address of these pointers must be stored. The storage is available until you terminate program.

Problems with Precedence

  • Common precedence problems:
    • *p.f means *(p.f)
    • int *ap[] means int *(ap[])
  • Use parentheses for clarity.

Other Common Constructs

int * fp() is function returning a pointer to int rather than pointer returning int. Use parentheses.

c = getchar() != EOF needs Parentheses since comparators are of higher precedence than assignment

Typedefs

  • Typedefs are aliases or shorthand notations for existing C types.
  • The following declares structure, and typedefs alias
Struct Vec2 {
Float x, y,
}
typedef struct Vec2 Vec2;

Types

  • Help to make code more readable
  • Offload some of the error checking onto the compiler
  • Builds good programming habits that will be useful when using languages with stronger type systems
  • Using types correctly also helps you to avoid precision issues

Rules for Using Types

  • Always pick the appropriate type for the job and use the narrowest type
  • Use the C bool type instead of int is always true or false

Enumerations

  • Or enum, is a data type consisting of a finite set of named values.
  • The following is the template for using enums
Enum CardSuit {CLUBS, DIAMONDS, HEARTS, SPADES};
  • Assigns each enum with unique integer

Studying That Suits You

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

Quiz Team

Related Documents

C Tool Chain Review

Description

This quiz covers C/C++ concepts like the difference between float and double, enumeration representation, memory management benefits, appropriate enumeration use cases and Makefile functionalities. Also covers inspecting make commands and multi-line commands in Makefiles. Aimed at evaluating understanding of fundamental programming concepts in C/C++.

More Like This

Use Quizgecko on...
Browser
Browser