Podcast
Questions and Answers
Who created the C programming language?
Who created the C programming language?
The C programming language was developed before the B programming language.
The C programming language was developed before the B programming language.
False
What are two research interests of Dr. Frank Guan?
What are two research interests of Dr. Frank Guan?
Virtual Reality and Artificial Intelligence
C was created by _______ at Bell Lab.
C was created by _______ at Bell Lab.
Signup and view all the answers
Match the programming languages with their main features:
Match the programming languages with their main features:
Signup and view all the answers
What is one of the suggested formats for questions during the lecture?
What is one of the suggested formats for questions during the lecture?
Signup and view all the answers
The lecture is conducted in person.
The lecture is conducted in person.
Signup and view all the answers
Which book is recommended for learning C programming?
Which book is recommended for learning C programming?
Signup and view all the answers
Which year was the C99 standard introduced?
Which year was the C99 standard introduced?
Signup and view all the answers
C programming language supports garbage collection.
C programming language supports garbage collection.
Signup and view all the answers
What are the names of the lead developers of UNIX?
What are the names of the lead developers of UNIX?
Signup and view all the answers
C is a ______-level language, which typically results in faster code execution.
C is a ______-level language, which typically results in faster code execution.
Signup and view all the answers
Which of the following languages were influenced by C?
Which of the following languages were influenced by C?
Signup and view all the answers
Match each programming feature with the appropriate language:
Match each programming feature with the appropriate language:
Signup and view all the answers
In Python, variable declarations are mandatory.
In Python, variable declarations are mandatory.
Signup and view all the answers
What does a compiler do in the context of C programming?
What does a compiler do in the context of C programming?
Signup and view all the answers
What symbol is used to terminate statements in C?
What symbol is used to terminate statements in C?
Signup and view all the answers
The main() function is an optional part of a C program.
The main() function is an optional part of a C program.
Signup and view all the answers
What is the purpose of using braces {} in C?
What is the purpose of using braces {} in C?
Signup and view all the answers
In C programming, all code must be placed within __________.
In C programming, all code must be placed within __________.
Signup and view all the answers
Match the following programming constructs with their language:
Match the following programming constructs with their language:
Signup and view all the answers
Study Notes
Introduction to C Programming
- Welcome to the course
- Instructor: A/Prof Frank Guan, PhD
- Course name: INF1002 – Programming Fundamentals
- Week 8: Introduction to C
- Course themes: Thinking, Able to Learn, Catalysts, and Grounded, as a community, transformative learning
- Research Interests: Virtual Reality (VR), Augmented Reality (AR), Artificial Intelligence (AI), Entrepreneurship & innovation
- Contact Email: [email protected]
Course Housekeeping
- Lectures are online (Zoom)
- Recordings are available
- Focus on the lecture and limit distractions
- Ask questions: voice up or type in the window
- Respectful and honest conduct within the community
Agenda for Week 8
- History of C
- From Python to C
- Simple C program
- Basic data types
- C formatted I/O
- Control structures
Recommended Reading
- Paul Deitel and Harvey Deitel, C: How to Program, 9th Edition (Pearson, 2021)
History of C
- J. Presper Eckert and John Mauchly (1943–1946): Electronic Numerical Integrator and Computer
- Martin Richard (1966–1967): Basic Combined Programming Language
- Ken Thompson (1969, Bell Lab): Developed B, based on BCPL
- Dennis Ritchie (1972–1973, Bell Lab): Created C, rewriting UNIX in C (1973)
- 1970s: Traditional C
- 1989: C89/ANSI C/Standard C
- 1990: ANSI/ISO C
- 1999: C99: attempt to standardize variations
C vs. Related Languages
- More recent derivatives: C++, Objective C, C#
- Influenced by: Java, Perl, Python (quite different)
- C lacks: Exceptions, Range-checking, Garbage collection, Object-oriented programming
- Low-level languages, usually faster code
Compilers vs. Interpreters
- Python uses an interpreter to execute a program
- In C, the compiler converts a program into machine code
- Machine code can be executed directly by the CPU
Variable Declarations
- C requires variable declarations, informing the compiler about the variable before it's used, specifying type.
- Once declared, a variable's type cannot be changed.
- In Python, no declarations are needed.
Whitespace
- Python uses whitespace characters for identifying statements and code blocks
- C uses whitespace only to separate words.
Functions
- All C code is within functions
- The
main()
function is the starting point for a C program
Good Coding Practices in C
- Indent blocks of code as in Python.
- Format braces and whitespace consistently
- Use comments to explain code for other programmers.
- Use meaningful variable names.
- Pay attention to compiler warnings (indicate syntax correct but potential run-time errors).
- Avoid system-specific features (the code should run on diverse systems without modification).
Simple C Program
- /* ... */ indicates comments (ignored by the compiler)
-
#include <stdio.h>
includes the standard input/output library -
int main() { ... }
defines the main program function -
printf("Hello world!\n");
displays output on the screen -
return 0;
signifies successful execution.
C Preprocessor
- Executes before compilation
- Defines symbolic constants
- Includes other files
#define directive
- Creates symbolic constants in C
- Replaces occurrences during compilation.
#include directive
- Includes header files into the program for necessary functions (such as
stdio.h
for standard input/output) - Normal usage includes angle brackets
<>
(standard libraries) or double quotes""
(user-defined files) for specifying location - Two forms of
#include
specify standard or user-defined file location.
Header files
- Contain function prototypes (declarations without body):
- Definitions of constants.
- Definitions of various data types.
Variable Declarations in C
- Must specify type before use (
int
,float
,char
). - Variables cannot change type once declared.
- Variables are named
integer1, integer2, sum
scanf() Function in C
- Reads formatted input from the standard input (usually the keyboard)
Formatted Input/Output
-
printf()
formats and displays output to the console- Uses format specifiers (e.g.,
%d
,%f
,%s
) to define the output format.
- Uses format specifiers (e.g.,
-
scanf()
reads input from the console- Uses format specifiers for input.
-
puts()
prints a string and prepends a newline at the end. -
getchar()
reads and returns a character from the console as an integer
Escape Sequences
- Used to represent special characters in strings
- Examples include
\n
(newline),\t
(tab), and\"
(double quote)
C Control Structures
-
if
/else
for decision-making -
switch
for multiple-case decisions -
for
andwhile
loops for iteration -
do-while
loop
Arrays
- An array is a collection of elements of the same type, stored contiguously in memory.
- Character arrays are used to represent strings.
- Arrays can be declared/initialised, and array elements can be identified using indices, starting with zero.
Structure
- A structure in C is a collection of variables of different types, grouped together under one name.
- Structures can be passed to functions in C
Dynamic Memory Allocation
- Allocates memory at runtime
- Allocates memory for variables that are not known in advance, using
malloc()
andcalloc()
. -
malloc()
: Allocates a block of memory in heap. -
calloc()
: Allocates a block of memory and sets all bytes to zero.
Void Pointers
- Can point to any data type
- Should be cast before dereferencing
Pointers
- Variables that stores memory addresses
- Declaration via
int *ptr
Strings
- Array of characters ending in
\0
C File Handling
-
fopen()
: Opens a file.
fprintf()
- Writes formatted data to a file.
fscanf()
- Reads formatted data from a file.
fclose()
- Closes a file.
File Types
- Binary or text
Random Access Files
- Can access any file position.
- Records are of the same length.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Test your knowledge about the C programming language with this comprehensive quiz. Explore its history, features, and key concepts, including important developers and standards. Whether you are a beginner or have some experience, this quiz will challenge your understanding of C.