You are on page 1of 25

NAOE 2214

Computer Programming for Engineers


Lecture 1: Introduction to C Programming
Course Teacher
Md. Tusher Ahmed
Assistant Professor
Department of Mechanical Engineering
Bangladesh University of Engineering and Technology
Course Description
NAOE2214: Computer Programming for Engineers
1.5 Credits- Total Marks: 150
1 Lectures per week (3 Contact Hours)
Schedule: Tuesday (11:00 AM - 1:30 PM)
Course Teacher:
Md. Tusher Ahmed, Assistant Professor, Dept. of ME, BUET
Contact No. : 01766199991
Email address:
tusher_ahmed@me.buet.ac.bd
tusherahmed94@gmail.com
Office: ME511A, 4th Floor , EME Building

NAOE 2214: Computer Programming for


2 Engineers 9/3/2020
Grading (Tentative)
Items Percentage

Attendance 10%

Class Performance/ Observation 10%

Assignment/Report 20%

Viva/Presentation/Mid Quiz 20%

Quiz 40%

Total 100%

Plagiarism is a major academic offence


3 NAOE 2214: Computer Programming for 9/3/2020
Engineers
Required Practices

All of the students are instructed to start their zoom meeting with a name of
his student ID.

For example: Student with St/ID 18411001 must enter the meeting with a
meeting ID of 18411001.
NAOE 2214: Computer Programming for
4 Engineers 9/3/2020
How C Works
• Executing a program written in C involves following steps:

1. Creating the program (Editor)

2. Compiling the program (Compiler)

3. Linking the program with functions that are needed from

the C library (Linker)


4. Executing the program

NAOE 2214: Computer Programming for


5 Engineers 9/3/2020
Program Flow

NAOE 2214: Computer Programming for


6 Engineers 9/3/2020
Compiler(s):
Used for converting Source code into object
code(executable program)

• Code Blocks 17.12 for Windows XP / Vista / 7 / 8.x / 10


• Download Link:
http://www.codeblocks.org/downloads/binaries#windows

• For the peoples who want to run their codes on the go try
the CppDroid app

NAOE 2214: Computer Programming for


7 Engineers 9/3/2020
Downloading Code::Blocks

NAOE 2214: Computer Programming for


8 Engineers 9/3/2020
Basic Structure of A typical C
Program
• Documentation Section
• Link Section
• Definition Section
• Global Declaration Section
• main() Function Section
{
Declaration part
Executable part
}
• Subprogram section

NAOE 2214: Computer Programming for


9 Engineers 9/3/2020
A Simple C Program: Example 1

Header file

Function name #include<stdio.h>


void main(void) Semicolon
{ terminates
printf (“Hello, world”) ; each program
statement
Opening curly brace }
to delimit body
of the function One statement

Closing curly This entire program


brace Consists of a function
to delimit body called main()
of the function

NAOE 2214: Computer Programming for


10 Engineers 9/3/2020
Keywords and Identifiers
Keywords
are predefined, reserved words used in programming that have
special meanings to the compiler. Keywords are part of the syntax
and they cannot be used as an identifier.
For example: void, main etc.;

Identifier
refers to name given to entities such as variables, functions,
structures etc. Identifiers must be unique. They are created to give
a unique name to an entity to identify it during the execution of the
program.

NAOE 2214: Computer Programming for


11 Engineers 9/3/2020
Variables and Data types
Variables
In programming, a variable is a container (storage area) to hold
data. To indicate the storage area, each variable should be given a
unique name (identifier). Variable names are just the symbolic
representation of a memory location.

Data types
In C programming, data types are declarations for variables. This
determines the type and size of data associated with variables.

Three basic data types are


i. int
ii. float
iii. char

NAOE 2214: Computer Programming for


12 Engineers 9/3/2020
Rules for Variable Declaration
• Consists of letters and digits, in any order
• 1st character must be letter or underscore, after that you
can use numbers.
• ( _ ) can be considered as a letter
• Both upper- and lowercase are permitted(Case sensitive)
• Keywords are not allowed
(int,char,float,if ,else,void,while signed,const,break,do,return
etc.)
• C recognizes only the 1st 31 characters
_0123456789012345678901234567890
_0123456789012345678901234567890
they are duplicate

NAOE 2214: Computer Programming for


13 Engineers 9/3/2020
Variable declaration
14

 General form
type variable-name;

 Example:

int year;
float weight;
char grade;

NAOE 2214: Computer


9/3/2020
Programming for Engineers
Test: Variable name
15

First_tag char Price$


Valid Not Valid
Valid? Valid Not valid
Keyword Valid?$
Illegal
Not Valid? Not valid?
sign
Not Valid?

group one int_type


Not valid Valid
Valid?
No Blank Valid?
Keyword, a part of
Not Valid?
space name
Not
valid?

NAOE 2214: Computer


9/3/2020
Programming for Engineers
C output: printf() function
a useful function from the standard library of functions
that are accessible by C programs

Statement output

printf(“String”);

Example # 2
Write a program that will display the following line
“The use of Mobile phones or pen drives is strictly
prohibited during the class time”
TIME: 2 MINUTES

NAOE 2214: Computer Programming for


16 Engineers 9/3/2020
C output: printf() function
Variable/Constant output
printf(“Format string”, variables);

printf(“ %s is %d million miles away from the sun.”,“Venus”, 67);

 Thevariables/constants on the right are plugged in


according to the Format Specifiers in the string on the left

• The resulting string is displayed on the monitor

NAOE 2214: Computer Programming for


17 Engineers 9/3/2020
Example # 3a
# include <stdio.h>
void main(void)
{
printf(“ %s is %d million miles away from the sun.”, “Venus”, 67);
}

NAOE 2214: Computer Programming for


18 Engineers 9/3/2020
Example # 3b

# include <stdio.h>
void main(void)
{
printf(“ %s is %d million miles away”, “Venus”, 67);
printf(“from the sun.”, “Venus”, 67);
}

What is the difference between the two codes?

NAOE 2214: Computer Programming for


19 Engineers 9/3/2020
Example # 3c

# include <stdio.h>
void main(void)
{
printf(“ %s is %d million miles away \n from the sun.”, “Venus”,
67);
}

NAOE 2214: Computer Programming for


20 Engineers 9/3/2020
Escape Characters

NAOE 2214: Computer Programming for


21 Engineers 9/3/2020
Example # 4
# include <stdio.h>

void main(void)
{
int event = 5;
char heat = „A‟;
float time = 27.25;

printf (“ \n The winning time in heat %c ”, heat) ;


printf (“ of event %d was %f.” , event, time);
}

NAOE 2214: Computer Programming for


22 Engineers 9/3/2020
Format specifiers
%d Integer Signed decimal integer
%i Integer Signed decimal integer
%o Integer Unsigned octal integer
%u Integer Unsigned decimal integer
%x Integer Unsigned hexadecimal int (with a, b, c, d, e, f)
%X Integer Unsigned hexadecimal int (with A, B, C, D, E, F)
%f Floating point Signed value of the form [-]dddd.dddd.
%e Floating point Signed value of the form [-]d.dddd or e[+/-]ddd
%g Floating point Signed value in either e or f form, based on
given value and precision. Trailing zeros and
the decimal point are printed if necessary.
%E Floating point Same as e; with E for exponent.
%G Floating point Same as g; with E for exponent if e format used
%c Character Single character
%s String pointer Prints characters until a null-terminator is
pressed or precision is reached
%% None Prints the % character

NAOE 2214: Computer Programming for


23 Engineers 9/3/2020
Format modifiers
24

Output of Integer Numbers % wd

Format Output
printf(“%d”, 9876); 9 8 7 6
printf(“%6d”, 9876); 0 0 9 8 7 6
printf(“%2d”, 9876); 9 8 7 6
printf(“%-6d”, 9876); 9 8 7 6
printf(“%06d”, 9876); 0 0 9 8 7 6

NAOE 2214: Computer


9/3/2020
Programming for Engineers
Format modifiers
Output of Real Numbers % w.p f % w.p e
Format (y = 98.7654) Output
printf(“%7.4f”, y); 9 8 . 7 6 5 4
printf(“%7.2f”, y); 9 8 . 7 7
printf(“%-7.2f”, y); 9 8 . 7 7
printf(“%f”, y); 9 8 . 7 6 5 4
printf(“%10.2e”, y); 9 . 8 8 e + 0 1
printf(“%11.4e”, -y); - 9 . 8 7 6 5 e + 0 1
printf(“%-10.2e”, y); 9 . 8 8 e + 0 1
printf(“%e”, y); 9 . 8 7 6 5 4 0 e + 0 1
9/3/2020 NAOE 2214: Computer
25
Programming for Engineers

You might also like