Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Foundational Concepts of the C Programming Language

Notes Nov 11 21

sizeof is an operator that returns the memory size (in bytes) occupied by a data type or variable.

The scanf function returns the number of successfully read items; if it fails to read any valid input, it returns EOF (a macro defined as -1, short for End of File). For continuous integer input, use a loop like while (scanf("%d", &num) == 1) — this loop runs as long as a valid integer is read, termianting when scanf returns EOF.

scanf skips all leading whitespace characters before starting to read, and stops reading at the first subsequent whitespace character, leaving remaining characters in the input buffer. The buffer content persists until it is either consumed by another read operation or explicitly flushed (e.g., fflush(stdin)). For continuous character input, use getchar() in a loop: while ((c = getchar()) != EOF). getchar() returns the ASCII value of the read character, or EOF on failure. To handle leftover newline characters in the buffer after scanf, call getchar() once to consume the newline.

Format Specifiers

  • %d: Prints integers in decimal format
  • %c: Outputs a single character
  • %s: Prints null-terminated strings
  • %f: Displays float values, defaulting to 6 decimal places. Use %.nf to specify n decimal places, or %mf to set a minimum output width of m characters with right alignment if needed.
  • %lf: Prints double-precision floating-point values
  • %ld: Prints long integers
  • %lld: Prints long long integers
  • %p: Shows memory addresses in hexadecimal format
  • %zd: Outputs values of type size_t
  • %u: Prints unsigned integers
  • %x: Displays unsigned integers in hexadecimal format
  • %.0f: Trims a float to its integer part with standard rounding

Data Types

C provides several built-in data types, including char, short, int, long, long long, float, and double. You can also define custom types like structs. A signed int tpyically ranges from -2147483648 to 2147483647.

1 byte equals 8 bits. A single Chinese character usually occupies 2 bytes, and 1 KB is 1024 bytes. A literal like 55.5 is treated as a double by default; append 'f' (e.g., 55.5f) to declare it as a float.

Variables and Constants

When a local variable and a global variable share the same name, the local variable takes precedence in its scope.

Scope and Lifetime

  • Scope: Local variables are only accessible within their declaring block or function. Global variables are accessible across the entire project.
  • Lifetime: Local variables are created when entering their scope and destroyed when exiting. Global variables exist for the entire duration of the program's execution.
  • Uninitialized global variables are automatically set to 0, while uninitialized local variables contain undefined (random) values. The extern keyword is used to declare variables or functions defined in another source file.

Strings

Strings in C are terminated by a null character ('\0'). When using printf with %s, it prints characters starting from the given address until it encounters '\0'. The printf function returns the number of characters printed, including the implicit null terminator for strings. Use %02d to print an integer with a fixed width of 2 characters (padding with leading zeros if needed), and %.2d to ensure the integer displays at least two digits.

The strlen library function calculates string length by counting characters from the starting address until it hits '\0' (the null terminator is not counted). If a character array does not end with '\0', strlen will return an undefined value as it reads past the array boundaries.

Key Differences Between sizeof and strlen

  1. sizeof is an operator, while strlen is a standard library function.
  2. sizeof measures the total memory size (in bytes) of its operand, regardless of stored data. strlen specifically counts string characters before '\0' and only works with strings.

Escape Characters

Escape Character Description
\? Avoids trigraph interpretation when writing multiple consecutive question marks
\' Represents a single quote character constant
`\

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.