Programming Logic Using ‘C’
BCAC0001
Lect-3 C Program Structure
Presented by:
Atul Kumar Uttam
Assistant Professor
Computer Engineering & Applications Department,
GLA University, Mathura
atul.uttam@gla.ac.in, +91-8979593001
C Program Structure
/*Comments*/ Optional, description of program
preprocessing commands To include header files, macro declaration
global Declarations; Optional, declare variables common to all functions
int main() Entry point of the program
{
Compulsory, user defined functions,
local declaration; start point of the program
executable statements;
Body of main function
-
return 0;
}
user defined functions()
{ Optional
fun definition
BCAC0001 Programming Logic using C :
} Atul Kr Uttam
A simple C program
#include<stdio.h>
int main(void)
{
printf(“Hello World”);
return 0;
}
BCAC0001 Programming Logic using C :
Atul Kr Uttam
The #include Directive
• Instructs the compiler to include the contents of the
file (stdio.h)
• Always begin with a # character
• Do not end with a semicolon ;
BCAC0001 Programming Logic using C :
Atul Kr Uttam
stdio.h
• stdio.h (standard input output)
• Contains declarations of data input and output
functions
printf(), scanf()
• Order of inclusion of header files does not matter
BCAC0001 Programming Logic using C :
Atul Kr Uttam
Brackets <file_name>
• The compiler searches in system dependent predefined
directories, where system files reside.
• "file_name" the compiler usually begins with the
directory of the source file(pwd), then searches the
predefined directories.
• If the file is not found, the compiler will produce an error
message and the compilation fails.
BCAC0001 Programming Logic using C :
Atul Kr Uttam
The main() Function
• Every C program must contain at least one function
• main( ) is a compulsory function
• Pre-declared
• User defined
• Tells the entry point of the program
BCAC0001 Programming Logic using C :
Atul Kr Uttam
int main() Function
• Called automatically when the program runs, by OS.
• The execution of the program ends
– when the last statement of main() is executed,
– unless an exit statement such as return is called earlier.
• The keyword int indicates that main() must return an
integer to the operating system when it terminates.
• The value 0 indicates normal termination.
BCAC0001 Programming Logic using C :
Atul Kr Uttam
The main() Function
• You may see other declarations such as:
void main()
• It is illegal according to the ANSI C standard because main()
must return a value.
• Because the return type is int by default, it is allowed to omit
the word int and write
main( )
{
BCAC0001 Programming Logic using C :
Atul Kr Uttam