C Programming: Stages, Errors

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 best describes computer programming?

  • Designing computer hardware components.
  • Creating a sequence of instructions for a computer to perform a specific task. (correct)
  • Creating artificial intelligence to mimic human behavior.
  • Developing new operating systems.

During which stage of computer programming is the program logic developed to accomplish a task?

  • Problem statement
  • Algorithm development (correct)
  • Program coding
  • Program testing

What is the primary purpose of program documentation in the context of computer programming?

  • To fully document the programming process (correct)
  • To provide a clear written statement of the problem
  • To test the program for errors
  • To convert program logic into specific syntax

Which type of programming error is NOT detected by the compiler?

<p>Logic error (D)</p> Signup and view all the answers

What is the key characteristic of a run-time error?

<p>It occurs during the execution of the program. (A)</p> Signup and view all the answers

In C programming, what is the role of the C Preprocessor?

<p>It is a program that is executed before the source code is compiled. (D)</p> Signup and view all the answers

What is the purpose of the #include directive in C programming?

<p>To give the program access to a library of pre-written code (B)</p> Signup and view all the answers

Which of the following statements about the main function in C is correct?

<p>The <code>main</code> function is where program execution begins. (B)</p> Signup and view all the answers

In C programming, what is the purpose of the curly braces {}?

<p>To enclose the body of a function (D)</p> Signup and view all the answers

What is the role of 'declaration' within a C function?

<p>To tell the compiler the names of memory cells needed in the function (B)</p> Signup and view all the answers

What does 'variable declaration' achieve in C programming?

<p>It communicates the name of variables and the type of information they will store to the compiler. (C)</p> Signup and view all the answers

Why is it important for a programmer to consider how to allocate memory for data?

<p>To handle data efficiently and effectively (D)</p> Signup and view all the answers

Which of the following is NOT a standard pre-defined data type in C?

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

What is the primary function of 'modifiers' in C data types?

<p>To define the amount of storage allocated to the variable (B)</p> Signup and view all the answers

Which of the following describes what a 'variable' is in C programming?

<p>A container in the computer's memory that can store, retrieve, or modify a value (B)</p> Signup and view all the answers

Which rule must an identifier in C programming adhere to?

<p>It must not be a reserved word. (D)</p> Signup and view all the answers

Which of the following statements related to Variable Naming in C is correct?

<p>At least the first three characters of a variable name are significant. (C)</p> Signup and view all the answers

What is the function of the equal sign (=) in C programming?

<p>It assigns the value on the right to the variable on the left. (A)</p> Signup and view all the answers

What is the function of the address operator (&) in C programming?

<p>It returns the address of a variable. (C)</p> Signup and view all the answers

In the context of the scanf function, what is the purpose of the format specifier?

<p>To define the type of data to be accepted from the standard input (B)</p> Signup and view all the answers

Which format specifier is used in scanf to accept a float?

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

If you want to read two integer values using scanf, how should you structure the function call?

<p><code>scanf(&quot;%d%d&quot;, &amp;num1, &amp;num2);</code> (B)</p> Signup and view all the answers

What will printf(“%d”, 5); display?

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

What is meant by 'control structure' in programming?

<p>The sequence of execution of a group of statements. (D)</p> Signup and view all the answers

Which type of control structure executes statements in a sequential manner?

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

When defining a programming problem, which of the following steps comes directly before identifying the process?

<p>Identifying the input. (B)</p> Signup and view all the answers

In C programming, what is the purpose of the #define directive?

<p>To allow for text substitutions before compiling the program (B)</p> Signup and view all the answers

In C, should identifiers that must be changed by the preprocessor be written in capital or lowercase letters?

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

In C programming, what are 'libraries'?

<p>Collections of functions and symbols that can be accessed by a program (D)</p> Signup and view all the answers

How is a predefined library opened in C?

<p>Using angle brackets, e.g., <code>&lt;library.h&gt;</code> (B)</p> Signup and view all the answers

What is the primary purpose of comments in C code?

