DEV Community

San Kang
San Kang

Posted on

[Adult Learning Log] C Language - Week 2 Review

[C Language Week 2] Comments, Preprocessor, Variables, and More

○ Key Takeaways from Week 2

  • Hands-on practice: Simple calculator, monthly salary calculator
  • Learned that most C programs define functions inside the main function
  • Learned three ways to write comments in C
  • Understood what a preprocessor is, why it’s used, and the meaning of directives like #include <stdio.h>
  • Learned the structure of functions and the significance of return values
  • Covered variable declarations, identifier naming rules, and standard data types
  • Practiced using printf(), scanf(), and scanf_s() functions

○ Typical Program Structure

Most programs follow this pattern: Input → Process → Output


○ Comments in C

C supports the following types of comments:

  • Single-line comment:
  /* This is a single-line comment */
Enter fullscreen mode Exit fullscreen mode
  • Multi-line comment:
  /* This is a
     multi-line
     comment */
Enter fullscreen mode Exit fullscreen mode
  • Since C99:
  // Comment from here to end of the line
Enter fullscreen mode Exit fullscreen mode

○ Preprocessor

Preprocessing refers to tasks performed before compilation starts.

Example:

#include <stdio.h>
Enter fullscreen mode Exit fullscreen mode
  • #include: directive to include external files
  • stdio.h: standard input/output header file
    • stdio = Standard Input Output
    • .h = Header file

This allows access to functions like scanf() and printf().

Why use header files?

  • Organize frequently used code (functions, constants, structures, etc.)
  • Reduce duplication, improve modularity and maintenance
  • Directives like #include allow reusable code blocks

Example 2:

#define _CRT_SECURE_NO_WARNINGS  // Used in Visual Studio to suppress scanf warnings
Enter fullscreen mode Exit fullscreen mode

○ Structure of a Function

Example:

int main(void) {
    return 0;
}
Enter fullscreen mode Exit fullscreen mode
  • int: return type (integer)
  • main: function name (entry point)
  • void: no input parameters
  • { }: function body
  • return 0;: signals successful termination

return 0 = Success

return 1 or other values = Error or abnormal termination


○ Variables

Declaration:

int x;        // Declares integer variable x
float radius; // Declares float variable
Enter fullscreen mode Exit fullscreen mode

Standard Data Types:

  • Integer types: short, int, long long
  • Floating-point types: float, double, long double
  • Character type: char (read as “character”)

Identifier Rules:

  • Must consist of letters, digits, and underscores (_)
  • Cannot contain spaces
  • First character must be a letter or _ (not a digit)
  • Case-sensitive
  • Cannot use reserved keywords (e.g., int, float)

Good names describe purpose: year, bank_account, BankAccount

Poor names: i, j, k (unclear meaning)

Initialization:

int x = 10;
int y = 20;
int sum = 0;
Enter fullscreen mode Exit fullscreen mode

Multiple variables of the same type can be initialized in one line:

int width = 100, height = 200;  // Recommended
int width, height = 200;        // Not recommended (only height is initialized)
Enter fullscreen mode Exit fullscreen mode

printf() and scanf()

printf() Example:

int x = 10;
printf("%d", x);  // %d = decimal integer
Enter fullscreen mode Exit fullscreen mode
  • %d tells printf to print an integer in decimal format

scanf() Example:

float radius;
printf("Enter radius: ");
scanf("%f", &radius);
Enter fullscreen mode Exit fullscreen mode
  • & (ampersand) passes the address of the variable so scanf() knows where to store the input

○ Omitting &radius (e.g., scanf("%f", radius);) will cause an error.

  • Use %lf to read a double value (lf = long float)

scanf() vs scanf_s() and Security

In Visual Studio, using scanf() often triggers a warning recommending scanf_s().

Why?

To prevent buffer overflow, which is a common vulnerability.

  • scanf() without input size limits can cause excessive input to overwrite memory
  • This can allow attackers to execute arbitrary code (a classic exploit)

This technique is known as pwnable hacking

  • Many systems (Windows, Adobe, UNIX tools) have been exploited in the past due to such vulnerabilities
  • scanf_s() is a Microsoft-specific secure function that forces the developer to specify buffer sizes
  • Not portable to Linux/GCC environments → safer alternatives like fgets() are often used

To disable the warning in Visual Studio:

#define _CRT_SECURE_NO_WARNINGS
Enter fullscreen mode Exit fullscreen mode

○ Practice Exercises

  1. Input two numbers and print the sum

Image description

  1. Calculate monthly salary based on annual salary

Image description

Top comments (0)