CSE287JUL2021 - LectureSlide - 02 (Introduction To C)
CSE287JUL2021 - LectureSlide - 02 (Introduction To C)
Computer Programming
Lecture 2:
Introduction to C
2
A first look at C
Preprocessor
Header file
Directive
End of function
A simple C program
3
A first look at C
#include
• # symbol indicates a preprocessor
• It means it has to be done before compilation
• #include to include the contents of the header file
4
A first look at C
<stdio.h>
• Name of the header file • Enclosed in < > (header in default
• Header files: constants, functions, other place)
declarations • May be enclosed in “ ” (header is in the
• You must know which header you need same folder as the source)
• Use help and documentation to find out • stdio.h : standard input/output header
file
5
• Needed for the function: printf()
A first look at C
main
• Every C program must have a ‘main’ function
• Program starts from the 1st line in ‘main’
• Parameter type void
• Return type int
• int main()
6
A first look at C
{ } curly braces
• The curly braces are like containers • Left curly brace {
• The code between two braces are called a block • Begin the body of function
• Missing either brace will generate compile error • Right Curly brace }
• “Compound Statement missing” • End of the function
7
A first look at C
printf
• A function given in stdio.h
• Prints the text given as the parameter
8
A first look at C
; (semicolon)
• Every C statement must end with a ;
• Otherwise compiler will generate an error
• Statement Missing
9
A first look at C
return 0
• Indicate how the program exited
• return 0 means that execution was successful and there was no error.
• abnormal termination is usually signaled by a non-zero return
• but there is no standard for how non-zero codes are interpreted.
• When a program is called by another program
10
Basic Structure of a C Program
• Function • Header file
• Building Block • .h extension
• Named subroutine • Add header file using #include
• Can be called by other parts of the (preprocessor directive)
program • Statement
• A program may contain more than
• Action performed by the program
one functions
• One of which must be main()
• Perform operations
• Where execution begins • End with a semicolon (;)
• Two or more statements can be
• Standard library placed on a single line
• Provides library functions
• Example: I/O operation, string,
math … etc
11
Lifecycle of a C Program
Executable
File
Acknowledgement
All the slides of this course have been prepared by taking help from numerous resources.
The notable contributors are listed below.
1. Content and organization of many pages have been taken from the lecture slides and
codes of the course CSE110 offered to the Department of EEE that were -
i. primarily created by Johra Muhammad Moosa, Assistant Professor (on leave),
CSE, BUET and
ii. later modified by Madhusudan Basak, Assistant Professor, CSE, BUET
2. Most of the wonderful coding examples have been taken from the course CSE281
offered to the Department of BME instructed by Rifat Shahriyar, Professor, CSE,
BUET (course link).
3. search and all the sites that it made available in response to the course
related queries. Some of the sites are: https://geeksforgeeks.org/,
https://www.tutorialspoint.com, https://www.w3schools.com and the list goes on …
Thank You
14