Introduction to C Programming

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

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?

  • 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?

  • 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?

<p>It creates a new line. (C)</p>
Signup and view all the answers

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

<p>The value of a variable can be changed during program execution. (D)</p>
Signup and view all the answers

Which of the following is NOT a valid variable name in C?

<p><code>float</code> (D)</p>
Signup and view all the answers

Which printf() placeholder is used to print an integer value?

<p><code>%d</code> (A)</p>
Signup and view all the answers

What happens if you use the wrong placeholder in printf(), such as using %d for a floating-point number?

<p>The program will output a garbage value or unexpected result. (C)</p>
Signup and view all the answers

What is the purpose of the scanf() function in C?

<p>To read input from the keyboard. (B)</p>
Signup and view all the answers

With scanf(), what symbol is necessary before a variable's name?

<p><code>&amp;</code> (D)</p>
Signup and view all the answers

Which data type is used to store a single character?

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

Which of the following data types can store decimal values?

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

What does the void data type signify?

<p>It means the absence of a data type. (B)</p>
Signup and view all the answers

In C, which operator is used to find the remainder of a division?

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

What is the result of the expression 5 / 2 in C?

<p><code>2</code> (A)</p>
Signup and view all the answers

Which operator is used to check if two values are equal?

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

What will the following code output?

int a = 5;
int b = 3;
printf("%d", a != b);

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

What is the result of the logical AND operation (&&) if one operand is true and the other is false?

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

Which of the following correctly expresses the condition 'divisible by 4 but not 100' in C code?

<p><code>y % 4 == 0 &amp;&amp; y % 100 != 0</code> (B)</p>
Signup and view all the answers

In C, what is the function of the if statement?

<p>To execute a block of code only if a condition is true. (D)</p>
Signup and view all the answers

What is the purpose of the else statement in conjunction with an if statement?

<p>To execute a block of code when the <code>if</code> condition is false. (D)</p>
Signup and view all the answers

What is the main difference between a while loop and a for loop?

<p>A <code>for</code> loop is used when the number of iterations is known, while a <code>while</code> loop is used when it is unknown. (A)</p>
Signup and view all the answers

What is the purpose of the declaration section in a for loop?

<p>To initialize a variable used in the loop. (A)</p>
Signup and view all the answers

What happens if the condition in a while loop is always true?

<p>The loop will execute indefinitely, creating an infinite loop. (D)</p>
Signup and view all the answers

When should you use a while loop instead of a for loop?

<p>When the number of iterations is unknown. (B)</p>
Signup and view all the answers

What is the purpose of a function in C?

<p>To break down a program into smaller, manageable parts. (B)</p>
Signup and view all the answers

Which part of a function declaration specifies the type of value that the function will return?

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

What is a parameter in a function?

<p>A value passed to the function when it is called. (D)</p>
Signup and view all the answers

Where should the definition of a function be placed?

<p>Outside the <code>main()</code> function. (B)</p>
Signup and view all the answers

What characteristics define a 'mathematical function'?

<p>They return a result. (D)</p>
Signup and view all the answers

What data type must procedures (non-returning functions) specify?

<p><code>void</code> (D)</p>
Signup and view all the answers

What is a recursive function?

<p>A function that calls itself. (A)</p>
Signup and view all the answers

What is an array?

<p>A collection of elements of the same data type. (C)</p>
Signup and view all the answers

If you declare an array as int arr[5], what is the valid range of indices you can use to access elements?

<p>0 to 4 (C)</p>
Signup and view all the answers

How do you access the first element of the array arr?

<p><code>arr[0]</code> (C)</p>
Signup and view all the answers

What header file is required to use string functions?

<p><code>string.h</code> (A)</p>
Signup and view all the answers

If you want to find the length of a string, should you use strlen(), strcat(), strcpy() or strcmp()?

<p><code>strlen()</code> (A)</p>
Signup and view all the answers

Which function is used to copy one string to another?

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

What is a pointer?

<p>A variable that stores the address of another variable. (B)</p>
Signup and view all the answers

What does the & operator do?

