Podcast
Questions and Answers
Which of the following best describes computer programming?
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?
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?
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?
Which type of programming error is NOT detected by the compiler?
What is the key characteristic of a run-time error?
What is the key characteristic of a run-time error?
In C programming, what is the role of the C Preprocessor?
In C programming, what is the role of the C Preprocessor?
What is the purpose of the #include
directive in C programming?
What is the purpose of the #include
directive in C programming?
Which of the following statements about the main
function in C is correct?
Which of the following statements about the main
function in C is correct?
In C programming, what is the purpose of the curly braces {}
?
In C programming, what is the purpose of the curly braces {}
?
What is the role of 'declaration' within a C function?
What is the role of 'declaration' within a C function?
What does 'variable declaration' achieve in C programming?
What does 'variable declaration' achieve in C programming?
Why is it important for a programmer to consider how to allocate memory for data?
Why is it important for a programmer to consider how to allocate memory for data?
Which of the following is NOT a standard pre-defined data type in C?
Which of the following is NOT a standard pre-defined data type in C?
What is the primary function of 'modifiers' in C data types?
What is the primary function of 'modifiers' in C data types?
Which of the following describes what a 'variable' is in C programming?
Which of the following describes what a 'variable' is in C programming?
Which rule must an identifier in C programming adhere to?
Which rule must an identifier in C programming adhere to?
Which of the following statements related to Variable Naming in C is correct?
Which of the following statements related to Variable Naming in C is correct?
What is the function of the equal sign (=) in C programming?
What is the function of the equal sign (=) in C programming?
What is the function of the address operator (&) in C programming?
What is the function of the address operator (&) in C programming?
In the context of the scanf
function, what is the purpose of the format specifier?
In the context of the scanf
function, what is the purpose of the format specifier?
Which format specifier is used in scanf
to accept a float
?
Which format specifier is used in scanf
to accept a float
?
If you want to read two integer values using scanf
, how should you structure the function call?
If you want to read two integer values using scanf
, how should you structure the function call?
What will printf(“%d”, 5);
display?
What will printf(“%d”, 5);
display?
What is meant by 'control structure' in programming?
What is meant by 'control structure' in programming?
Which type of control structure executes statements in a sequential manner?
Which type of control structure executes statements in a sequential manner?
When defining a programming problem, which of the following steps comes directly before identifying the process?
When defining a programming problem, which of the following steps comes directly before identifying the process?
In C programming, what is the purpose of the #define
directive?
In C programming, what is the purpose of the #define
directive?
In C, should identifiers that must be changed by the preprocessor be written in capital or lowercase letters?
In C, should identifiers that must be changed by the preprocessor be written in capital or lowercase letters?
In C programming, what are 'libraries'?
In C programming, what are 'libraries'?
How is a predefined library opened in C?
How is a predefined library opened in C?
What is the primary purpose of comments in C code?
What is the primary purpose of comments in C code?
Which of the following statements about comments in C is correct?
Which of the following statements about comments in C is correct?
In C, what is a 'reserved word'?
In C, what is a 'reserved word'?
In C programming, what is the purpose of punctuation marks?
In C programming, what is the purpose of punctuation marks?
Which punctuation mark is used as a statement separator?
Which punctuation mark is used as a statement separator?
In C programming, what is the key attribute of 'constants'?
In C programming, what is the key attribute of 'constants'?
How is a constant or a read-only variable created in C?
How is a constant or a read-only variable created in C?
What is the purpose of 'backslash character constants' in C?
What is the purpose of 'backslash character constants' in C?
What effect does the unary minus operator have on a variable?
What effect does the unary minus operator have on a variable?
What is the difference between prefix and postfix increment operators?
What is the difference between prefix and postfix increment operators?
In which header file are the predefined mathematical functions in C located?
In which header file are the predefined mathematical functions in C located?
What data type do the functions from the math.h
library accept as an argument?
What data type do the functions from the math.h
library accept as an argument?
Flashcards
Computer Programming
Computer Programming
Creating a sequence of instructions to enable the computer to do something.
Programming Language
Programming Language
An artificial language to control the behavior of a machine, particularly a computer.
Problem Statement
Problem Statement
A clear, written statement of the problem to be solved by the computer.
Algorithm Development
Algorithm Development
Signup and view all the flashcards
Program Coding
Program Coding
Signup and view all the flashcards
Program Testing
Program Testing
Signup and view all the flashcards
Program Documentation
Program Documentation
Signup and view all the flashcards
Syntax Error
Syntax Error
Signup and view all the flashcards
Run-time Error
Run-time Error
Signup and view all the flashcards
Logic Error
Logic Error
Signup and view all the flashcards
#include directive
#include directive
Signup and view all the flashcards
Main Function
Main Function
Signup and view all the flashcards
Declaration
Declaration
Signup and view all the flashcards
Executable Statements
Executable Statements
Signup and view all the flashcards
Variable Declaration
Variable Declaration
Signup and view all the flashcards
Data Type
Data Type
Signup and view all the flashcards
Variable
Variable
Signup and view all the flashcards
Identifier
Identifier
Signup and view all the flashcards
Assignment Operator (=)
Assignment Operator (=)
Signup and view all the flashcards
Binary Operators
Binary Operators
Signup and view all the flashcards
Address Operator (&)
Address Operator (&)
Signup and view all the flashcards
scanf()
scanf()
Signup and view all the flashcards
Format Specifier (scanf)
Format Specifier (scanf)
Signup and view all the flashcards
printf()
printf()
Signup and view all the flashcards
Format Specifier (printf)
Format Specifier (printf)
Signup and view all the flashcards
Control Structure
Control Structure
Signup and view all the flashcards
Sequential Control Structure
Sequential Control Structure
Signup and view all the flashcards
#define Directive
#define Directive
Signup and view all the flashcards
Libraries
Libraries
Signup and view all the flashcards
Comments
Comments
Signup and view all the flashcards
Reserved Word
Reserved Word
Signup and view all the flashcards
Constants
Constants
Signup and view all the flashcards
Unary Operators (+/-)
Unary Operators (+/-)
Signup and view all the flashcards
Increment/Decrement Operators
Increment/Decrement Operators
Signup and view all the flashcards
Math functions
Math functions
Signup and view all the flashcards
getchar()
getchar()
Signup and view all the flashcards
getch()
getch()
Signup and view all the flashcards
getche()
getche()
Signup and view all the flashcards
gets()
gets()
Signup and view all the flashcards
puts()
puts()
Signup and view all the flashcards
putchar()
putchar()
Signup and view all the flashcards
Relational Operator
Relational Operator
Signup and view all the flashcards
Logical Operator
Logical Operator
Signup and view all the flashcards
If Statement
If Statement
Signup and view all the flashcards
Switch statement
Switch statement
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 likescanf
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;
- Example:
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
, orunsigned
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
- Text (
Defining Data Types
int
: integer numbers- Example:
Int Count; Count = 5;
- Example:
float
: floating point numbers- Example:
float Miles; Miles = 5.6;
- Example:
double
: BIG floating point numbers- Example:
double Atoms, Atoms = 2500000;
- Example:
char
: characters- Example:
char Letter; Letter = 'x';
- Example:
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.)
- Example:
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
- Angle brackets
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
- Syntax:
- 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
- Character constants are put in single quotes
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)
- Prefix increment is when the
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), andfloor()
(rounds down),sqrt()
(square root),pow()
(power), andfmod()
(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();
- Syntax:
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 stringgets(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”);
- Syntax:
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
- Syntax:
Conditional Control Structure
- Conditional control structure evaluates condition with a true or false result, where
false
is 0 andtrue
is any other non-zero value.- Utilizes the
if
statement with optionalelse
orelse-if
switch-case
statement
- Utilizes the
- 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)
- Includes
- Logical operators: logical “and”, “or” and “not”
&&
(and),||
(or), and!
(not)
- Relational operators: evaluate to true or false
- Order of Precedence:
- Arithmetic
*, /, %
+, -
- Relational
<, <=, >, >=, ==, !=
- Logical
!
&&
||
- Arithmetic
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 andstatementfalse
is skipped, otherwise it acts in reverse
- if the condition is evaluated to true, then
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 atrue
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 anint
orchar
and made ofcase - 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.