printf¶
āĻāĻŋ-āĻā§āĻžāϰā§āĻĄ āϰāĻŋāĻāĻŋāĻļāύ āĻāĻā§āϏāĻžāϰāϏāĻžāĻāĻ (Keyword Revision Exercise)¶
- āϏāĻŋ āϞā§āϝāĻžāĻā§āĻā§ā§ā§āĻā§
switch,while,forāĻāϤā§āϝāĻžāĻĻāĻŋ āĻāĻŋāĻā§āĻžāϰā§āĻĄāϏ (Keywords) āϏāϰāĻžāϏāϰāĻŋ āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞā§āϰ āύāĻžāĻŽ āĻšāĻŋāϏā§āĻŦā§ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰāĻž āϏāĻŽā§āĻĒā§āϰā§āĻŖ āύāĻŋāώāĻŋāĻĻā§āϧāĨ¤ - āϤāĻŦā§ āĻā§āϏ-āϏā§āύā§āϏāĻŋāĻāĻŋāĻāĻŋāĻāĻŋāϰ āĻāĻžāϰāĻŖā§ āĻāĻ āĻāĻŋāĻā§āĻžāϰā§āĻĄāĻā§āϞā§āϰ āϝā§āĻā§āύ⧠āĻāĻāĻāĻŋ āĻŦāĻž āĻāĻāĻžāϧāĻŋāĻ āĻ
āĻā§āώāϰ āĻāĻĒāĻžāϰāĻā§āϏ (Uppercase) āĻŦāĻž āĻŦā§ āĻšāĻžāϤā§āϰ āĻ
āĻā§āώāϰ⧠āĻĒāϰāĻŋāĻŦāϰā§āϤāύ āĻāϰ⧠(āϝā§āĻŽāύ:
Switch) āϏā§āĻāĻŋāĻā§ āĻā§āϝāĻžāϞāĻŋāĻĄ āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞ āύāĻžāĻŽ āĻšāĻŋāϏā§āĻŦā§ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰāĻž āϝāĻžā§āĨ¤
printf āĻĢāĻžāĻāĻļāύ āĻāĻŦāĻ āĻāϰā§āĻā§āĻŽā§āύā§āĻā§āϰ āĻāĻ āύ (printf Function and Arguments Structure)¶
printfāĻĢāĻžāĻāĻļāύ⧠āĻāϰā§āĻā§āĻŽā§āύā§āĻā§āϰ (Arguments) āϏāĻāĻā§āϝāĻž āĻāĻŋāύā§āύ āĻšāϤ⧠āĻĒāĻžāϰā§; āϝā§āĻŽāύ āĻāĻāĻāĻŋ āĻāϰā§āĻā§āĻŽā§āύā§āĻprintf("Neso Academy");, āĻĻā§āĻāĻŋ āĻāϰā§āĻā§āĻŽā§āύā§āĻprintf("...", var);, āĻ āĻĨāĻŦāĻž āϤāĻŋāύāĻāĻŋ āĻāϰā§āĻā§āĻŽā§āύā§āĻ āĻĨāĻžāĻāϤ⧠āĻĒāĻžāϰā§āĨ¤- āĻĒā§āϰāϧāĻžāύ āύāĻŋā§āĻŽ (Core Rule):
printfāĻĢāĻžāĻāĻļāύā§āϰ āĻĒā§āϰāĻĨāĻŽ āĻāϰā§āĻā§āĻŽā§āύā§āĻāĻāĻŋ āϏāϰā§āĻŦāĻĻāĻž āĻĄāĻžāĻŦāϞ āĻā§āĻā§āĻļāύ āĻŦāĻž āĻāĻĻā§āϧā§āϤāĻŋ āĻāĻŋāĻšā§āύā§āϰ (" ") āĻā§āϤāϰ⧠āĻĨāĻžāĻāϤ⧠āĻšāĻŦā§āĨ¤ āĻāĻžāϰāĻŖ āĻĒā§āϰāĻĨāĻŽ āĻāϰā§āĻā§āĻŽā§āύā§āĻāĻāĻŋ āĻŽā§āϞāϤ āĻāĻāĻāĻŋ āϏā§āĻā§āϰāĻŋāĻ āĻāύāϏā§āĻā§āϝāĻžāύā§āĻ (String Constant), āϝāĻž āϏā§āĻā§āϰāĻŋāύ⧠āĻĒā§āϰāĻŋāύā§āĻ āĻāϰāĻžāϰ āĻāύā§āϝ āĻā§āϝāĻžāϰā§āĻā§āĻāĻžāϰā§āϰ āĻāĻāĻāĻŋ āϏāĻŋāĻā§ā§ā§āύā§āϏ āĻŦāĻž āϏāĻŽāώā§āĻāĻŋāĨ¤
āĻĒā§āϞā§āϏāĻšā§āϞā§āĻĄāĻžāϰ āĻāĻŦāĻ āĻĢāϰāĻŽā§āϝāĻžāĻ āϏā§āĻĒā§āϏāĻŋāĻĢāĻžā§āĻžāϰ (Placeholders and Format Specifiers)¶
- āĻĄāĻžāĻŦāϞ āĻā§āĻā§āϰ āĻā§āϤāϰā§āϰ
%dāϏāϰāĻžāϏāϰāĻŋ āϏā§āĻā§āϰāĻŋāύ⧠āĻĒā§āϰāĻŋāύā§āĻ āĻšā§ āύāĻž; āĻāĻāĻŋ āĻāĻāĻāĻŋ āĻĒā§āϞā§āϏāĻšā§āϞā§āĻĄāĻžāϰ (Placeholder) āĻŦāĻž āĻĢāϰāĻŽā§āϝāĻžāĻ āϏā§āĻĒā§āϏāĻŋāĻĢāĻžā§āĻžāϰ āĻšāĻŋāϏā§āĻŦā§ āĻāĻžāĻ āĻāϰā§āĨ¤ - āĻāĻāĻŋ āĻāĻŽā§āĻĒāĻžāĻāϞāĻžāϰāĻā§ āύāĻŋāϰā§āĻĻā§āĻļ āĻĻā§ā§ āĻĄāĻžāĻŦāϞ āĻā§āĻā§āϰ āĻŦāĻžāĻāϰ⧠āĻĨāĻžāĻāĻž āĻĒāϰāĻŦāϰā§āϤ⧠āĻāϰā§āĻā§āĻŽā§āύā§āĻ āĻŦāĻž āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞā§āϰ āĻŽāĻžāύ āĻĻāĻŋā§ā§ āĻāĻ āϏā§āĻĨāĻžāύāĻāĻŋāĻā§ āĻĒā§āϰāϤāĻŋāϏā§āĻĨāĻžāĻĒāύ (Replace) āĻāϰāϤā§āĨ¤
- āĻāĻāĻžāύā§
dāĻ āĻā§āώāϰā§āϰ āĻāĻāĻāĻŋ āύāĻŋāϰā§āĻĻāĻŋāώā§āĻ āĻ āϰā§āĻĨ āϰā§ā§āĻā§; āĻāϰ āĻŽāĻžāύ⧠āĻšāϞ⧠āĻāĻāϤ āĻĄā§āĻāĻžāĻā§ āĻĄā§āϏāĻŋāĻŽā§āĻžāϞ āύāĻžāĻŽā§āĻŦāĻžāϰ (Decimal Number) āĻŦāĻž āĻĒā§āϰā§āĻŖāϏāĻāĻā§āϝāĻž (Integer) āĻšāĻŋāϏā§āĻŦā§ āĻĒā§āϰāĻŋāύā§āĻ āĻāϰāϤ⧠āĻšāĻŦā§ (ā§Ļ āĻĨā§āĻ⧠⧝ āĻĒāϰā§āϝāύā§āϤ āϏāĻāĻā§āϝāĻžāϰ āϝā§āĻā§āύ⧠āĻāĻŽā§āĻŦāĻŋāύā§āĻļāύ)āĨ¤
āĻāϰā§āĻā§āĻŽā§āύā§āĻ āĻŽā§āϝāĻžāĻĒāĻŋāĻ āĻāĻŦāĻ āĻāϰāϰ āĻšā§āϝāĻžāύā§āĻĄā§āϞāĻŋāĻ (Argument Mapping and Error Handling)¶
-
āĻā§āĻĄā§ āĻāĻāĻžāϧāĻŋāĻ āĻĒā§āϞā§āϏāĻšā§āϞā§āĻĄāĻžāϰ āĻĨāĻžāĻāϞ⧠āϤāĻžāϰāĻž āĻā§āϰāĻŽāĻžāύā§āϏāĻžāϰ⧠āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞāĻā§āϞā§āϰ āϏāĻžāĻĨā§ āĻŽā§āϝāĻžāĻĒ āĻāϰā§āĨ¤ āϝā§āĻŽāύ:
printf("%d %d", var1, var2);āϏā§āĻā§āĻāĻŽā§āύā§āĻā§ āĻĒā§āϰāĻĨāĻŽ%dāĻŽā§āϝāĻžāĻĒ āĻāϰāĻŦā§var1āĻāϰ āϏāĻžāĻĨā§ āĻāĻŦāĻ āĻĻā§āĻŦāĻŋāϤā§ā§%dāĻŽā§āϝāĻžāĻĒ āĻāϰāĻŦā§var2āĻāϰ āϏāĻžāĻĨā§āĨ¤ āϝāĻĻāĻŋvar1 = 3āĻāĻŦāĻvar2 = 4āĻšā§, āϤāĻŦā§ āĻŽāĻžāĻāĻāĻžāύ⧠āĻāĻžāϞāĻŋ āĻāĻžā§āĻāĻžāϏāĻš āĻāĻāĻāĻĒā§āĻ āĻāϏāĻŦā§3 4āĨ¤ -
āĻŦāĻžāϧā§āϝāϤāĻžāĻŽā§āϞāĻ āύāĻŋā§āĻŽ (Mandatory Rule): āĻĒā§āϰā§āĻā§āϰāĻžāĻŽā§ āϝāϤāĻā§āϞ⧠āĻĒā§āϞā§āϏāĻšā§āϞā§āĻĄāĻžāϰ (
%d) āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰāĻž āĻšāĻŦā§, āĻ āĻŋāĻ āϤāϤāĻā§āϞā§āĻ āĻŽā§āϝāĻžāĻāĻŋāĻ āĻā§āϝāĻžāϰāĻŋā§āĻžāϞ āĻāϰā§āĻā§āĻŽā§āύā§āĻ āĻĒā§āϰāĻĻāĻžāύ āĻāϰāϤ⧠āĻšāĻŦā§āĨ¤ āϝāĻĻāĻŋ āĻā§āύ⧠āĻĒā§āϞā§āϏāĻšā§āϞā§āĻĄāĻžāϰā§āϰ āĻŦāĻŋāĻĒāϰā§āϤ⧠āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞ āĻāϰā§āĻā§āĻŽā§āύā§āĻ āĻāĻŽ āĻĨāĻžāĻā§, āϤāĻŦā§ āĻāĻŽā§āĻĒāĻžāĻāϞāĻžāϰ āϏāϰāĻžāϏāϰāĻŋ āĻā§āĻā§āύ āĻāϰāϰ (Token Error) āĻĻā§āĻāĻžāĻŦā§:expected expression before...āĨ¤
āĻ ā§āϝāĻžāϰāĻŋāĻĨāĻŽā§āĻāĻŋāĻ āĻāĻā§āϏāĻĒā§āϰā§āĻļāύ āĻāĻŦāĻ āĻāĻāĻāĻĒā§āĻ āĻŽā§āϞā§āϝāĻžāϝāĻŧāύ (Arithmetic Expression and Output Evaluation)¶
- āĻāĻāĻāĻŋ āĻāĻžāĻŖāĻŋāϤāĻŋāĻ āϏāĻŽā§āĻāϰāĻŖā§āϰ āĻāĻĻāĻžāĻšāϰāĻŖ: ā§ĒāĻāĻŋ āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞ āϝāĻĨāĻžāĻā§āϰāĻŽā§
2,3,6,3āĻŽāĻžāύ⧠āĻāύāĻŋāĻļāĻŋā§āĻžāϞāĻžāĻāĻ āĻāϰāĻž āĻšāϞ⧠āĻāĻŦāĻ āϏāĻŽā§āĻāϰāĻŖāĻāĻŋ āĻšāϞā§result = var1 + var2 * var3 / var4;āĨ¤ - āĻā§āĻĄ āĻāĻā§āϏāĻŋāĻāĻŋāĻāĻļāύā§āϰ āϏāĻŽā§ āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞā§āϰ āύāĻžāĻŽāĻā§āϞ⧠āĻŽā§āĻŽāϰāĻŋāϤ⧠āϤāĻžāĻĻā§āϰ āĻāĻāĻā§ā§āĻžāϞ āĻāύāϏā§āĻā§āϝāĻžāύā§āĻ āĻā§āϝāĻžāϞ⧠āĻĻā§āĻŦāĻžāϰāĻž āĻĒā§āϰāϤāĻŋāϏā§āĻĨāĻžāĻĒāĻŋāϤ āĻšā§ (āϝā§āĻŽāύ:
2 + 3 * 6 / 3)āĨ¤ - āĻāĻŋāĻĄāĻŋāĻāϰ āϏā§āĻā§āϰāĻŋāĻĒā§āĻ āĻ
āύā§āϝāĻžā§ā§ āĻāĻžāĻŖāĻŋāϤāĻŋāĻ āĻšāĻŋāϏāĻžāĻŦā§āϰ āĻā§āϰāĻŽāĻžāύā§āϏāĻžāϰ: āĻĒā§āϰāĻĨāĻŽā§ ā§¨ āĻāĻŦāĻ ā§Š āϝā§āĻ āĻāϰ⧠ā§Ģ, āϤāĻžāϰāĻĒāϰ ⧍ā§Ŧ āĻĻāĻŋā§ā§ āĻā§āĻŖ āĻāĻ°ā§ ā§Šā§Ļ āĻāĻŦāĻ āϏāĻŦāĻļā§āĻˇā§ ā§Š āĻĻāĻŋā§ā§ āĻāĻžāĻ āĻāϰ⧠āĻā§ā§āĻžāύā§āϤ āĻĢāϞāĻžāĻĢāϞ āĻŦāĻž
resultāĻāϰ āĻŽāĻžāύ āĻĻāĻžāĻā§āĻžā§10āĨ¤ - āϏāĻŽā§āĻāϰāĻŖāĻāĻŋ āĻĒā§āϞā§āϏāĻšā§āϞā§āĻĄāĻžāϰ āĻĻāĻŋā§ā§ āĻĒā§āϰāĻŋāύā§āĻ āĻāϰāĻžāϰ āύāĻŋā§āĻŽ:
āĻāĻāĻŋ āĻāύāϏā§āϞ āĻāĻāύā§āĻĄā§āϤ⧠āĻĒā§āϞā§āϏāĻšā§āϞā§āĻĄāĻžāϰāĻā§āϞ⧠āϏāϰāĻŋā§ā§ āϏāϰāĻžāϏāϰāĻŋ āĻŽāĻžāύāϏāĻš āĻāĻā§ā§ā§āĻļāύāĻāĻŋ āĻĒā§āϰāĻŋāύā§āĻ āĻāϰāĻŦā§:2 + 3 * 6 / 3āĨ¤ - āĻā§ā§āĻžāύā§āϤ āĻĢāϞāĻžāĻĢāϞ āĻĒā§āϰāĻŋāύā§āĻ āĻāϰāĻžāϰ āĻāύā§āϝ āĻĻā§āĻŦāĻŋāϤā§ā§
printfāϏā§āĻā§āĻāĻŽā§āύā§āĻ:printf("and the result of this expression is %d", result);, āϝāĻž āϏā§āĻā§āϰāĻŋāύā§10āĻāĻāĻāĻĒā§āĻ āĻĒā§āϰāĻĻāϰā§āĻļāύ āĻāϰāĻŦā§ āĻāĻŦāĻ āϏāĻĢāϞ āϏāĻŽāĻžāĻĒā§āϤāĻŋ āĻšāĻŋāϏā§āĻŦā§process returned 0āĻŽā§āϏā§āĻ āĻĻā§āĻāĻžāĻŦā§āĨ¤
Keyword Revision Exercise¶
- Direct utilization of keywords (āĻāĻŋāĻā§āĻžāϰā§āĻĄ) like
switch,while, orforas variable names is strictly prohibited in C. - However, due to the language being case-sensitive, changing one or more letters to uppercase (āĻāĻĒāĻžāϰāĻā§āϏ) (e.g.,
Switch) converts it into a valid (āĻŦā§āϧ) variable name configuration.
Structure of the printf Function and Arguments¶
- The number of arguments (āĻāϰā§āĻā§āĻŽā§āύā§āĻ) supplied to a
printffunction can vary: a single argument likeprintf("Neso Academy");, two argumentsprintf("...", var);, or three arguments. - Core Rule: The first argument of the
printffunction must strictly be enclosed within double quotes (" "). This is because the initial argument is always a string constant (āϏā§āĻā§āϰāĻŋāĻ āϧā§āϰā§āĻŦāĻ), representing a sequence of characters to be printed directly onto the screen.
Understanding Placeholders and Format Specifiers¶
- The character sequence
%dinside the double quotes does not get printed literally; it functions as a placeholder (āĻĒā§āϞā§āϏāĻšā§āϞā§āĻĄāĻžāϰ) or format specifier. - It instructs the compiler to retrieve the subsequent sequential argument outside the quotes and replace the placeholder with that specific variable's value.
- The letter
dholds a specific architectural meaning, denoting a decimal number (āĻĻāĻļāĻŽāĻŋāĻ āϏāĻāĻā§āϝāĻž), which mandates the system to print the incoming data as an integer composed of digits from 0 to 9.
Argument Mapping and Error Handling¶
-
When multiple placeholders are used, they map sequentially (āĻā§āϰāĻŽāĻžāύā§āϏāĻžāϰā§) to the subsequent variable arguments. For instance, in
printf("%d %d", var1, var2);, the first%dmaps tovar1and the second maps tovar2. Ifvar1 = 3andvar2 = 4, the output rendered on the console will be3 4separated by a blank space. -
Mandatory Constraint: For every single placeholder (
%d) declared inside the format string, a matching variable argument must be provided. Failing to supply a corresponding variable for any placeholder causes the compiler to generate a token error:expected expression before....
Arithmetic Expression and Output Evaluation¶
- Consider an arithmetic expression where 4 variables are initialized to
2,3,6, and3respectively, forming the expression:result = var1 + var2 * var3 / var4;. - During execution, variable names are replaced under the hood by their respective constant values (
2 + 3 * 6 / 3). - According to the strict execution sequence detailed in the video: 2 plus 3 yields 5, multiplied by 6 yields 30, and divided by 3 results in a final computed valuation of
10stored inside theresultvariable. - To print the structure of the equation using variables:
This replaces the specifiers to cleanly render the numbers on the screen as:2 + 3 * 6 / 3. - The second statement
printf("and the result of this expression is %d", result);replaces the placeholder with the calculated variable content to output10onto the console window, concluding with a successfulprocess returned 0indicator.