ICS 103 Lecture 2 - Introduction To C
ICS 103 Lecture 2 - Introduction To C
Programming in C
Lecture 2
Introduction to C
Most of the slide material are copied from
https://www.tutorialspoint.com/cprogramming/index.h
tm
:Useful link
https://developerinsider.co/c-and-cpp-insider/
2https://www.w3schools.com/c/index.php 07/06/2025
Overview
C is a general-purpose, procedural, imperative
computer programming language developed in 1972
by Dennis M. Ritchie at the Bell Telephone
Laboratories to develop the UNIX operating system.
C was originally first implemented on the DEC PDP-
11 computer in 1972.
It keeps fluctuating at number one scale of
popularity along with Java programming language,
which is also equally popular and most widely used
among modern software programmers.
3
Overview
C has now become a widely used
professional language for various
reasons −
Easy to learn
Structured language
platforms
4
Facts about C
C was invented to write an operating system called UNIX.
C is a successor of B language which was introduced around
the early 1970s.
The language was formalized in 1988 by the American
National Standard Institute (ANSI).
The UNIX OS was totally written in C.
Today C is the most widely used and popular System
Programming Language.
Most of the state-of-the-art software have been implemented
using C.
Today's most popular Linux OS and RDBMS MySQL have
5
been written in C.
Why use C?
C was initially used for system development work, particularly
the programs that make-up the operating system.
Some examples of the use of C might be −
Operating Systems
Language Compilers
Assemblers
Text Editors
Print Spoolers
Network Drivers
Modern Programs
Databases
Language Interpreters
6 Utilities
C - Environment Setup
Text Editor
This will be used to type your program. Examples of few a editors
include Windows Notepad, Notepad++, OS Edit command, Brief,
Epsilon, EMACS, and vim or vi.
The C Compiler
The source code written in source file is the human readable source for
your program. It needs to be "compiled", into machine language so that
your CPU can actually execute the program as per the instructions
given.
The compiler compiles the source codes into final executable
programs.
Installation on Windows
To install GCC on Windows, you need to install MinGW. To install
MinGW, go to the MinGW homepage, www.mingw.org, and follow the
7 link to the MinGW download page.
C - Program Structure
A C program basically consists of the following
parts −
Preprocessor Commands
Functions
Variables
Statements & Expressions
Comments
8
C - Program Structure
1. #include <stdio.h>
2. int main() {
3. /* my first program in C */
4. printf("Hello, World! \n");
5. return 0;
}
2) The next line int main() is the main function where the program
execution begins.
9
C - Program Structure
3) The next line /*...*/ will be ignored by the compiler and it
has been put to add additional comments in the program.
So such lines are called comments in the program.
printf
(
"Hello, World! \n“
)
12 ;
C - Basic Syntax
Semicolons
In a C program, the semicolon is a statement
terminator. That is, each individual statement
must be ended with a semicolon. It indicates the
end of one logical entity.
Given below are two different statements −
13
C - Basic Syntax
Comments
Comments provide supplementary information making
it easier for us to understand the program, but are
ignored by the C compiler.
Two forms of comments:
/* */ - anything between them with be considered a
comment, even if they span multiple lines.
// - anything after this and before the end of the line is
considered a comment.
Comments are used to create Program Documentation
Information that help others read and understand the
program.
14
C - Basic Syntax
Identifiers
A C identifier is a name used to identify a variable, function,
or any other user-defined item.
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
% within identifiers.
C is a case-sensitive programming language. Thus,
Manpower and manpower are two different identifiers in C.
Here are some examples of acceptable identifiers −
mohd zara abc move_name a_123
myname50 _temp j a23b9 retVal
15
C - Basic Syntax
Keywords
The following list shows the reserved words in C.
These reserved words may not be used as constants or
variables or any other identifier names.
auto else long switch
break enum register typedef
case extern return union
char float short unsigned
const for signed void
continue goto sizeof volatile
default if static while
do int struct Packed_
16
C - Basic Syntax
Whitespace in C
A line containing only whitespace, possibly with a
comment, is known as a blank line, and a C compiler
totally ignores it.
Whitespace is the term used in C to describe blanks,
tabs, newline characters and comments.
Whitespace separates one part of a statement from
another and enables the compiler to identify where
one element in a statement, such as int, ends and the
next element begins.
int age;
fruit = apples + oranges; // get the total fruit
17
C - Basic Syntax
White Space Example:
Bad: Good:
int main(void) int main(void)
{ int foo,blah; {
scanf("%d",&foo); int foo, blah;
blah=foo+1; scanf("%d", &foo);
printf("%d", blah); blah = foo + 1;
return 0;} printf("%d", blah);
return 0;
}
18
Bad Programming practices
Missing statement of purpose
Inadequate commenting
Variables names are not meaningful
Use of unnamed constant.
Indentation does not represent program structure
Algorithm is inefficient or difficult to follow
Program does not compile
Program produces incorrect results.
Insufficient testing (e.g. Test case results are different than
expected, program branch never executed, borderline case
not tested etc.)
19
Other Styles Concerns
Properly comment your code
Give variables meaningful names
Prompt the user when you want to input data
Display things in a way that looks good
Insert new lines to make your information
more readable.
Format numbers in a way that makes sense
for the application
20
C - 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.
The types in C can be classified as follows −
Basic Types
They are arithmetic types and are further classified into:
integer types.
floating-point types.
The type void
The type specifier void indicates that no value is available.
Derived types
(a) Pointer types, (b) Array types, (c) Structure types, (d) Union types
and (e) Function types.
Enumerated types
21
C Data Types
22
Integer Types
The following table provides the details of standard integer
types with their storage sizes and value ranges
Type Storage size Value range
char byte 1 to 127 128-
short bytes 2 to 32,767 32,768-
int bytes 4 to 2,147,483,648-
2,147,483,647
long bytes 8 -9×1018 to +9×1018
25
The void Type
Function returns as void
There are various functions in C which do not return any
value or you can say they return void. A function with no
return value has the return type as void. For example, void
exit (int status);
Function arguments as void
There are various functions in C which do not accept any
parameter. A function with no parameter can accept a void.
For example, int rand(void);
Pointers to void
A pointer of type void * represents the address of an object,
but not its type. For example, a memory allocation function
void *malloc( size_t size ); returns a pointer to void which
26
can be casted to any data type.
C - 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
27
case-sensitive.
C - Variables
The following is the basic variable types
Type Description
char Typically a single octet(one byte). This is an
.integer type
int The most natural size of integer for the
.machine
float .A single-precision floating point value
double .A double-precision floating point value
void .Represents the absence of type
28
Variable Definition in C
A variable definition tells the compiler where and how much
storage to create for the variable.
A variable definition specifies a data type and contains a list of one
or more variables of that type as follows −
type variable_list;
type must be a valid C data type including char, w_char, int, float,
double, bool, or any user-defined object; and
variable_list may consist of one or more identifier names separated
by commas.
Some valid declarations are shown here
int i, j, k;
char c, ch;
float f, salary;
29 double d;
Variables Initialization
Variables can be initialized (assigned an initial value) in their
declaration. The initializer consists of an equal sign followed
by a constant expression as follows:
type variable_name = value;
Some examples are −
int d = 3, f = 5; // definition and initializing d and f.
byte z = 22; // definition and initializes z.
char x = 'x'; // the variable x has the value 'x'.
30
C - Constants & Literals
Constants refer to fixed values that the program may
not alter during its execution.
Constants can be of any of the basic data types like
an integer constant, a floating constant, a character
constant, or a string literal.
There are enumeration constants as well.
Constants are treated just like regular variables
except that their values cannot be modified after
their definition.
31
Defining Constants
There are two simple ways in C to define
constants −
Using #define preprocessor.
Using const keyword.
The #define Preprocessor
Given below is the form to use #define
preprocessor to define a constant −
#define identifier value
The next slide example explains it in detail −
32
The #define Preprocessor Example
#include <stdio.h>
#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'
int main() {
int area;
return 0;
}
34
The printf Function
35
Placeholders
Placeholders always begin with the symbol %
% marks the place in a format string where a value
will be printed out or will be read
Format strings can have multiple placeholders, if
you are printing multiple values
Variable
Placeholder Function Use
Type
c% char printf / scanf
d% int printf / scanf
f% double printf
lf% double scanf
36
Displaying Prompts
When input data is needed in an interactive
program, you should use the printf function to
display a prompting message, or prompt, that tells
the user what data to enter.
37
The scanf Function
function Number
name function arguments Entered 30.5
miles
scanf("%lf", &miles);
30.5
place holders input variables
The & is the address operator. It tells scanf the address of
variable miles in memory.
When user inputs a value, it is stored in miles.
The placeholder %lf tells scanf the type of data to store into
variable miles.
38
return Statement
Syntax: return expression ;
Example: return (0);
Returning from the main function terminates the
program and transfers control back to the operating
system. Value returned is 0.
The return statement transfers control from a
function back to the caller.
Once you start writing your own functions, you will
use the return statement to return the result of a
function back to the caller.
39
Lecture Summary
C was originally first implemented on the DEC PDP-
11 computer in 1972.
A variable definition tells the compiler where and
how much storage to create for the variable.
The assignment statement computes the expression
that appears after the assignment operator and
stores its value in the variable that appears to the
left.
Placeholders always begin with the symbol %
40