<p>Returns the memory address of a variable. (A)</p>
Signup and view all the answers

Flashcards

Variables

Variables are data stored in computer's memory whose value can be changed.

Type

Defines the data type of a variable (integer, float, character).

Name

A name is how we call a variable, following certain rules (e.g., no spaces)

%d

A placeholder to fill in the value of variables when using printf().

Signup and view all the flashcards

%f

A placeholder to fill in the value of floating point numbers when using printf().

Signup and view all the flashcards

%c

A placeholder to fill in the character values when using printf().

Signup and view all the flashcards

%s

A placeholder to fill in the string values when using printf().

Signup and view all the flashcards

scanf()

Used to input a number/character from the keyboard.

Signup and view all the flashcards

Operators

Math functions (plus, minus, multiply, divide) acting on operands.

Signup and view all the flashcards

Modulo Operator (%)

Yields the remainder after division.

Signup and view all the flashcards

Compare Operations

Returns 1 if true, 0 if false. Compares two values or expressions.

Signup and view all the flashcards

Logical Operations

Returns 1 if true or 0 if false, combines or modifies conditions.

Signup and view all the flashcards

Assignment Operators

Reduce redundancy and repetition in your code.

Signup and view all the flashcards

Increment/Decrement Operators

Increments or decrements a variable by 1.

Signup and view all the flashcards

Preincrement

Increase the variable first, then use it.

Signup and view all the flashcards

Postincrement

Use the variable's current value, then increment it.

Signup and view all the flashcards

Conditional Statement

Executes code based on whether a condition is true or false.

Signup and view all the flashcards

Loop

Repeats a block of code until a condition is met.

Signup and view all the flashcards

For Loop

Repeats code a known number of times.

Signup and view all the flashcards

While Loop

Repeats code as long as a condition is true.

Signup and view all the flashcards

Functions

Breaks big tasks into smaller, reusable parts.

Signup and view all the flashcards

Data type

Specifies what type of data the function will return (int, float, etc.).

Signup and view all the flashcards

Func_name

The name you give the function.

Signup and view all the flashcards

Parameters

Values passed into a function for it to use.

Signup and view all the flashcards

Mathematical Functions

Functions that return a result.

Signup and view all the flashcards

Procedures

Functions that only perform tasks (no return value).

Signup and view all the flashcards

Recursive Functions

Functions that call themselves.

Signup and view all the flashcards

Array

Collection of elements of the same type.

Signup and view all the flashcards

array[i]

Access an element in an array.

Signup and view all the flashcards

String

Sequence of letters (collection of characters).

Signup and view all the flashcards

strlen()

Returns the length of the string.

Signup and view all the flashcards

strcpy()

Copies a string to another string.

Signup and view all the flashcards

strcat()

Joins two strings together.

Signup and view all the flashcards

Pointer

Variable containing the address of another variable.

Signup and view all the flashcards

&variable

Include pointer parameter.

Signup and view all the flashcards

*pointer

Get value at address the pointer points to

Signup and view all the flashcards

When declaring a pointer as an array,

Points to the first element of the array.

Signup and view all the flashcards

Dynamic Memory Allocation

Allocation of sized data in memory while the program is running.

Signup and view all the flashcards

Structure

Creating a new made-up data

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 execution
  • main() is a required function and MUST be int main(); programs without this function will not run
  • return 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 or 0 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 or 0 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 to a = a + b;
  • a -= b; is equivalent to a = a - b;
  • a *= b; is equivalent to a = a * b;
  • a /= b; is equivalent to a = a / b;
  • a %= b; is equivalent to a = a % b;

Increments / Decrements

  • For increments/decrements of 1, there are special cases
    • a++; is equivalent to a = a + 1; or a += 1;
    • a--; is equivalent to a = a - 1; or a -= 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), the dothis; block executes
  • When condition == 0 (false), the dothat; 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 loop
    • declaration declares a variable for calculations
    • condition runs if true. Once the condition is false, the loop exits
    • increment/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
  • 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 structure
    • data_type is the type the function returns
    • func_name is the name of the function
    • parameters 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.

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser