Introduction To Programming
Introduction To Programming
Rezoun
Lecturer, Dept. of CSE
Sonargaon University
E-mail: abusaid.rezoun@gmail.com
Contact: 01788210077
Structure of C Programming
Component in a program code
◦ Preprocessor Directives / Library
◦ Reserved Word
◦ Data Type
◦ Identifiers and their declarations
◦ Comments
◦ Escape Characters
◦ Operators
◦ Preprocessor Functions
Good Programming Style
Preprocessor
Directives
Reserved Word
Data Type
Identifiers
Comments
Escape
Character
Operators
Preprocessor
Functions /
statements
In C programming, the preprocessor
directives start with #include
It is the specific line to tells the preprocessor
to include the contents of the predefined
function in to the program from the header
file
The header file contains information and
declarations used by the compiler when
compiling the predefined functions
Each header file consists of specific function
to be applied
Examples:
Header File Explanation
<stdio.h> Contains functions prototypes for the
standard input output library function
and the information to use them
<stdlib.h> Contains function prototypes for
conversions of numbers to text and
text to numbers, memory allocation,
random number, and other utility
functions
<string.h> Contains function prototypes for string
processing functions
<time.h> Contains function prototypes and
types for manipulating the time and
date
<math.h> Contains function prototypes for math
library functions
- identify language entities, such as
statements, data types, and language element
attributes;
- They have special meaning to compiler, must
appear in correct location in program, typed
correctly, and used in correct context.
- Case sensitive: differentiates between
lowercase & uppercase letter.
- Example: const, double, int, return
A data type is a set of data values and a set of operations on those
values.
Usually has two classes of data types:
◦ Built-in data types
◦ Programmer-defined data types (covered in Structures)
In addition, the C language has two classes of built-in data types:
◦ Fundamental data types:
corresponds to the most common, fundamental storage units of a
computer and the most common, fundamental ways of using such
data.
Example: int, char, double, float, void
Some of these data types have short, long, signed, and unsigned
variants.
short 2
unsigned short 2
int 4
unsigned int 4
long 4
unsigned long 4
/*Function body*/
23
There are several types of statements in :
1.Expression statements
2.Selection statements
3.Repetition statements
4.Jump statements
5.Labeled statements
6.Compound statements
Compound Statement
• Is a list of statements enclosed in braces, { }.
• Can contain any number of statements and
declarations.
• Even though all statements must end with
semicolons, a compound statement does not need
the semicolon delimiter after right brace, }.
Writing
a
Program
#include <stdio.h>
return 0;
27
What is Programming Style?
28
Comments
Ideally, a comments serves two purposes:
29
•Heading
The first comment should contain the name of the program.
Also include a short description of what it does. You may have
the most amazing program but it is useless if no one knows
what it does.
•Author
You’ve gone to a lot of trouble to create this program. Take
credit for it. Also, if someone else must later modify the
program, he or she can come to you for information and help.
•Purpose
Why did you write this program? What does it do?
30
•Usage
In this section give a short explanation of how to run the program.
In an ideal world, every program comes with a set of documents
describing how to use it.
•References
Creative copying is a legitimate form of programming (if you don’t
break the copyright laws in the process). In the real world, it
doesn’t matter how you get a working program, as long as you get
it; but, give credit where credit is due. In this section you should
reference the original author of any work you copied.
•Restrictions
List any limits or restrictions that apply to the program, such as:
The data file must be correctly formatted; the program does not
check for input errors.
•Revision history
This section contains a list indicating who modified the program
and when and what changes have been made.
31
Variable Name
A variable is a place in the computer’s memory for storing a value.
Names can be any length and should be chosen so their meaning
is clear.
The following declaration tells C that you are going to use three
integer (int) variables named r, s,Avoid
t: using abbreviations
int r, s, t;
Consider another declaration:
int account_number;
int balance_owed;
32
Indentation and Code
Format
The general rule for a C program is to indent one level for each
new block or conditional.
There are two styles of indentation. Use any of the two, but make
sure you are consistent in using the style that you choose.
33
1st Style: braces are put on the same line as the
statements
int main(void) {
/*Variable declarations*/
double gross_income;
double town_tax;
/*Function body*/
return 0;
34
2nd Style: puts the curly braces on lines by
themselves
int main(void)
{
/*Variable declarations*/
double gross_income;
double town_tax;
/*Function body*/
return 0;
35
/*Function body*/
return 0;
37
A better version would be:
/* Swap X coordinate */
temp = box_x1;
box_x1 = box_x2;
box_x2 = temp;
/* Swap Y coordinate */
temp = box_y1;
box_y1 = box_y2;
box_y2 = temp;
38
Some styles conventions commonly used to produce
readable code.
1. Insert blank lines between consecutive program sections, as shown
below:
double gross_income;
double town_tax;
39
Example 1:
#include <stdio.h>
#include <stdio.h>
printf(“Welcome”);
printf(“to C!\n|”);
return 0;
}
Example 3:
#include <stdio.h>
int main(void)
{
printf(“Welcome \nto\nC!\n”);
return 0;
}
Any Questions??
1) Print the hello world sentence.
Output:
Hello World!
Output:
A * (asterisk) B | (pipeline)
. .
C - (hyphen) D _ (underscore)
. .
Answer: Option D
Explanation:
Variable names in C are made up of letters (upper and lower case)
and digits. The underscore character ("_") is also permitted. Names
must not begin with a digit.
3 How would you round off a value from 1.66 to 2.0?
.
A ceil(1.66) B floor(1.66)
. .
C roundup(1.66) D roundto(1.66)
. .
Answer: Option A
Explanation: ceil() function truncates a number to its upper
integer.
4 By default a real number is treated as a
.
A float B double
. .
C long double D far double
. .
Answer: Option B
Explanation:
When the accuracy of the floating point number is insufficient, we can
use the doubleto define the number. The double is same as float but
with longer precision and takes double space (8 bytes) than float.
To extend the precision further we can use long double which
occupies 10 bytes of memory space.
5. Which of the following is a correct comment?
A. */ Comments */
B. ** Comment **
C. /* Comment */
D. { Comment }
Answer: Option C
6. Which of the following is not a correct variable type?
A. float
B. real
C. int
D. double
Answer: Option B
7. Which of the following is the correct operator to
compare two variables?
A. :=
B. =
C. equal
D. ==
Answer: Option D
8. A number such as 45.567 needs to be stored in a variable of
which data type?
A. Int
B. Char
C. Double
D. Real
Answer: Option C
10. The "\n" character does which of the following operations?
A. Double line spacing
B. Character deletion
C. Character backspace
D. Places cursor on the next line
Answer: Option D