Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Introduction to

C Programming
Mahit Kumar Paul
Assistant Professor, Dept. of CSE
RUET, Rajshahi-6204
mahit.cse@gmail.com
mahit@cse.ruet.ac.bd

2023-10-05 1
Course Introduction
Couse No.: CSE 1111
Course Title: Computer Programming
Contact Hours/Week: 3
Credits: 3.00

Couse No.: CSE 1112


Course Title: Computer Programming Sessional
Contact Hours/Week: 3
Credits: 1.50
2023-10-05 2
Book Reference

1. Teach Yourself C - Herbert Schildt

2. Programming With C - Byron S. Gottfried

3. Programming in ANSI C – E Balagurusamy

Web-Link: https://www.tutorialspoint.com/cprogramming/

2023-10-05 3
What is C ?
• C is a general purpose, high level Computer
Programming Language.
• It can be used to create lists of instructions for a
computer to follow.
• It has been designed by Dennis M. Ritchie at Bell
Laboratories in the early 1970s to develop the UNIX
operating system.
• The UNIX operating system, the C compiler, all
UNIX application programs, Linux OS and RDBMS
MySQL have been written in C.

2023-10-05 4
Sample C Program
C program to add two integer numbers:
Source Code:

2023-10-05 5
Sample C Program…
Output:

Summation is: 15

2023-10-05 6
Sample C Program with comments

2023-10-05 7
Sample C Program…
Output:

Summation is: 15

2023-10-05 8
Structure of C Program…
C program basically consists of the following parts:

• Preprocessor Commands

• Functions

• Variables

• Statements & Expressions

• Comments

2023-10-05 9
Structure of C Program…

preprocessor

main() function

variable

expression

function

comment
2023-10-05 10
Preprocessor…
• Header files (*.h) export interface definitions:
 Function prototypes, data types, macros, inline
functions and other common declarations.

• #include: directs the preprocessor to “include” the


contents of the file at this point in the source file.

• #define: directs preprocessor to define macros.

• #include <stdio.h> is a directive to the C preprocessor.

2023-10-05 12
Preprocessor…
• Standard Headers you should know about:

– stdio.h – file and console (also a file) IO: printf, open,


close, read, write, scanf, etc.

– stdlib.h - common utility functions: malloc, calloc,


strtol, atoi, etc

– string.h - string and byte manipulation: strlen, strcpy,


strcat, memcpy, memset, etc.

– math.h – math functions: ceil, exp, floor, sqrt, etc.

– time.h – time related facility: asctime, clock, time_t,


etc.
2023-10-05 13
• main() : Program execution begins here.

• Variables : A variable is nothing but a name given to a


storage area that our programs can manipulate.

• Expression : an expression is any legal combination of


symbols that represents a value. E.g. z = x + y;

• Statements : A statement is a command given to the


computer that instructs the computer to take a specific
action. E.g. if ( expression ) statement;

• Comments: Statements ignored by the compiler.


2023-10-05 14
C-Basic Syntax
Identifiers

• C identifier is a name used to identify a variable,


function, or any other user defined item.

• Identifier Rules: An identifier starts with a letter A to


Z, a to z, or an underscore '_' followed by zero or
more letters, underscores, and digits (0 to 9).

• C does not allow punctuation characters, such as @,


%, and so on within identifiers.

• C is a Case-sensitive programming language. Thus,


Sum and sum are two different identifiers in C.
2023-10-05 15
C-Basic Syntax
Examples of valid identifiers
mohd
zara
abc
move_name
a_123
myname50 _temp
j
a23b9
retVal

2023-10-05 16
C-Basic Syntax
Keywords

• Reserved words in C.

• These reserved words can not be used as constants or


variables or any other identifier names.

2023-10-05 17
C-Basic Syntax
Some Keywords reserved in C

2023-10-05 18
C-Basic Syntax
Comments

• Statements ignored by the compiler.

• They start with /* and terminate with the characters */


for block of lines.

• Preceded by // (double backward slash) for


commenting a single line.

2023-10-05 19
C-Basic Syntax
Comments Example

Block of lines

Single line

2023-10-05 20
C-Basic Syntax
Semicolon
• In a C program, the Semicolon is a
statement/expression terminator.

• That is, each individual statement/expression must be


ended with a semicolon.

• It indicates the end of one logical entity.

2023-10-05 21
C-Basic Syntax
Semicolon Example

Semicolon

2023-10-05 22
C-Basic Syntax
Data Types

• Data types in c refer to an extensive system used for


declaring variables or functions of different types.

• The type of a variable determines how much space it


occupies in storage and how the bit pattern stored is
interpreted.

2023-10-05 24
C-Basic Syntax
Various Data Types

• integer type: 2 or 4 bytes.


• char type: 1 byte
• float type: 4 bytes
• double type: 8 bytes
• void type: Size of void is platform dependent. It
could be 2,4,8 bytes etc.

2023-10-05 25
C-Basic Syntax
void Data Type
• The void type specifies that no value is available. It is used in three kinds of
situations-

2023-10-05 26
C-Basic Syntax
Other Data Types

• Pointer types.
• Array types
• Structure types
• Union types and
• Function types

Note: (Will be discussed on upcoming classes)


2023-10-05 27
C-Basic Syntax
Variables
• A variable is nothing but a name given to a storage area
that our programs can manipulate.

• Each variable in C has a specific type, which


determines:
o the size and layout of the variable's memory;
o the range of values that can be stored within that memory;
o the set of operations that can be applied to the variable.

Rules of Variable Declaration: Same as Identifier


declaration.
2023-10-05 28
C-Basic Syntax
Variables can be of the following types

• char
• integer
• float
• double
• void

2023-10-05 29
C-Basic Syntax
Variables Definition
• A variable definition specifies a data type and contains
a list of one or more variables of that type as follows:

type variable_list;

• Examples:

2023-10-05 30
2023-10-05 31

You might also like