IT 3210 - C programming language

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/52

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

53 Terms

1
New cards

Algorithm

Set of instructions specifying the steps required to accomplish a task

2
New cards

Components of an algorithm

  1. Variables and values

  2. Instructions:

  • Sequences

  • Selections

  • Iterations

  • Procedures

3
New cards

(computer) Program

A sequence of instructions aims at solving a specific task

→ Instruction is carried out one after the other, none carried out when the previous isn’t accomplished

4
New cards

Interpreted & compiled language

  • Interpreted: translated during execution

  • Compiled: translated before execution

5
New cards

Problem - solving process

Progran → Algorithm → Program

6
New cards

Values

Represent quantities/ amounts/ measurements

7
New cards

Variables (definition) 

Containers/ places to store values

→ Can be restricted to contain a specific type of value

8
New cards

Sequence structure

Series of instructions to be carried out one after the other

9
New cards

Selection

An instruction that decides which of two possible sequences is executed

→ Based on a condition (true/false)

  • if …

  • then …

  • else …

10
New cards

Iteration

Repeat an instruction while/ until some true or false condition occurs.

→ Two kinds of iteration: Test the condition each time before/ after repeating the instruction

11
New cards

Procedure

A series of instructions with a name

→ divide a program into smaller parts with different names

12
New cards

C Language Structure

• #include <stdio.h> → To declare using the standard I/O library

• int main() → To declare the main() function → only one

• { … } → The syntax to open and close a block of codes.

• return 0; → Stop the program

13
New cards

Syntax

  • Keywords: reserved words for specific meaning

  • User’s names: defined by user to specify a variable, a function

  • Specific characters: represent expressions in a program and make it have structure

14
New cards

Keywords

  • Flow control (6) – if, else, return, switch, case, default

  • Loops (5) – for, do, while, break, continue

  • Common types (5) – int, float, double, char, void

  • Structures (3) – struct, typedef, union

  • Counting and sizing things (2) – enum, sizeof

  • Rare but still useful types (7) – extern, signed, unsigned, long, short, static, const

  • Jump to label (1) – goto

  • Weirdies (3) – auto, register, volatile

15
New cards

Common used characters

  • {…} create a block of instructions

  • “…” create a string to display

  • /* … */ create a block of comment in the program

  • ; End of an instruction

  • other characters for formulas such as +, -, *, /, (),

16
New cards

Identifier

When declare a variable/ procedure

17
New cards

Variable

  • has a name (identifier)

  • has types

  • declared at the beginning of a function: <data type> <variable list>; → Assignment = puts a specified value into a specified variable

18
New cards

Basic data types

• char

• short

• int

• long

• float

• double

19
New cards

Strings

Groups of char form strings

→ must be enclosed by double quote “ ”

20
New cards

Logic

every number give also a boolean value

• 0 for false

• 1 for true

21
New cards

Const declaration

A variable declaration with the prefix const indicates that the value of that variable never changes in the program.

22
New cards

Data input/ ouput

  • printf()

  • scanf()

23
New cards

Conversion character of printf()

do not display in the screen → replaced by values

• %d: signed decimal integer

• %u: unsigned decimal integer

• %x: hexadecimal integer

• %o: octal integer

• %s: string

• %c: single character

• %f: fixed decimal floating point

• %e: scientific notation floating point

= To print a character % → use %%

24
New cards

Formatting with printf

specify the field width by following:

% [-] [fwidth] [.p] X

• [fwidth] the field width

• [-] left justified

• [.p] the number of decimal places/ how many characters are to be printed

25
New cards

Formatting with scanf

• %d : decimal integer (int)

• %ld : long decimal integer (long)

• %x : hexadecimal integer

• %o : octal integer

• %h : short integer (short)

• %f : float type

• %lf : long float or double

• %c : single character

• %s : character string

26
New cards

Return value of scanf()

n = scanf ("string...", pointers);

→ The value n returned is the number of items matched or the end of file character EOF, or NULL if the first item did not match

