[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()
, andscanf_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 */
- Multi-line comment:
/* This is a
multi-line
comment */
- Since C99:
// Comment from here to end of the line
○ Preprocessor
Preprocessing refers to tasks performed before compilation starts.
Example:
#include <stdio.h>
-
#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
○ Structure of a Function
Example:
int main(void) {
return 0;
}
-
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
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;
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)
○ printf()
and scanf()
printf()
Example:
int x = 10;
printf("%d", x); // %d = decimal integer
-
%d
tellsprintf
to print an integer in decimal format
scanf()
Example:
float radius;
printf("Enter radius: ");
scanf("%f", &radius);
-
&
(ampersand) passes the address of the variable soscanf()
knows where to store the input
○ Omitting
&radius
(e.g.,scanf("%f", radius);
) will cause an error.
- Use
%lf
to read adouble
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
○ Practice Exercises
- Input two numbers and print the sum
- Calculate monthly salary based on annual salary
Top comments (0)