C Basics, C 101 – Study Notes
offline

Computer Science I (University of Central Florida)

Tags: C programming, stdio.h, switch statement, function declaration, function definition, loops, arrays, variable names, ASCII, primes, vowels

Difficulty: Beginner | Prerequisites: None


Big Picture

This material covers the foundational building blocks of programming in C: standard I/O, control flow, functions, arrays, and basic problem-solving patterns. These are the concepts everything else in the course builds on, from pointers to dynamic memory. If you are coming in cold, start here. You should be comfortable with the idea of writing, compiling, and running a simple C program before moving on to pointers or structs.


TL;DR

C programs use #include to access input/output functions like printf and scanf. Functions can be declared (prototype) or defined (full body). Arrays hold same-type elements accessed by index. Control flow tools like for, while, switch, and if/else let you repeat or branch logic. Knowing which operators work on which types, and how variable naming rules work, will save you on the exam.


Key Terms

stdio.h

The standard I/O library header in C. It provides functions like printf (output) and scanf (input). You include it with #include at the top of your program. In simple terms, this is the header that lets your programme talk to the user.

Switch statement

A control structure that selects one of many code blocks to execute based on the value of a variable. Useful when you have more than two or three discrete cases. Think of it as a cleaner replacement for a long chain of if-elseif-else blocks.

Function declaration (prototype)

A statement that tells the compiler a function's name, return type, and parameter types, without providing the body. Example: int add(int, int); In simple terms, it is a promise to the compiler that this function exists and will be defined later.

Function definition

The full implementation of a function, including the body that runs when the function is called. Think of it as the actual recipe, where the declaration was just the title on the recipe card.

ctype.h

A header providing character classification and conversion functions such as tolower(), toupper(), islower(), and isupper(). In simple terms, this is the toolbox for checking and changing individual characters.

ASCII value

The numerical code assigned to each character in the ASCII standard. For example, 'A' is 65, 'a' is 97. You can print a character's ASCII value using the %d format specifier. Think of it as each character's secret numerical identity.


Core Content

Including Headers and Standard I/O

  • #include gives access to printf, scanf, and related functions

  • Without it, the compiler will not recognise those function names

  • Other common headers: for character functions, for maths functions

Switch Statements vs. If-Else Chains

  • Use a switch when you are matching a single variable against multiple discrete values (more than two or three)

  • Each case label handles one value; break prevents fall-through to the next case

  • A switch is not suitable for range checks or complex boolean conditions, only for equality comparisons on integers or characters

Function Declarations vs. Definitions

  • A declaration (prototype) goes above main() or in a header file:

    • int add(int, int);

  • A definition provides the body:

    • int add(int a, int b) { return a + b; }

  • The compiler needs at least a declaration before a function is called, so it knows the return type and parameter types

Loops and Counting Patterns

  • Counting even numbers from 1 to 200: start a loop at 2, increment by 2, count and sum inside the loop

  • Alternative: use the formula (200 - 2) / 2 + 1 for the count, and n * (n + 1) style formulas for the sum

  • Counting vowels in a string: iterate character by character, convert to lowercase with tolower(), then check against 'a', 'e', 'i', 'o', 'u'

Character Manipulation

  • islower(ch) and isupper(ch) test case; tolower(ch) and toupper(ch) convert

  • Printing a character with %c gives the character itself; printing with %d gives its ASCII integer value

  • A while(1) loop with a sentinel value (like '*') is a common pattern for repeated user input until a stop signal

Prime Number Testing

  • A number less than 2 is not prime

  • For each candidate, test divisibility by all integers from 2 up to the square root of the candidate (j * j <= i)

  • If any divisor is found, mark it as not prime and break out of the inner loop

