Programming with C Fundamentals

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/59

Last updated 9:55 PM on 3/10/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

60 Terms

1
New cards

What is the basic structure of a C program?

#include <stdio.h>

int main(void)
{
	//Declare variables
	//Input
	//Program Logic (loops, conditions)
	//output
	return 0;
}

2
New cards

What does #include <stdio.h> do?

It allows the program to use input/output functions like:

printf()
scanf()

3
New cards

How do you declare variables in C?

int x;
float y;
char c;

Variables store data in the program.

4
New cards

When should variables be declared?

At the top of a function, usually right after {.

5
New cards

What does printf() do?

Prints output to the screen.

printf("Hello");

6
New cards

What does scanf() do?

Reads input from the user.

scanf("%d", &x);

& means the address of the variable.

7
New cards

Common format specifiiers

%d ->	integer
%f ->	float
%c -> 	character

8
New cards

What does #define do?

Creates a constant value.

#define SIZE 200

Used often with arrays.

9
New cards

What arithmetic operators are used in C?

+ addition
- subtraction
* multiplication
/ division
% modulus (remainder)

10
New cards

When do you use % (modulus)?

To check divisibility.

if (num % 2 == 0)

Checks if a number is even.

11
New cards

When do you use an if statement?

When the program must make a decision.

if (x > 0)
{
	printf("Positive");
}

12
New cards

What does an if-else statement look like?

if (condition)
{
	//code if true
}
else
{
	//code if false
}

13
New cards

Common relational operators

== equal (comparison)
!= not equal
> greater than
< less than
>= greater or equal
<= less or equal

14
New cards

When do you use a for loop?

When you know how many times the loop should run.

Example:

for (i=0; i<n; i++)
{
	printf("%d", i);
}

Often used with arrays.

15
New cards

Structure of a for loop

for (initialization; condition; update)
{
	statements;
}

Example:

for (i=0; i<10; i++)

16
New cards

When do you use a while loop?

When a loop runs while a condition is true, and the number of repetitions is not known.

Example:

while (x>0)
{
	printf("%d", x);
}

17
New cards

Structure of a while loop

while(condition)
{
	statements;
}

18
New cards

When do you use a do-while loop?

When the loop must run at least once.

Example:

do
{
	printf("Hello");
}
while(x>0);

19
New cards

What does a break do?

Immediately exits a loop/

Example:

if (x == 5)
{
	break;
}

20
New cards

What is an array?

A collection of variables of the same type stored together.

Example:

int numbers [10];

21
New cards

What are valid array indexes?

If array size is:

int arr[10];

//Indexes are 0 to 9

22
New cards

How do you read values into an array?

for(i = 0; i<n; i++)
{
	scanf("%d", &arr[i]);
}

23
New cards

How do you print values from an array?

for (i=0; i<n; i++)
{
	printf("%d", arr[i]);
}

24
New cards

How do you search for a number in an array?

for (i=0; i<n; i++)
{
	if (arr[i] == key)
	{
		// found it
	}
}

25
New cards

What is a function?

A reusable block of code that performs a specific task.

Example:

int square(int x)
{
	return x * x;
}

26
New cards

What is a function prototype?

Declares the function before main.

Int square(int x);

27
New cards

How do you call a function?

result = square(5);

28
New cards

When do you use a switch statement?

When selecting one option from multiple choices.
Often used for menus.

29
New cards

Structure of a switch statement

switch(x)
{
	case 1:
		statements;
	case 2:
		statements;
		break;
	default:
		statements;
}

30
New cards

Why is break important in a switch?

without break, the program continues executing the next cases.

31
New cards

What are common causes of compile errors? (Hint: 6 common mistakes)

  • missing ;

  • missing }

  • wrong variable name

  • using variable before declaring it

  • forgetting & in scanf

  • mismatched function names

32
New cards

What should you do first when given a lab exam prompt?

  • Write main structure

  • Declare variables

  • Write input section

  • Write loops/logic

  • Write output section

  • Compile and fix errors

33
New cards

What is a flag variable?

A variable used to remember whether something happened during a loop.

int found = 0;

if(arr[i] == key)
{
	found = 1;
}

often used when searching for arrays

34
New cards

When should you use a flag variable?

Use a flag when you need to track:

  • if a value was found

  • if a condition occurred in a loop

  • if a number is prime

Example:

  • Searching Arrays

  • Prime Checking

  • Validating input

35
New cards

What does i++ and ++i mean?

i++ is a post-increment operator, which increments the value of ‘i’ but returns the original value that ‘i’ held before being incremented. Equivalent to i = i + 1;

++i is a pre-increment operator, which increments the value of ‘i’ immediately and returns the increments value.

36
New cards

What does i- - and - -i mean?

i- - is a post-increment operator, which decrements the value of ‘i’ but returns the original value that ‘i’ held before being decremented. Equivalent to i = i - 1;

- -i is a pre-increment operator, which decrements the value of ‘i’ immediately and returns the decrement value.

37
New cards

What does continue do?

Skips the rest of the current loop iteration and goes into the next iteration.