<p>To provide explanations and annotations for human readers of the code (C)</p> Signup and view all the answers

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

<p>The compiler ignores all comments. (C)</p> Signup and view all the answers

In C, what is a 'reserved word'?

<p>A word with a special meaning that cannot be used for other purposes (C)</p> Signup and view all the answers

In C programming, what is the purpose of punctuation marks?

<p>To define the structure and grammar of the code (A)</p> Signup and view all the answers

Which punctuation mark is used as a statement separator?

<p>Semicolon (;) (D)</p> Signup and view all the answers

In C programming, what is the key attribute of 'constants'?

<p>Constants should be initialized during the first declaration (A)</p> Signup and view all the answers

How is a constant or a read-only variable created in C?

<p>Using the <code>const</code> keyword (B)</p> Signup and view all the answers

What is the purpose of 'backslash character constants' in C?

<p>To represent characters that are hard to type directly (C)</p> Signup and view all the answers

What effect does the unary minus operator have on a variable?

<p>Changes the sign of a value or variable. (D)</p> Signup and view all the answers

What is the difference between prefix and postfix increment operators?

<p>Prefix increments before the value is used; postfix increments after the value is used. (B)</p> Signup and view all the answers

In which header file are the predefined mathematical functions in C located?

<p>math.h (C)</p> Signup and view all the answers

What data type do the functions from the math.h library accept as an argument?

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

Flashcards

Computer Programming

Creating a sequence of instructions to enable the computer to do something.

Programming Language

An artificial language to control the behavior of a machine, particularly a computer.

Problem Statement

A clear, written statement of the problem to be solved by the computer.

Algorithm Development

Developing the program logic through a sequence of steps.

Signup and view all the flashcards

Program Coding

Converting program logic into the specific syntax of a programming language.

Signup and view all the flashcards

Program Testing

Checking a coded program for errors.

Signup and view all the flashcards

Program Documentation

Completing the programming process with full documentation.

Signup and view all the flashcards

Syntax Error

Occurs when your code violates one or more grammar rules of C.

Signup and view all the flashcards

Run-time Error

Detected during program execution when the computer performs illegal operations.

Signup and view all the flashcards

Logic Error

Occurs when a program follows a faulty algorithm.

Signup and view all the flashcards

#include directive

Gives program access to a library.

Signup and view all the flashcards

Main Function

Where program execution begins.

Signup and view all the flashcards

Declaration

Part of the program that tells the compiler the names of memory cells.

Signup and view all the flashcards

Executable Statements

Derived statements from the algorithm into machine language.

Signup and view all the flashcards

Variable Declaration

Communicates to the C compiler the names of all variables used in the program.

Signup and view all the flashcards

Data Type

A set of values and set of operations that can be performed on those values.

Signup and view all the flashcards

Variable

Like a container in your computer's memory.

Signup and view all the flashcards

Identifier

Name that identifies or labels the identity of an object.

Signup and view all the flashcards

Assignment Operator (=)

Assigns the value on its right to the variable on its left.

Signup and view all the flashcards

Binary Operators

Arithmetic operators that take two operands and return a result.

Signup and view all the flashcards

Address Operator (&)

Returns the address of a variable.

Signup and view all the flashcards

scanf()

Accepts data from the standard input, usually the keyboard.

Signup and view all the flashcards

Format Specifier (scanf)

Defines the type of input data to be accepted by scanf.

Signup and view all the flashcards

printf()

Writes formatted output to the standard output device, such as the monitor.

Signup and view all the flashcards

Format Specifier (printf)

Defines the type of data to be printed in the standard output.

Signup and view all the flashcards

Control Structure

Specifies the sequence of execution of a group of statements.

Signup and view all the flashcards

Sequential Control Structure

Statements are executed in the order of their appearance in the source code.

Signup and view all the flashcards

#define Directive

Allows you to make text substitutions before compiling the program.

Signup and view all the flashcards

Libraries

C implementations that contain collections of useful functions and symbols.

