Podcast
Questions and Answers
Which of the following is a correct way to declare the main
function in C?
Which of the following is a correct way to declare the main
function in C?
- `float main()`
- `char main()`
- `int main()` (correct)
- Any of the above
In C, what is the purpose of the stdio.h
library?
In C, what is the purpose of the stdio.h
library?
- It is required for all mathematical operations.
- It's a user-defined library that contains code for specific project.
- It is used to manage memory allocation.
- It provides basic input/output functions. (correct)
What happens if you forget to put a semicolon (;
) at the end of a command in C?
What happens if you forget to put a semicolon (;
) at the end of a command in C?
- The program may exhibit unexpected behavior.
- The compiler will ignore the line.
- The compiler will throw an error. (correct)
- The program will still compile and run normally.
What is the purpose of the \n
character in a printf()
statement?
What is the purpose of the \n
character in a printf()
statement?
Which of the following statements about variables in C is correct?
Which of the following statements about variables in C is correct?
Which of the following is NOT a valid variable name in C?
Which of the following is NOT a valid variable name in C?
Which printf()
placeholder is used to print an integer value?
Which printf()
placeholder is used to print an integer value?
What happens if you use the wrong placeholder in printf()
, such as using %d
for a floating-point number?
What happens if you use the wrong placeholder in printf()
, such as using %d
for a floating-point number?
What is the purpose of the scanf()
function in C?
What is the purpose of the scanf()
function in C?
With scanf()
, what symbol is necessary before a variable's name?
With scanf()
, what symbol is necessary before a variable's name?
Which data type is used to store a single character?
Which data type is used to store a single character?
Which of the following data types can store decimal values?
Which of the following data types can store decimal values?
What does the void
data type signify?
What does the void
data type signify?
In C, which operator is used to find the remainder of a division?
In C, which operator is used to find the remainder of a division?
What is the result of the expression 5 / 2
in C?
What is the result of the expression 5 / 2
in C?
Which operator is used to check if two values are equal?
Which operator is used to check if two values are equal?
What will the following code output?
int a = 5;
int b = 3;
printf("%d", a != b);
What will the following code output?
int a = 5;
int b = 3;
printf("%d", a != b);
What is the result of the logical AND operation (&&
) if one operand is true and the other is false?
What is the result of the logical AND operation (&&
) if one operand is true and the other is false?
Which of the following correctly expresses the condition 'divisible by 4 but not 100' in C code?
Which of the following correctly expresses the condition 'divisible by 4 but not 100' in C code?
In C, what is the function of the if
statement?
In C, what is the function of the if
statement?
What is the purpose of the else
statement in conjunction with an if
statement?
What is the purpose of the else
statement in conjunction with an if
statement?
What is the main difference between a while
loop and a for
loop?
What is the main difference between a while
loop and a for
loop?
What is the purpose of the declaration section in a for
loop?
What is the purpose of the declaration section in a for
loop?
What happens if the condition in a while
loop is always true?
What happens if the condition in a while
loop is always true?
When should you use a while
loop instead of a for
loop?
When should you use a while
loop instead of a for
loop?
What is the purpose of a function in C?
What is the purpose of a function in C?
Which part of a function declaration specifies the type of value that the function will return?
Which part of a function declaration specifies the type of value that the function will return?
What is a parameter in a function?
What is a parameter in a function?
Where should the definition of a function be placed?
Where should the definition of a function be placed?
What characteristics define a 'mathematical function'?
What characteristics define a 'mathematical function'?
What data type must procedures (non-returning functions) specify?
What data type must procedures (non-returning functions) specify?
What is a recursive function?
What is a recursive function?
What is an array?
What is an array?
If you declare an array as int arr[5]
, what is the valid range of indices you can use to access elements?
If you declare an array as int arr[5]
, what is the valid range of indices you can use to access elements?
How do you access the first element of the array arr
?
How do you access the first element of the array arr
?
What header file is required to use string functions?
What header file is required to use string functions?
If you want to find the length of a string, should you use strlen()
, strcat()
, strcpy()
or strcmp()
?
If you want to find the length of a string, should you use strlen()
, strcat()
, strcpy()
or strcmp()
?
Which function is used to copy one string to another?
Which function is used to copy one string to another?
What is a pointer?
What is a pointer?
What does the &
operator do?
What does the &
operator do?
Flashcards
Variables
Variables
Variables are data stored in computer's memory whose value can be changed.
Type
Type
Defines the data type of a variable (integer, float, character).
Name
Name
A name is how we call a variable, following certain rules (e.g., no spaces)
%d
%d
Signup and view all the flashcards
%f
%f
Signup and view all the flashcards
%c
%c
Signup and view all the flashcards
%s
%s
Signup and view all the flashcards
scanf()
scanf()
Signup and view all the flashcards
Operators
Operators
Signup and view all the flashcards
Modulo Operator (%)
Modulo Operator (%)
Signup and view all the flashcards
Compare Operations
Compare Operations
Signup and view all the flashcards
Logical Operations
Logical Operations
Signup and view all the flashcards
Assignment Operators
Assignment Operators
Signup and view all the flashcards
Increment/Decrement Operators
Increment/Decrement Operators
Signup and view all the flashcards
Preincrement
Preincrement
Signup and view all the flashcards
Postincrement
Postincrement
Signup and view all the flashcards
Conditional Statement
Conditional Statement
Signup and view all the flashcards
Loop
Loop
Signup and view all the flashcards
For Loop
For Loop
Signup and view all the flashcards
While Loop
While Loop
Signup and view all the flashcards
Functions
Functions
Signup and view all the flashcards
Data type
Data type
Signup and view all the flashcards
Func_name
Func_name
Signup and view all the flashcards
Parameters
Parameters
Signup and view all the flashcards
Mathematical Functions
Mathematical Functions
Signup and view all the flashcards
Procedures
Procedures
Signup and view all the flashcards
Recursive Functions
Recursive Functions
Signup and view all the flashcards
Array
Array
Signup and view all the flashcards
array[i]
array[i]
Signup and view all the flashcards
String
String
Signup and view all the flashcards
strlen()
strlen()
Signup and view all the flashcards
strcpy()
strcpy()
Signup and view all the flashcards
strcat()
strcat()
Signup and view all the flashcards
Pointer
Pointer
Signup and view all the flashcards
&variable
&variable
Signup and view all the flashcards
*pointer
*pointer
Signup and view all the flashcards
When declaring a pointer as an array,
When declaring a pointer as an array,
Signup and view all the flashcards
Dynamic Memory Allocation
Dynamic Memory Allocation
Signup and view all the flashcards
Structure
Structure
Signup and view all the flashcards
Study Notes
- These are study notes on the basics of C programming
Introduction to a C Program
#include <stdio.h>
calls the input/output default library, containing basic utilities for program executionmain()
is a required function and MUST beint main()
; programs without this function will not runreturn 0;
in main() is optional but recommended- Text or numbers can be printed in the terminal using the
printf()
function printf("text you want to print");
is the syntax for this function- A semicolon (
;
) must be at the end of commands - Text must be within double quotes (
"
)
Variables
- Variables are data stored in the computer's memory
- The value of a variable can be changed
- Constants cannot have their values changed
- Variables are needed to perform calculations and counting
Declaring Variables
- Variables need a type and a name to be declared
type
defines the data type (integer, float, character)name
is how the variable is called, following certain rules- Optionally, a variable can be given a value upon declaration
Common Mistakes Naming Variables
- Using a reserved keyword as the variable name
- Starting a variable name with a number
- Using special characters like
$
,@
,+
,-
,*
,/
,#
,%
,&
Printing Variables
- The
printf()
function is used to print variables to the terminal printf("%placeholder", variable_name);
is the basic syntax- Placeholders fill in the value of variables
- Use different placeholders depending on the variable type
%d
for integers%f
for floats%c
for characters%s
for strings
- Placeholders must be in between double quotes (
"
) - Multiple variables can be declared at once and will be filled in order into the respective placeholders
Entering Data from Keyboard
- The
scanf()
function is used to input a number or character from the keyboard scanf("%placeholder", &variable_name);
is the syntax- The variable must be declared before inputting it
- The placeholder should match the data type
- The “&” represents the address of the variable in computer memory
Data Types
- Each data type has its meaning and capacity
- Common data types include
int
for integers- Takes up 2 or 4 bytes (16 or 32 bits)
float
for real numbers- Takes up 4 bytes (32 bits)
char
for characters- Takes up 1 byte (8 bits)
long long int
- Takes up 8 bytes (64 bits)
double
- Takes up 8 bytes (64 bits)
void
, refers to an empty data type and cannot be declared
Operators
- Operators are math functions (+, -, *, /) that act on operands
- Operations are similar to standard math
Basic Operations
+
is addition-
is subtraction*
is multiplication/
is division%
finds the remainder after division (modulo)- The modulo operation can determine if a number is odd or even
Compare Operations
- These operations return
1
if true or0
if false ==
tests for equality>
is greater than<
is less than!=
tests for inequality>=
is greater than or equal to<=
is less than or equal to==
compares, while=
assigns
Logical Operations
- These operations return
1
if true or0
if false &&
is the logical AND operator, requiring both operands to be true||
is the logical OR operator, requiring either operand to be true!
is the logical NOT operator, reversing the logical state
Assignments Operations
- These operations reduce redundancy and repetition
a += b;
is equivalent toa = a + b;
a -= b;
is equivalent toa = a - b;
a *= b;
is equivalent toa = a * b;
a /= b;
is equivalent toa = a / b;
a %= b;
is equivalent toa = a % b;
Increments / Decrements
- For increments/decrements of 1, there are special cases
a++;
is equivalent toa = a + 1;
ora += 1;
a--;
is equivalent toa = a - 1;
ora -= 1;
Types of Increment
- There are two types
- Preincrement: increase first, execute after
- Postincrement: execute first, increase after
Conditional (If / Else)
- Conditional statements (if / else / else if) execute code lines if a condition is met
- When
condition == 1
(true), thedothis;
block executes - When
condition == 0
(false), thedothat;
block executes - The condition must be inside parentheses
()
Loops
- Loops repeat a block of code until a condition is met
For Loop
- Counting loops are used when the number of loops is limited and known
for (declaration; condition; increment) { things_to_do; }
is the syntax to define a for loopdeclaration
declares a variable for calculationscondition
runs if true. Once the condition is false, the loop exitsincrement/decrement
is the action after each loop, affecting when the loop stops executing
While Loop
- Conditional loops are used when it is unknown when the loop will stop
while (condition) { things_to_do; }
is the syntax for a while loop- The
condition
is checked at the beginning of each loop - The code runs while true. Once the condition is false, the loop exits
- The
- Code in the while loop should lead to stopping the loop
Which Loop to Use
- If the exact amount of loops is known, use a
for
loop - If the stopping point is unknown, use a
while
loop
Functions
- Functions break down large programs into smaller parts or modularize sections of code
data_type func_name(parameters) { things_to_do; return; }
is the structuredata_type
is the type the function returnsfunc_name
is the name of the functionparameters
is what we send into the function
- Functions should be defined outside the
main()
function
Types of Functions
- Mathematical functions will return a result
- Non-returning functions only do specific tasks and must have a data type
void
- Recursive functions will call themselves
Array
- An array is a collection of elements of the same type.
Array Declaration and Initialization
- There are three ways
int arr1[] = {1,2,3}; //Create an array with just big enough to hold the elements
int arr2[5]; //Create an array with 5 unknown elements
int arr3[6] = {1,2,3,4,5,6}; //Create an array with 6 known elements
Access Array Values
- To access variables A[i] where A is the array and I am the index for the value
- First entry has index 0
- Second entry has index 1
- Third entry has index 2
- etc.
Passing Arrays
- An array can be passed to a function by defining its parameters with square brackets
- To return array in a function, we use int* (which refers to Pointer):
String
- A string is a collection of letters/characters
- To declare string use
char mystring[]
String Access
- Strings can be accessed like arrays with variable A[i], where A is the string and I am the index for the letter/character
- The first character is 0, the second is 1, third is 2 etc
File I/O
- Using files will reduce needing to input things over and over upon each run
Reading files
- Use command
FILE *name = fopen("fileName.txt", "r");
- "r" means read
- filename is the name of what you are reading
- After, read command using command
fscanf (name, "%d", &var);
Writing files.
- Use command
FILE *name = fopen("fileName.txt", "w");
w means write - Next write command using
fprintf (name, "%s", str);
:
Strings
- Strings are a collection of letters
char mystring[]
is the command to call astring
Pointers
- a variable containing the address of another variable
- imagine that you have a map that shows direction to the One Piece treasure. The map is now a pointer to the variable treasure.
- The syntax for pointer is:
- int* pointer_name = &variable; or int *pointer_name = &variable;
Pointer Arithmetic
- When declaring a pointer as an array, it points to the first element of that array. (A [0])
- When you increase the pointer above by 1 using p++ (or by n unit using p[n]), it points to the next element: A [1]
- p [3] means p = p +3, or to be more precise, it means *(p+3) or "the value located 3 positions after the memory address pointed to by p".
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.