27
New cards

Expressions

  • Combine values using operators, function calls

  • Return a value of a known type 

28
New cards

Operator

Take values & do something to produce a result 

29
New cards

Operand

Operated upon by an operator

30
New cards

Arithmetic operators

  • + (unary plus)

  • - (unary minus)

  • + (addition)

  • - (subtraction)

  • * (multiplication)

  • / (division or quotient)

  • % (modulus or remainder)

31
New cards

Comparison operators

• < (less than)

• <= (less than or equal)

• > (greater than)

• >= (greater than or equal)

• == (equal)

• != (in-equal)

32
New cards

Logic

  • Have 2 values: true (1) ; false (0)

  • Create the selection of conditions/ the loop for an algorithm

33
New cards

Logic operators

• && (and)

• || (or)

• ! (not)

• comparison (==, !=, <, >, <=, >=)

34
New cards

Bit operators

  • & (and bit)

  • | (or bit)

  • ~ (negation)

  • >> (shift right)

  • << (shift left)

35
New cards

Short-circuiting

A complex Boolean expression is only evaluated as far as necessary

36
New cards

Extend assignment operators

  • +=

  • -=

  • *=

  • /=

  • %=

37
New cards

Increment, decrement operators

  • ++ is the increment operator

  • -- is the decrement operator

→ Two ways of writing: prefix (++i) and suffix (i++):

  • Prefix return value after adding 1

  • Suffix return value before adding 1

  • In both cases, value of i increases by 1

38
New cards

Conditional Expressions

<Condition> ? <Expression 1> : <Expression 2>

39
New cards

Casting data type

int a;

float f;

a = (int) f;

40
New cards

Precedences

• Unary operators (!, -)

• Multiply, divide (*, /, %)

• Addition, subtraction (+, -)

• Comparison 1 (<, <=, >, >=)

• Comparison 2 (==, !=)

• And (&&)

• Or (||)

• Assignment (=)

41
New cards

if statement

if ( expression )

statement

42
New cards

Statements

lines of instructions in programs ending with a semicolon (;).

• A compound statement/ block: series of statements surrounded by braces.

• An empty statement: a single semicolon.

43
New cards

else statement

if ( expression )

statement1

else statement2

  • only occur after an if statement

  • only executed when the if block does not execute

44
New cards

Cascading if (else-if)

if (expr1)

statement1

else if (expr2)

statement2

else if (expr3)

statement3

else

statement4

  • At most only one block will be executed

  • Choose one among several conditions

45
New cards

switch statement

switch (integer value)

{

case 1: statement1;

break; /* optional line */

case 2: statement2;

break; /* optional line */

....

default: default statement;

break; /* optional line */

}

  • The expression in the parentheses is evaluated

  • Check to see whether the result matches any constants labelled with case

  • A match is made → execution start after that case statement → carry on until the closing brace } / break statement

  • Default case are executed for all cases not specifically listed

46
New cards

Using break

All following statements are carried out until a break statement

→ break is a way of jumping straight out of the switch block

47
New cards

Loop

Repeat statement(s)

→ Provide versatile manipulation:

  • number of repetitions

  • range of loops

48
New cards

while loop

while ( condition(s), )

statement(s)

• Execute a statement according to a condition

• Verify the condition at the beginning of the loop

• Stop the loop when the condition is false ( = 0 )

49
New cards

for loop

for ( initialization ; condition ; update )

   statement

  • All components are optional → If the condition is empty, interpreted as true

  • (,) separates elements of a component

50
New cards

do … while statement

do {

statements;

} while ( condition);

→ The loop always executed at least once before the test → determine whether it should continue

51
New cards

Infinitive loops

The loop condition is always true

  • while (1)

  • for (;;)

52
New cards

Break statement

Use in an infinitive loop to terminate the loop

→ for (;;)

if (condition) break;

}

53
New cards

Continue statement

A loop will stop whatever it is doing → go straight to the start of the next loop pass

→ for (…)

{…

if ( condition) continue;

…}