Signup and view all the flashcards

Comments

Inform code what the code does for revisits, ignored by the compiler.

Signup and view all the flashcards

Reserved Word

Word with a special meaning in C; cannot be used as identifier.

Signup and view all the flashcards

Constants

Identifiers with constant Value throughout program execution.

Signup and view all the flashcards

Unary Operators (+/-)

Changes the sign of a value or variable.

Signup and view all the flashcards

Increment/Decrement Operators

Adds one to (++) or subtracts one from (--) the value of a variable.

Signup and view all the flashcards

Math functions

Functions in library math.h take double and return double

Signup and view all the flashcards

getchar()

Reads a single character from a standard input.

Signup and view all the flashcards

getch()

Allows the user to input a character and wait for the enter key.

Signup and view all the flashcards

getche()

Allows the user to input a character and there is no need for the enter key.

Signup and view all the flashcards

gets()

Allows the user to input a sequence of characters or string.

Signup and view all the flashcards

puts()

Writes the string to the standard output device and positions the cursor to the next line.

Signup and view all the flashcards

putchar()

Writes a single character to the screen.

Signup and view all the flashcards

Relational Operator

Evaluates to true or false.

Signup and view all the flashcards

Logical Operator

Evaluates to true or false.

Signup and view all the flashcards

If Statement

The if statement performs a task or a group of tasks when the condition is true or false

Signup and view all the flashcards

Switch statement

In the switch statement, the variable or the controlling expression, is evaluated and compared to each of the case labels in the case constant until a match is found.

Signup and view all the flashcards

Study Notes

C Programming Basics

  • Computer programming involves creating a sequence of instructions for a computer
  • A programming language is an artificial language to control a machine's behavior, especially a computer.

Stages of Computer Programming

  • Problem statement: Start with a clear written definition of the problem
  • Algorithm development: Create a program logic to accomplish the task, visualized with tools like flowcharts.
    • Algorithm is a set of steps to accomplish a task
  • Program coding: Convert the logic into a programming language's specific syntax
  • Program testing: Check the coded program for errors
  • Program documentation: fully document complete programming process

Common Programming Errors

  • Syntax error: Violates grammar rules, detected by the compiler, preventing translation and execution
  • Run-time error: Detected during program execution, usually due to illegal operations, halting the program
  • Logic error: Program follows a faulty algorithm, difficult to detect, resulting in incorrect output

Turbo C History and Definition

  • Turbo C was created by Dennis MacAlistair Ritchie at AT&T Bell Laboratories
  • Turbo C is an Integrated Development Environment and compiler for the C programming language by Borland
    • Notably first introduced in 1987, the solution features comprehensive manuals, fast compile times, small size and low price

Turbo C General Structure

  • Programs start with preprocessor directives
  • #include gives access to libraries, inserting definitions from header files before compilation
    • #include<stdio.h> opens "stdio.h" for functions like scanf and `printf
  • Every C program needs a main function where execution starts, name cannot be changed
  • main function body is enclosed in braces {} comprised of declarations (memory cell names) and executable statements (derived from the algorithm).

Variable Declaration

  • Important to consider how to allocate memory to handle data
  • Variable declaration is a statement that communicates to the C compiler the names of all variables and the kind of informaiton stored
  • Variables are declared before use in C
  • Syntax: Data type variable list;
    • Example: int x, age;

Data Types

  • Data Type is a set of values and operations on those values.
  • Standard pre-defined data types include:
    • char
    • double
    • int
  • Modifiers: they can be qualified by short, long, signed, or unsigned
    • short int <= int <= long int
    • float <= double <= long double
  • Seven basic data types:
    • Text (char): single characters/strings, 1 byte, range 0-255
    • Integer
    • Floating-point
    • Double-floating point
    • Enumerated
    • void: signifies values that occupy 0 bit and have no value
    • Pointer: contains the address of the memory location

