Introduction to C and C++

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Which programming paradigm does the C language primarily support?

  • Procedural programming (correct)
  • Logic programming
  • Object-oriented programming
  • Functional programming

In what year was the C programming language developed?

  • 1985
  • 1969
  • 1972 (correct)
  • 1978

Which operating system was primarily developed using the C programming language?

  • Windows
  • UNIX (correct)
  • macOS
  • Android

Which of the following features is NOT supported by the C programming language?

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

What is the correct file extension for a C program source file?

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

Which of the following is an Integrated Development Environment (IDE) commonly used for C/C++ development?

<p>Visual Studio (D)</p> Signup and view all the answers

Which of the following is a C/C++ compiler commonly used on Linux systems?

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

Which of the following is a feature of C++ but NOT C?

<p>Namespace feature (D)</p> Signup and view all the answers

What is the purpose of the #include directive in a C program?

<p>To include a header file (C)</p> Signup and view all the answers

Which of the following is the correct syntax to print output to the console in C?

<p><code>printf()</code> (D)</p> Signup and view all the answers

What is the purpose of the return 0; statement at the end of the main() function in a C program?

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

Which of the following is the correct function to read formatted input from the keyboard in C?

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

During the compilation process, what is the role of the preprocessor?

<p>Handles directives like <code>#include</code> and macro expansions (B)</p> Signup and view all the answers

Which stage of compilation translates assembly code into machine code?

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

What is the main function of the linker in the compilation process?

<p>To combine object files and libraries to create an executable file (A)</p> Signup and view all the answers

What does the term 'preprocessing' refer to in the context of C compilation?

<p>The initial stage where directives are processed and code is prepared for compilation (B)</p> Signup and view all the answers

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

<p>Converting preprocessed code to assembly instructions (A)</p> Signup and view all the answers

What is the result of the assembly stage in C compilation?

<p>Object file (C)</p> Signup and view all the answers

What is the purpose of the linker stage in C compilation?

<p>Combining object files and libraries into an executable program (B)</p> Signup and view all the answers

What is the primary difference between scanf and scanf_s in C?

<p><code>scanf_s</code> is a more secure version, as it checks buffer sizes to prevent overflows. (D)</p> Signup and view all the answers

What is the purpose of conversion specifiers in printf?

<p>They define the format for printing variables. (A)</p> Signup and view all the answers

Which conversion specifier is used in printf to print a signed decimal integer?

<p>%d (D)</p> Signup and view all the answers

Which conversion specifier is used in printf to print an unsigned octal integer?

<p><code>%o</code> (A)</p> Signup and view all the answers

Which conversion specifier is used in printf to print a character?

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

In printf, what is the purpose of a 'length modifier'?

<p>To specify the size of the data type being printed (e.g., short, long) (D)</p> Signup and view all the answers

In the context of format control strings for printf, what is the purpose of flags?

<p>To modify the formatting of the output (e.g., justification, sign) (C)</p> Signup and view all the answers

Which escape sequence is used to insert a newline character in C?

<p>\n (D)</p> Signup and view all the answers

Which escape sequence is used to insert a horizontal tab in C?

<p>\t (C)</p> Signup and view all the answers

What is the storage size of an int data type in C?

<p>2 or 4 bytes (A)</p> Signup and view all the answers

What is the range of values for a signed char data type in C?

<p>-128 to 127 (B)</p> Signup and view all the answers

According to operator precedence in C, which operation is evaluated first in the expression (a + b) * c?

<p>Parentheses <code>(a + b)</code> (A)</p> Signup and view all the answers

In C, what is the meaning of the && operator?

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

In C, what is the purpose of the | operator?

<p>Bitwise OR (C)</p> Signup and view all the answers

What is the result of the bitwise AND operation between 5 (0101 in binary) and 3 (0011 in binary)?

<p>1 (0001) (D)</p> Signup and view all the answers

What is the result of the bitwise XOR operation between 12 (1100 in binary) and 5 (0101 in binary)?

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

What is the effect of the left shift operator <<?

<p>Shifts bits to the left (D)</p> Signup and view all the answers

What does the bitwise complement operator ~ do?

<p>Inverts all the bits of a number (D)</p> Signup and view all the answers

In the given code, what will the program count until before terminating? EOF (ctrl + z on windows, ctrl+d on linux and mac)

<p>Letter grades (D)</p> Signup and view all the answers

Signup and view all the answers

Flashcards

What is C?

A general-purpose, procedural programming language.

How are C programs written?

Text files with .c extension.

What is a Compiler?

Translates source code to machine code.

Compilation in C vs. C++

C compiles to machine code, C++ compiles to machine code

Signup and view all the flashcards

C++ paradigm

Both Procedural and OOP (Hybrid)

Signup and view all the flashcards

What is #include <stdio.h>?

a directive to the C preprocessor to include contents of a standard input/output header.

Signup and view all the flashcards

What is int main()?

The main function where program execution begins.

Signup and view all the flashcards

What does printf() do?

Prints output to the standard output stream (screen).

Signup and view all the flashcards

What does scanf() do?

Input data from standard input stream (keyboard)

