Study Web

Weekly Programming Topics

Programming in Other Languages

Week 11 – Programming in Other Languages · Table content

Programming in Other Languages

Week 11 – Programming in Other Languages

Table content

  1. Introduction to C
  2. Creator of C – Dennis Ritchie
  3. Differences between C and Python
  4. C Programming Language
  5. C String Handling
  6. C Naming Conventions
  7. Statements, Variables, and Constants
  8. Functions, Procedures, and Parameters
  9. Custom Data Types
  10. Array
  11. Assignment Statements
  12. Pointers
  13. Branching
  14. Loops
  15. Return or Exit
  16. Strings
  17. The string.h Library
  18. Terminal I/O
  19. Using Libraries and Header files
  20. Introduction to C C is a foundational programming language with the following key characteristics:
  21. A statically typed language – variables must be declared with a specific type and can only hold that type
  22. A compiled language – source code is translated into machine code before running
  23. A functional language that supports structured programming
  24. Developed by Dennis Ritchie between 1971 and 1973
1.1 Creator of C
Dennis Ritchie
  • Ritchie was also heavily involved in the development of the Unix operating system. Ritchie's death did not receive much news coverage, as the media was focused on Steve Jobs who died the week before [1]
  • "Ritchie was under the radar. His name was not a household name at all, but [...] if you had a microscope and could look in a computer, you'd see his work everywhere inside." — Computer historian Ceruzzi [2] [1] Srinivasan, Rajeev (October 25, 2011). "Dennis Ritchie, a tech genius as great as Steve Jobs". Firstpost. Retrieved December 4, 2017. [2] Langer, Emily (October 14, 2011). "Dennis Ritchie, founder of Unix and C, dies at 70".

1.2 Differences between C and Python The concepts you have already learned in Python are largely the same in other languages:

  • Variables and constants
  • Loops and conditional statements
  • Arrays to hold multiple values
  • Library files for code that is already written and can be re-used

1.2 Differences between C and Python (cont.) C is a language that may be described as "closer to the machine":

  • It does less for you automatically (so you can only have it do exactly what you specify)
  • It generates smaller, faster code – trading developer productivity for performance
  • It can appear cryptic and produces more cryptic error messages than Python C syntax is the basis for many modern languages – so knowing C is useful for syntax alone
  • We use the GNU C compiler (gcc) or C Language family front-end (clang)
  • In labs you may need to run mingw (in Windows) or Terminal (Mac OS)

1.2 Differences between C and Python (cont.) Coming from Python, here are the key differences you will encounter in C Feature Python C Case Sensitivity Case sensitive Case sensitive

Strings Native string type No strings
use char arrays
Memory Management Automatic (garbage collected) Manual
allocate and free memory yourself Function Return Types Any type, no declaration needed Must declare return type explicitly
Passing by Pass by object No references
pass pointers instead
Returning Arrays Arrays returnable directly Cannot return arrays
pass pointer to first element
Array / List Size Dynamic
lists resize automatically
Fixed size
you manage the size yourself

2.1 C String Handling

String handling in C is much lower-level than Python:

  • In Python, strings are a built-in type with rich methods (e.g..upper(),.split(), len())
  • In C, a string is either a char* (pointer) or a char array of fixed size: char my_array[n]
  • C strings are arrays of characters terminated with the null character '\0'
  • You must ensure you never read or write beyond the end of the array
  • You must store and track the length of the array yourself – Python does this for you

2.2 C Naming Conventions