Defining Data Types

  • int: integer numbers
    • Example: Int Count; Count = 5;
  • float: floating point numbers
    • Example: float Miles; Miles = 5.6;
  • double: BIG floating point numbers
    • Example: double Atoms, Atoms = 2500000;
  • char: characters
    • Example: char Letter; Letter = 'x';

Variables

  • Variable is a container in computer memory which stores, retrieves, and modifies values
  • It is associated with a memory cell, whose value can change

Identifiers

  • Identifier is the name that identifies or labels the identity of an object
  • Key naming conventions:
    • Made up of letters and digits
    • Must start with a letter
    • C is case-sensitive
    • Underscore _ is considered as a letter
      • not recommended as the first character
    • First 3 characters of a name are significant
    • Should not be a reserved word/keyword in C

Fundamental Commands and Operators

  • Equal sign (=): assignment operator, assigns the right value (rvalue) to the left variable (lvalue), example: lvalue = rvalue;
  • Binary operators: arithmetic operators with two operands
    • +: adds
    • -: subtracts
    • *: multiplies
    • /: divides
    • %: computes remainder
  • Address operator (&): returns the address of a variable
    • Important when getting an address in order to assign the data

Fundamental Input Command - scanf

  • scanf() accepts data from standard input (keyboard), primarily for numeric data
  • Syntax: scanf(“format specifier”, &var_name);
  • The address operator returns the address of the variable to assign the data
  • Assignment is done by position, separated with whitespace
  • Format specifiers:
    • %Lf: Long double
    • %lf: Double
    • %f: Float
    • %lu: Unsigned long int
    • %ld: Long int
    • %u: Unsigned int
    • %d: Int
    • %hd: Short
    • %c: Char

Fundamental Output Command - printf

  • printf() writes formatted output to the standard output (monitor) which can be a string, formatted value, or mixed expression
  • Syntax:
    • printf(“string expression”);
    • printf(“format specifier”, var_name);
    • printf(“format specifier with string expression”, var_name);
  • Format specifiers are the same as scanf()

Control Structure

  • Control structure specifies the sequence of execution of a group of statements
    • namely sequential, conditional, and iterative.

Defining a Programming Problem

  • Five steps for defining a programming problem:
    • Restate the problem
    • Analyze the problem
    • Identify the output
    • Identify the input
    • Identify the process.
  • Variables should use descriptive names and appropriate data types.

C Language Elements: #define

  • Directive define allows for text substitutions before compiling.
  • Identifiers for preprocessor changes are in capital letters by convention.
    • Example: #define MAX 10 (MAX is a substitute of 10, every time MAX is stated, it means 10.)

Libraries

  • Libraries are C implementations that contain collections of commonly used functions with the name ending in .h
  • The #include call is used to open the file
    • Angle brackets <> for predefined, " " for user-defined

Comments

  • C provides comments using multi-line remarks with /* */ and single-line with preceding slashes //
  • Nested comments are not allowed
  • Used to inform what code does and ignored by the compiler, thus not affecting program efficiency
  • Can also be used to comment out sections of code when debugging

Reserved Words

  • Reserved word is a word used in C language for a specific use and not to be used for anything else
  • There is a complete list of 32 C reserved words

Punctuation Marks

  • Character: Use
  • /**/: enclose are single line remarks
  • : display series of characters, and initializing string constant
  • ;: statement separator
  • ,: separate one variable to another
  • =: assignment operator
  • ': initializing character expresssion
  • &: address operator
  • {}: denotes the beginning and end of the program

Constants

  • Constant identifiers have a constant value throughout program execution and written in uppercase letters by convention
  • To create a read-only variable, use the keyword const keyword
    • Syntax: const float PI = 3.14
  • Types of constants:
    • Character constants are put in single quotes
      • examles: 'A', '+'
    • Integer constants are numbers without fractional parts
      • Examples: 5 and -160
    • Floating constants require decimal points
      • Examples: 16.234, 4.23
    • String constants are sets of characters in double quotes
      • Examples: "bag", "this is good"
    • Backslash character constants are character constants in single quotes for most printing characters

