Read a C program from top to bottom
This chapter turns the first example into a structure you can reuse. A small console program normally contains header inclusions, function definitions, declarations and executable statements. These parts have different jobs; indentation helps people read the program but braces define its blocks.
A complete Hello World
#include
/* Program entry point */
int main(void) {
puts("Hello, World!");
return 0;
} The #include directive makes declarations from a header available during preprocessing. It does not mean that the entire implementation of the library is pasted into your source. int is the return type of main, and (void) states that this version takes no parameters. The opening and closing braces enclose the function body.
puts prints a string followed by a newline. Its call ends in a semicolon. A preprocessor directive such as #include does not take that semicolon. return 0; communicates successful termination to the environment.
Comments and layout
A block comment begins with /* and ends with */. Modern C also supports // comments until the end of the line. Comments should explain intent, such as why a value is capped; repeating every statement in English makes maintenance harder. Block comments cannot be nested.
Three different errors
- Syntax: a missing semicolon or unmatched brace prevents translation.
- Linking: declaring a function but providing no matching definition can produce an undefined-reference error.
- Logic: printing the wrong message may compile and run successfully, but still fail the requirement.
Read the first compiler diagnostic before changing code. Later diagnostics may be consequences of the same missing punctuation.
Build and expected result
gcc -std=c17 -Wall -Wextra -pedantic hello.c -o helloRun the executable using your environment's normal command or the editor's Run button. Expected output is one line: Hello, World! The source filename can change; the entry-point function must still be named main in this hosted program.
Practice with answers
- Print your course and batch on separate lines. Use two calls to puts; each supplies its own newline.
- Can
Mainreplacemain? No: identifiers are case-sensitive. - Does a blank line change execution? No, when inserted between tokens normally separated by whitespace. Splitting a string literal across an unescaped newline is different and is invalid.
- Remove a closing brace, compile once, and restore it. Locate the block that was left open rather than adding random braces.
हिन्दी में मुख्य बात: header घोषणा देता है, main से execution शुरू होता है, braces block बनाते हैं और semicolon statement खत्म करता है। अगला अध्याय variables और data types को इसी structure में जोड़ेगा।