C naming conventions are similar to Python's PEP 8 style:

  • lower_case – used for identifiers such as functions/procedures, types, and variables
  • UPPER_CASE – used for constants (same as Python convention)
  • Indent within { } and control structures (C uses braces rather than Python's indentation)

2.3 Statements, Variables, and Constants In both Python and C, a program is a list of statements where each statement is an instruction that commands the CPU to perform a specific action, differing only in syntax.

2.3 Statements, Variables, and Constants (cont.) Variables and constants store values. In Python you do not need to declare a type; in C you must explicitly identify the TYPE of the variable:

2.4 Functions, Procedures, and Parameters Code is divided into functions and procedures with parameters and local variables. In C, you must explicitly identify the TYPE of the function and the TYPE of each parameter. In Python, you don’t need to.

2.4 Functions, Procedures, and Parameters (cont.) A procedure call is a statement that uses the procedure's name to run its code, passing in values (expressions) as parameters.

2.4 Functions, Procedures, and Parameters (cont.) In C: In Python: Q: Do you notice the differences?

2.5 Custom Data Types

Both C and Python allow you to create custom data types, though the syntax is different. C provides several kinds of custom types as below:

2.5 Custom Data Types (cont.) In C: In Python: Q: Do you notice the differences?

2.6 Array

An array stores multiple values of the same type, each accessed by an index starting at 0, and in C you must declare its type and size explicitly.

2.6 Array (cont.)

Python lists are flexible and dynamic. C arrays are fixed-size and must be declared with an explicit type and size: In C: In Python:

2.7 Assignment Statements

An assignment statement calculates the value of an expression on the right-hand side and stores it into a variable on the left-hand side.

2.7 Assignment Statements (cont.)

In C: In Python:

2.8 Pointers

A pointer is a type built into the C programming language that stores a memory address, which "points to" a value stored elsewhere in memory. You can use the value in the pointer to access and interact with the value it points to.

2.8 Pointers (cont.)

Python handles this automatically using references. In C you must manage this explicitly. Example: In week 7, we discovered that Python uses Pass by Object Reference. In C, you explicitly manage whether you pass a value or a pointer to a memory location. Q: Can you guess what is the difference between pass by value and pass by pointer in C?

2.9 Branching

2.9 Branching (cont.)

Both Python and C use if statements for branching: In C: In Python:

  • Python has elif; C uses else if
  • C has a switch statement similar to Python's match statement (Python 3.10+)

2.10 Loops

2.10 Loops (cont.)

Both languages support looping. A condition is checked each loop to control whether the loop runs again or ends. C supports three loop forms: While loop (C vs. Python) For loop (C vs. Python)

C also has a do-while loop (runs at least once), which Python does not have natively:

2.11 Return or Exit

Jumping actions change the code sequence, allowing you to exit a function early). Both Python and C use return for this and the syntax is nearly identical. In C: In Python:

2.12 Strings

Not all languages have the same level of string support. Python offers rich, built-in string handling while C requires manual memory management for strings: In C: In Python:

  • Python's f-strings (name = f"{first} {last}") make string concatenation trivial.
  • In C, you must manually allocate memory (malloc), format the string (sprintf), and then free the memory when done.

2.13 The string.h Library

  • In C you need to use a function to compare strings (since they are just arrays of characters).
  • The strcmp() function checks each character to see if it is the same in both arrays. If the arrays have the same content, strcmp returns 0; otherwise it returns a non-zero integer.
  • You will need this for this week’s HD task.

2.14 Terminal I/O

Input

  • Read from the user with our supplied functions, or use scanf() / gets() to read values. scanf() use format strings to indicate the format of the input data. See 3_io-input.c for detail.
  • You can use the supplied terminal user input code – access the string characters using the.str field of the record/struct. See terminal_user_input.c and terminal_user_input.h for detail. Output:
  • Use printf() to write to the terminal. printf() is defined in the library <stdio.h>.

2.15 Using Libraries and Header files

  • In C you need to #include libraries. This is similar to Python's import statement
  • C has header files (.h) that contain function prototypes – declarations of what functions exist and their signatures. The actual implementations are in the matching.c files. Example from terminal_user_input.h:

2.15 Using Libraries and Header files (cont.)

  • The implementation in terminal_user_input.c:
  • This demonstrates information hiding and encapsulation – the public interface (.h) is separate from the private implementation (.c), similar to Python modules.

Summary:

We have looked at the C language and compared it to your existing Python knowledge. Key takeaways:

  • The concepts are largely the same – variables, loops, conditionals, functions, arrays, libraries
  • The syntax is different: C requires type declarations, uses { } for blocks, and ends statements with;
  • C gives you more control but requires more effort – memory management, string handling, and type management are all your responsibility
  • Python does much of this automatically; C does not
  • Knowing C is valuable because its syntax underpins many modern languages (C++, Java, JavaScript, Swift, etc.)