Unary Operators

  • Unary minus (-) or plus (+) changes the sign of a value/variable.

Increment (++) and Decrement (--) Operators

  • ++: adds one to variable value
  • --: subtracts one from variable value The value of the expression depends on where the operator is placed
    • Prefix increment is when the ++ is placed in front of its operand (value of expression is variable's value after increment)
    • Prefix decrement is when the -- is placed in front of its operand (value of expression is variable's value after decrement)
    • Postfix increment is when the ++ is placed immediately after the operand (value of expression is variable's value before increment)
    • Postfix decrement is when the -- is placed immediately after the operand (value of expression is variable's value before decrement)

Predefined Mathematical Functions

  • The math.h library handles math functions, using double as an argument and returning a double
  • Examples include: fabs() (absolute value), ceil() (rounds up), and floor() (rounds down), sqrt() (square root), pow() (power), and fmod() (remainder)

More I/O Functions

  • Input/Output (I/O) functions in C:
    • getchar(): Reads a single character from the standard input.
      • Syntax: var_name = getchar();
    • getch(): Allows the user to input a character and wait for the enter key.
      • No echo is displayed, requires "conio.h"
      • Syntax: var_name = getch();
    • getche(): Allows the user to input a character and there is no need for the enter key
      • Inputted character will be echoed.
      • Syntax: var_name = getche();
    • gets(): Input a sequence of characters or string
      • gets(var_name);, where var_name is an array of characters.
    • puts(): Writes the string to the standard output device positions the cursor to the next line.
      • Syntax: puts(“string expression”);
    • putchar(): Writes a single character to the screen.
      • Syntax: putchar(var_name);
      • Also putchar(int); where the parameter may be a char variable of an integer value for a chatacter

Conditional Control Structure

  • Conditional control structure evaluates condition with a true or false result, where false is 0 and true is any other non-zero value.
    • Utilizes the if statement with optional else or else-if
    • switch-case statement
  • Conditional expressions or statements use these types of operators:
    • Relational operators: evaluate to true or false
      • Includes > (greater than), >= (greater than or equal to), < (less than), <= (less than or equal to), == (equal to), and != (not equal to)
    • Logical operators: logical “and”, “or” and “not”
      • && (and), || (or), and ! (not)
  • Order of Precedence:
    • Arithmetic
      • *, /, %
      • +, -
    • Relational
      • <, <=, >, >=, ==, !=
    • Logical
      • !
      • &&
      • ||

if Statement

  • Performs task when condition is true
  • Syntax for single-if:
if (<conditional expression>)
    <statement>
  • Example:
if(age > 18)
    printf("You are an adult!");
  • For multiple statements, use a pair of braces:
if(age > 18){
    printf("You are an adult!");
    printf("Welcome to the club!");
}
  • if-else (double alternatives):
    • if the condition is evaluated to true, then statementTrue is executed and statementfalse is skipped, otherwise it acts in reverse
if (<conditional expression>)
    <statementTrue>
else
    <statementFalse>
 - Example:
if(age > 18)
    printf("You are an adult!");
else
    printf("You are a minor!");
  • if - else if - else (Multiple Alternatives) are evaluated in sequence until a true result is reached.
 if (<conditional expression>)
        <statement>
    else if (<conditional expression>)
        <statement>
    else
        <statement>
 - `Nested-if` is the placing an `if` statement inside an `if` statement

Switch Statement

  • Switch statement compares a variable to a set of values (cases), which should be an int or char and made of case - constant
switch (<controlling expression>){
          case constant: <statement>
               break;
          case constant: <statement>
               break;
          default:
               <statement>
}
  • Must use break to skip matching statements, otherwise the rest will be skipped

Studying That Suits You

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

Quiz Team

Related Documents

More Like This

Basic Programming Concepts Quiz
122 questions
Programming Errors and Screen Objects
58 questions
Runtime Errors vs Syntax Errors
8 questions
Use Quizgecko on...
Browser
Browser