Signup and view all the flashcards

What does %d conversion specifier do?

Display as a signed decimal integer.

Signup and view all the flashcards

What is Preprocessing?

The first stage of compilation

Signup and view all the flashcards

What are preprocessor commands?

Lines starting with # that are interpreted by the preprocessor.

Signup and view all the flashcards

What is Compilation?

Translates preprocessed code into assembly instructions.

Signup and view all the flashcards

What is Assembly?

Translates assembly instructions into machine code or object code.

Signup and view all the flashcards

What is Linking?

Arranges pieces of object code and adds library functions to create an executable program.

Signup and view all the flashcards

What does ' escape sequence do?

Output the single quote (') character.

Signup and view all the flashcards

What does " escape sequence do?

Output the double quote (") character.

Signup and view all the flashcards

What does ? escape sequence do?

Output the question mark (?) character.

Signup and view all the flashcards

What does \a escape sequence do?

Cause an audible or visual alert.

Signup and view all the flashcards

What does \n escape sequence do?

Move the cursor to the beginning of the next line.

Signup and view all the flashcards

scanf_s() arguments

Width specification, memory address, buffer size.

Signup and view all the flashcards

() precedence

Evaluated first.

Signup and view all the flashcards

= precedence

Evaluated last.

Signup and view all the flashcards

Bitwise AND (&)

Sets each bit in the result to 1 if both operands is 1

Signup and view all the flashcards

Bitwise OR (|)

Sets each bit in the result to 1 if the corresponding bit in either (or both) operand(s) is 1

Signup and view all the flashcards

Bitwise XOR (^)

Sets each bit to 1 if the corresponding bit in exactly one operand is 1

Signup and view all the flashcards

Left shift (<<)

Shifts bits of its left operand to the left by the number specified

Signup and view all the flashcards

Right shift (>>)

Shifts bits to the right by the number specified

Signup and view all the flashcards

Bitwise complement (~)

Sets all 0 bits to 1 and all 1 bits to 0

Signup and view all the flashcards

Study Notes

  • C is a general-purpose, procedural programming language.
  • It was developed in 1972 to develop the UNIX operating system.
  • C doesn't support OOP, meaning no polymorphism, inheritance, or encapsulation.
  • C programs are written in plain text files with the extension ".c".

C/C++ Compilers and IDEs

  • Many C/C++ compilers and IDEs exist.
  • IDEs include Code::blocks (Windows/Linux/MacOS), Visual Studio (Windows), and Dev-C++ (Windows).
  • Compilers include GCC (Linux), MinGW (Minimalist GNU for Windows), and Clang (OS X).
  • There have been four standard revisions: C++11, C++14, and C++17

C vs C++

  • C uses procedural programming.
  • C does not support polymorphism, encapsulation, and inheritance.
  • C does not support function and operator overloading.
  • C lacks namespace features
  • C does not have virtual and friend functions
  • C uses scanf for input and printf for output.
  • C uses malloc() and free() for memory allocation/deallocation.
  • C++ uses both procedural and OOP (Hybrid) programming.
  • C++ supports polymorphism, encapsulation, and inheritance.
  • C++ supports function and operator overloading.
  • C++ uses the namespace feature.
  • C++ supports virtual and friend functions.
  • C++ uses cin for input and cout for output.
  • C++ uses new and delete for memory allocation/deallocation.

C++ vs Java

  • C++ compiles to machine code.
  • C++ supports pointers and references.
  • C++ utilizes manual garbage collection.
  • C++ offers operator and method overloading.
  • C++ supports multiple inheritance.
  • C++ supports structures and unions.
  • C++ provides access specifiers when inheriting from a class.
  • Java compiles to byte code
  • Java doesn't support pointers
  • Java utilizes automatic garbage collection.
  • Java does not have operator overloading.
  • Java uses single inheritance.
  • Java has no support for structures and unions.
  • Java, access specifiers are not supported when inheriting.

First C Example

  • The statement #include <stdio.h> is a directive to the C preprocessor.
  • It tells the preprocessor to include the contents of the standard input/output header (<stdio.h>) in the program.
  • #include is similar to import in Java but the code is copied and added to the current file, increasing its size.
  • Every program in C contains one or more functions, one of them must be the main function.
  • The program begins executing with the main() function.
  • The returning value of the main function is an integer.
  • The statement printf("Welcome to CMPS270! \n"); prints to the standard output.

Compiling Stages

  • Compilation of C code involves four stages: preprocessing, compilation, assembly, and linking.
  • Preprocessing is the first stage of compilation.
  • Lines starting with a # are interpreted by the preprocessor as preprocessor commands.
  • These commands form a simple macro language with its own syntax.
  • It reduces repetition in source code
  • Compilation translates the preprocessed code to assembly instructions specific to the target processor architecture.
  • It forms intermediate, human-readable language.
  • Some compilers support an integrated assembler that directly generates machine code.
  • Assembly translates assembly instructions into machine code or object code using an assembler.
  • The output is the actual instructions for the target processor.
  • An object file with the extension .obj is created.
  • Linking: The object code, generated in the previous stage, is raw machine instructions that the processor understands.
  • The linker arranges pieces of object code and adds pieces for library functions used by the program.
  • The result of this stage is the final executable program.

Input / Output

  • scanf reads input data from the standard input stream (keyboard).
  • printf outputs data to the standard output stream (screen).
  • printf contains a format control string that describes the format of the output.

Printf Format Control String

  • Possible format control string consists of conversion specifiers (like %d, %f, %c, %s), flags (like +, -, 0), field widths, precisions, and literal characters.
  • Possible formats include rounding floating-point values to a specific number of decimal places, aligning numbers in a column, and output justification (right / left).

Integer Conversion Specifiers

  • d: Display as a signed decimal integer.
  • i: Display as a signed decimal integer. The i and d specifiers are different when used with scanf.
  • o: Display as an unsigned octal integer.
  • u: Display as an unsigned decimal integer.
  • x or X: Display as an unsigned hexadecimal integer. X causes the digits 0-9 and the uppercase letters A-F to be used in the display and x causes the digits 0-9 and the lowercase letters a-f to be used in the display.
  • h, l, or ll: Placed before any integer conversion specifier, these indicate that a short, long, or long long integer is displayed, respectively. They are called length modifiers.

Conversion Specifiers

  • e or E: Display a floating-point value in exponential notation.
  • f or F: Display floating-point values in fixed-point notation (F is supported in the Microsoft Visual C++ compiler in Visual Studio 2015 and higher).
  • g or G: Display a floating-point value in either the floating-point form f or the exponential form e (or E), based on the magnitude of the value.
  • L: Place before any floating-point conversion specifier to indicate that a long double floating-point value should be displayed.
  • p: Display a pointer value in an implementation-defined manner.
  • %: Display the percent character.
  • c: Display a character argument.
  • s: Displays a string (array of characters).

Format String Flags

  • - (minus sign): Left justify the output within the specified field.
    • Display a plus sign preceding positive values and a minus sign preceding negative values.
  • space Print a space before a positive value not printed with the + flag.
  • #: Prefix 0 to the output value when used with the octal conversion specifier o.
  • Prefix 0x or 0X to the output value when used with the hexadecimal conversion specifiers x or X.
  • Force a decimal point for a floating-point number printed with e, E, f, g or G that doesn't contain a fractional part.
  • (Normally the decimal point is printed only if a digit follows it.) For g and G specifiers, trailing zeros are not eliminated.
  • 0 (zero) Pad a field with leading zeros.

Escape Sequences

  • ': Output the single quote (') character.
  • ": Output the double quote (") character.
  • ?: Output the question mark (?) character.
  • \: Output the backslash () character.
  • \a: Cause an audible (bell) or visual alert (typically, flashing the window in which the program is running).
  • \b: Move the cursor back one position on the current line.
  • \f: Move the cursor to the start of the next logical page.
  • \n: Move the cursor to the beginning of the next line.
  • \r: Move the cursor to the beginning of the current line.
  • \t: Move the cursor to the next horizontal tab position.
  • \v: Move the cursor to the next vertical tab position.