if(x < 0)
{
	continue;
}

38
New cards

Difference between break and continue?

Break exits the loop completely

Continue skips to the next iteration

39
New cards

What is a nested loop?

A loop inside of another loop.

for(i = 0; i < n; i++)
{
	for(j = 0; j < m; j++)
	{
		printf("%d", arr[i][j]);
	}
}

Often used for:

  • comparing arrays

  • pattern printing

  • searching combinations

40
New cards

When are nested loops commonly used?

  • comparing two arrays

  • checking all combinations

  • matrix-style problems

  • searching multiple lists

41
New cards

How do you sum elements of an array?

sum = 0;

for(i = 0; i < n; i++)
{
	sum = sum + arr[i];
}

42
New cards

How do you calculate average of an array?

average = sum / n;

After computing the sum.

43
New cards

How do you find the largest number in an array?

largest = arr[0];

for(i=1; i<n; i++)
{
	if(arr[i] > largest)
		largest = arr[i];
}

44
New cards

How do you find the smallest number in an array?

smallest = arr[0];

for(i=1; i<n; i++);
{
	if(arr[i] < smallest)
		smallest = arr[i];
}

45
New cards

How do you search for a number in an array?

for(i = 0; i < n; i++)
{
	if(arr[i] == key)
	{
		printf("Found");
	}
}

46
New cards

What is linear search?

Checking each element of the array one by one until the value is found.

47
New cards

How do you reverse an array?

Swap elements from the front and back.

Example logic:

temp = arr[i];
arr[i] = arr[n-1-i];
arr[n-1-i] = temp;

48
New cards

When are menu-driven programs used?

When the user chooses options from a list.

Example:

1. Add
2. Subtract
3. Exit

Usually uses switch + loop

49
New cards

What is the typical structure of a menu program

do
{
	print menu
	scanf choice

	switch(choice)
	{
		case 1:
		    ...
		    break;
		case 2:
		    ...
		    break;
	}
} while(choice != exit);

50
New cards

What type must a case label be?

A constant value.

Example:

case 1:
case 2:
case 3:

Expressions are not allowed

51
New cards

What causes infinite loops?

  • condition never becomes false

  • update statement missing

  • incorrect loop condition

Example mistake:

for(i = 0, i < 10;)

52
New cards

Basic logic for checking if a number is prime

  1. Assume the number is prime

  2. Try dividing from 2 to n-1

  3. if divisible → not prime

Example:

for(i = 2; i < n; i++);
{
	if(n % 1 == 0)
	    not prime
}

53
New cards

What does pass-by-value mean?

Functions receive a copy of the variable, not the original.

Changes inside the function do not affect the original value.

54
New cards

How do you pass an array to a function?

void process(int arr[], int size)

Call:

process(arr, n);

55
New cards

What does tracing code mean?

Following the program step-by-step to determine what it prints

Often used with:

  • loops

  • arrays

  • counters

56
New cards

How should you approach a coding lab problem?

  1. Read prompt carefully

  2. identify needed tools (loops, arrays, switch)

  3. Write program structure

  4. Add logic step by step

  5. Compile frequently

57
New cards

Most common pattern in array problems

read n
read array
loop through array
apply condition
print result

58
New cards

Standard loop for processing an array

for(i = 0; i < n; i++)
{
	// work with arr[i]
}

59
New cards

Structure of a function

returnType functionName(parameters)
{
	//statements
	return value;
}

Example:

int square(int x)
{
	return x * x;
}

60
New cards

Structure of an if-else statement

if(condition)
{
   statements;
}
else
{
   statements;
}

Explore top flashcards

flashcards
English 1111-Sadlier Unit IIII
20
Updated 1242d ago
0.0(0)
flashcards
APHUG Unit 6 & 7 Vocab
50
Updated 1066d ago
0.0(0)
flashcards
PSYC 14
64
Updated 188d ago
0.0(0)
flashcards
Kafli 9 - Choice and Preference
43
Updated 120d ago
0.0(0)
flashcards
Fifty common Russian verbs
51
Updated 435d ago
0.0(0)
flashcards
Unit 3 vocab
20
Updated 1206d ago
0.0(0)
flashcards
PA Drivers Permit Flashcards
166
Updated 1056d ago
0.0(0)
flashcards
Unit 1A
61
Updated 1160d ago
0.0(0)
flashcards
English 1111-Sadlier Unit IIII
20
Updated 1242d ago
0.0(0)
flashcards
APHUG Unit 6 & 7 Vocab
50
Updated 1066d ago
0.0(0)
flashcards
PSYC 14
64
Updated 188d ago
0.0(0)
flashcards
Kafli 9 - Choice and Preference
43
Updated 120d ago
0.0(0)
flashcards
Fifty common Russian verbs
51
Updated 435d ago
0.0(0)
flashcards
Unit 3 vocab
20
Updated 1206d ago
0.0(0)
flashcards
PA Drivers Permit Flashcards
166
Updated 1056d ago
0.0(0)
flashcards
Unit 1A
61
Updated 1160d ago
0.0(0)