Array Pair-Sum Problem

  • Given an array and a target sum (here, the array's own length), use a nested loop: outer index i from 0 to length - 2, inner index j from i + 1 to length - 1

  • Check if arr[i] + arr[j] == target and print matching pairs

  • This brute-force approach runs in O(n^2) time


Formulas / Diagrams

Count of even numbers from 1 to N:

count = (N - 2) / 2 + 1 (when N is even)

Sum of even numbers from 2 to N:

sum = (N / 2) * (N / 2 + 1) (i.e. sum of first N/2 positive integers, each multiplied by 2)

Prime check optimisation:

Only test divisors up to sqrt(i), i.e. loop while j * j <= i.


Real-World Applications

Character manipulation and ASCII values underpin everything from text editors to encryption schemes. Prime-number testing is the foundation of modern cryptography (RSA encryption relies on the difficulty of factoring large primes). Even the simple pair-sum problem mirrors real interview questions at software companies.


Common Misconceptions

  • Students often think stdio.h stores the source code of their programme. It does not. It is a header file that provides function prototypes for standard I/O.

  • Students frequently confuse function declarations with definitions. A declaration has no curly braces or body; a definition does.

  • The % (modulus) operator cannot be used with floating-point numbers. Students sometimes try 5.0 % 2.0, which will not compile.

  • An array declared as int arr[50] holds exactly 50 elements (indices 0 to 49), not 49 or 51.


Why It Matters / Exam Flags

⚠️ Know the difference between function declaration and function definition, including syntax. This comes up in both multiple-choice and short-answer form.

⚠️ void is the return type that cannot return a value. Expect a multiple-choice question on this.

⚠️ Variable naming rules: names can contain letters, digits, and underscores. They cannot start with a digit, contain operators like + or ., or use $ (non-standard). Float is valid because C keywords are lowercase (float), so capitalised versions are legal identifiers.

⚠️ arr[5][5] stores 5 × 5 = 25 elements. 2D array size = rows × columns.

⚠️ Array name is equivalent to the address of its first element, not its last.

⚠️ Arrays store elements in consecutive memory locations.

⚠️ The closing brace of main() is the logical end of the programme.

⚠️ Not all arithmetic operators share the same precedence (multiplication and division bind tighter than addition and subtraction).


Quick Self-Test

  1. True or false: stdio.h is used to store the source code of your programme.

  1. True or false: An array name can be used as a pointer.

  1. Fill in the blank: The ___ operator finds the remainder of an integer division.

  1. True or false: All arithmetic operators have the same precedence.

  1. How many elements can int array[50] hold?

Answers: 1. False 2. True 3. % 4. False 5. 50


Practice Q&A

Q: Why do we include in C programmes?

A: It provides the prototypes for standard input/output functions like printf and scanf. Without it, the compiler does not know those functions exist.

Q: When should you use a switch statement instead of if-else?

A: When you are testing a single variable against more than two or three discrete constant values. A switch makes the code cleaner and easier to read than a long if-elseif chain.

Q: What is the difference between a function declaration and a function definition?

A: A declaration states the function's name, return type, and parameters without a body (e.g. int add(int, int);). A definition includes the body with the actual code that executes.

Q: Which of the following is a valid C variable name: Initial.Name, A+B, $amt, Float?

A: Float. C variable names cannot contain . or +, and $ is non-standard. Float is valid because it is not the keyword float (C is case-sensitive).

Q: What does printf("%d", scanf("%d", &num)); do, and is it valid?

A: Yes, it is valid. scanf returns the number of items successfully read (here, 1 if the user enters a valid integer), and printf prints that return value.


Connections to Other Topics

This material connects directly to pointers and pass-by-reference, since understanding how arrays decay to pointers and how function parameters work is the bridge. Loop patterns here (nested loops, sentinel-controlled loops) reappear in sorting algorithms and dynamic memory management later in the course.


Related Terms / Search Tags

C programming basics, stdio.h, scanf, printf, switch case, function prototype, function declaration vs definition, for loop, while loop, prime number algorithm, vowel counter C, ASCII value, ctype.h, tolower, toupper, array indexing, 2D array size, valid variable names C, modulus operator, C 101 UCF, Computer Science I