Scanf_s

  • scanf(): reads input from the standard input. If the input exceeds the variable's size, it overrides the adjacent memory.
  • scanf_s(): a safer function takes width specification, memory address, buffer size, and terminating character.
  • scanf_s() checks the size of the buffer and prevents writing to the buffer if the input is too large.

C Data Types

  • char: 1 byte, range: -128 to 127 or 0 to 255.
  • unsigned char: 1 byte, range: 0 to 255.
  • signed char: 1 byte, range: -128 to 127.
  • int: 2 or 4 bytes, range: -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647.
  • unsigned int: 2 or 4 bytes, range: 0 to 65,535 or 0 to 4,294,967,295.
  • short: 2 bytes, range: -32,768 to 32,767.
  • unsigned short: 2 bytes, range: 0 to 65,535.
  • long: 4 bytes, range: -2,147,483,648 to 2,147,483,647.
  • unsigned long: 4 bytes, range: 0 to 4,294,967,295.

C / C++ Operators

  • Parentheses (): Evaluated first.
  • Multiplication (*), Division (/), Remainder (%) Evaluated second from left to right.
  • Addition (+), Subtraction (-) Evaluated third from left to right.
  • Assignment (=) Evaluated last.

Relational Operators

  • Relational operators compare two values and return a Boolean result.
  • Equality operators check for equality or inequality.
  • Logical operators (&&, ||, !) perform logical AND, OR, and NOT operations.

Bitwise Operators

  • Bitwise operators:
    • AND (&): Sets each bit in the result to 1 if both operands are 1, otherwise 0.
    • inclusive OR (|): Sets each bit to 1 if either (or both) operand(s) is 1.
    • exclusive OR (^): Sets each bit to 1 if exactly one operand is 1; otherwise, it sets the bit to 0.
    • Left shift (<<): Shifts bits to the left by the number specified.
    • Right shift (>>): Shifts bits to the right by the number specified.
    • Complement (~): Sets all 0 bits to 1 and all 1 bits to 0.
  • Bitwise AND, OR, and XOR compare their operands bit by bit.

Studying That Suits You

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

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser