You are on page 1of 75

Department of Electronics and Communication Engineering

National Institute of Technology, Calicut

ZZ1004D : Computer Programming


Lecture 3

Abdulkareem V
vabdulkareemv@gmail.com

August 10, 2018


Content
1

Recall from last class

Compilation Steps

Steps for doing coding

C Program - Example

Comments in C

Escape Sequences

Data Types & Variables


Variable Names

Abdulkareem V | C Programming - Lecture 3


Recall from last class
2

I High Level language


I Compiler and Interpreter
I Steps to solve a problem
I Compilation Steps
I Simple C Program

Abdulkareem V | C Programming - Lecture 3


Compilation Steps
3

Abdulkareem V | C Programming - Lecture 3


Steps for doing coding
4

I Login to the account ec ( Password : ec@ccc)


I Make a directory with your own roll number : Right click on the
mouse, select Create New Folder option and put your roll
number as the name of the directory
I Please make sure that you place all your C programs inside that
directory, in different folders like Assignment1, Assignment2 etc.,
so that it is easy for you and your evaluators to refer.
I Make sure that you write your file name as assignment1_Q1.c
(a1_q1.c or a1_q1a.c,a1_q1b.c, a2_q1a.c etc) , so that it is easy
for you and your evaluators to refer.

Abdulkareem V | C Programming - Lecture 3


Step 1
5

Abdulkareem V | C Programming - Lecture 3


Step 2
6

Abdulkareem V | C Programming - Lecture 3


Step 3
7

Abdulkareem V | C Programming - Lecture 3


Screenshot - Terminal
8

Abdulkareem V | C Programming - Lecture 3


Steps for coding in lab
9

I Open terminal : Press Ctrl, Alt, T (all three keys together)

Abdulkareem V | C Programming - Lecture 3


Steps for coding in lab
9

I Open terminal : Press Ctrl, Alt, T (all three keys together)


I Change to your directory : cd Desktop/Your directory name

Abdulkareem V | C Programming - Lecture 3


Steps for coding in lab
9

I Open terminal : Press Ctrl, Alt, T (all three keys together)


I Change to your directory : cd Desktop/Your directory name
I Open an editor with file name hello.c: gedit hello.c

Abdulkareem V | C Programming - Lecture 3


Steps for coding in lab
9

I Open terminal : Press Ctrl, Alt, T (all three keys together)


I Change to your directory : cd Desktop/Your directory name
I Open an editor with file name hello.c: gedit hello.c
I Write your program in the window opened and save it. Then
close the editor window.

Abdulkareem V | C Programming - Lecture 3


Steps for coding in lab
9

I Open terminal : Press Ctrl, Alt, T (all three keys together)


I Change to your directory : cd Desktop/Your directory name
I Open an editor with file name hello.c: gedit hello.c
I Write your program in the window opened and save it. Then
close the editor window.
I Compile your program : gcc hello.c

Abdulkareem V | C Programming - Lecture 3


Steps for coding in lab
9

I Open terminal : Press Ctrl, Alt, T (all three keys together)


I Change to your directory : cd Desktop/Your directory name
I Open an editor with file name hello.c: gedit hello.c
I Write your program in the window opened and save it. Then
close the editor window.
I Compile your program : gcc hello.c
I An executable file is created with the name a.out

Abdulkareem V | C Programming - Lecture 3


Steps for coding in lab
9

I Open terminal : Press Ctrl, Alt, T (all three keys together)


I Change to your directory : cd Desktop/Your directory name
I Open an editor with file name hello.c: gedit hello.c
I Write your program in the window opened and save it. Then
close the editor window.
I Compile your program : gcc hello.c
I An executable file is created with the name a.out
I Execute your program : ./a.out

Abdulkareem V | C Programming - Lecture 3


Steps for coding in lab
9

I Open terminal : Press Ctrl, Alt, T (all three keys together)


I Change to your directory : cd Desktop/Your directory name
I Open an editor with file name hello.c: gedit hello.c
I Write your program in the window opened and save it. Then
close the editor window.
I Compile your program : gcc hello.c
I An executable file is created with the name a.out
I Execute your program : ./a.out
I Instead of step 1 & 2 : Right click on your folder and select open
in terminal.

Abdulkareem V | C Programming - Lecture 3


Steps for coding in lab
9

I Open terminal : Press Ctrl, Alt, T (all three keys together)


I Change to your directory : cd Desktop/Your directory name
I Open an editor with file name hello.c: gedit hello.c
I Write your program in the window opened and save it. Then
close the editor window.
I Compile your program : gcc hello.c
I An executable file is created with the name a.out
I Execute your program : ./a.out
I Instead of step 1 & 2 : Right click on your folder and select open
in terminal.
I We can specify the name name of executable file (default is
a.out) at the time of compilation : eg. gcc -o hello hello.c

Abdulkareem V | C Programming - Lecture 3


A simple C program
10

#include < s t d i o . h>


int main ( )
{
printf ( "Welcome to C\n" ) ;
return 0 ;
}

Abdulkareem V | C Programming - Lecture 3


A simple C program
11

Abdulkareem V | C Programming - Lecture 3


C program - Basics
12

Abdulkareem V | C Programming - Lecture 3


C program - Basics
12

I Every C Program Should have exactly one main function

Abdulkareem V | C Programming - Lecture 3


C program - Basics
12

I Every C Program Should have exactly one main function


I C Program Execution Always Starts from main

Abdulkareem V | C Programming - Lecture 3


C program - Basics
12

I Every C Program Should have exactly one main function


I C Program Execution Always Starts from main
I Execution of C Program begins at Opening brace of function
and ends at return statement or closing brace of the function

Abdulkareem V | C Programming - Lecture 3


C program - Basics
12

I Every C Program Should have exactly one main function


I C Program Execution Always Starts from main
I Execution of C Program begins at Opening brace of function
and ends at return statement or closing brace of the function
I Every C statement must ends with semicolon

Abdulkareem V | C Programming - Lecture 3


C program - Basics
12

I Every C Program Should have exactly one main function


I C Program Execution Always Starts from main
I Execution of C Program begins at Opening brace of function
and ends at return statement or closing brace of the function
I Every C statement must ends with semicolon
I All variables must be declared with respective data types
before using

Abdulkareem V | C Programming - Lecture 3


C program - Basics
12

I Every C Program Should have exactly one main function


I C Program Execution Always Starts from main
I Execution of C Program begins at Opening brace of function
and ends at return statement or closing brace of the function
I Every C statement must ends with semicolon
I All variables must be declared with respective data types
before using
I Comments can be inserted anywhere in C Program

Abdulkareem V | C Programming - Lecture 3


C program - Basics
12

I Every C Program Should have exactly one main function


I C Program Execution Always Starts from main
I Execution of C Program begins at Opening brace of function
and ends at return statement or closing brace of the function
I Every C statement must ends with semicolon
I All variables must be declared with respective data types
before using
I Comments can be inserted anywhere in C Program
I Braces are generally used for the grouping of statements

Abdulkareem V | C Programming - Lecture 3


Comments in C
Single Line Comment
13

Abdulkareem V | C Programming - Lecture 3


Comments in C
Single Line Comment
13

I Single Line Comment Can be Placed Anywhere

Abdulkareem V | C Programming - Lecture 3


Comments in C
Single Line Comment
13

I Single Line Comment Can be Placed Anywhere


I Single Line Comment Starts with //

Abdulkareem V | C Programming - Lecture 3


Comments in C
Single Line Comment
13

I Single Line Comment Can be Placed Anywhere


I Single Line Comment Starts with //
I Any Symbols written after // are ignored by Compiler

Abdulkareem V | C Programming - Lecture 3


Comments in C
Single Line Comment
13

I Single Line Comment Can be Placed Anywhere


I Single Line Comment Starts with //
I Any Symbols written after // are ignored by Compiler
#include < s t d i o . h>
// Author : Abdulkareem
int main ( )
{
printf ( "Welcome to C\n" ) ; // To print the line
return 0 ;
}

Abdulkareem V | C Programming - Lecture 3


Comments in C
Multi Line Comment 14

I Multi Line Comment Can be Placed Anywhere

Abdulkareem V | C Programming - Lecture 3


Comments in C
Multi Line Comment 14

I Multi Line Comment Can be Placed Anywhere


I Multi Line Comment Starts with /* and ends with */

Abdulkareem V | C Programming - Lecture 3


Comments in C
Multi Line Comment 14

I Multi Line Comment Can be Placed Anywhere


I Multi Line Comment Starts with /* and ends with */
I Any Symbols written between /* and */ are ignored by Compiler
I Nested comments are not supported by C

Abdulkareem V | C Programming - Lecture 3


Comments in C
Multi Line Comment 14

I Multi Line Comment Can be Placed Anywhere


I Multi Line Comment Starts with /* and ends with */
I Any Symbols written between /* and */ are ignored by Compiler
I Nested comments are not supported by C
#include < s t d i o . h>
/* Author : Abdulkareem
C Program to print a sentence */
int main ( )
{
printf ( "Welcome to C\n" ) ;
return 0 ;
}

Abdulkareem V | C Programming - Lecture 3


Escape Sequences
15

Abdulkareem V | C Programming - Lecture 3


Escape Sequences
15

I For using characters which cannot be typed or has special


meaning in C programming
I For example: newline(enter), tab, question mark etc.
I In order to use these characters, escape sequence is used

Abdulkareem V | C Programming - Lecture 3


Escape Sequences
15

I For using characters which cannot be typed or has special


meaning in C programming
I For example: newline(enter), tab, question mark etc.
I In order to use these characters, escape sequence is used
I For example: \n is used for newline.

Abdulkareem V | C Programming - Lecture 3


Escape Sequences
15

I For using characters which cannot be typed or has special


meaning in C programming
I For example: newline(enter), tab, question mark etc.
I In order to use these characters, escape sequence is used
I For example: \n is used for newline.
I The backslash ( \) causes "escape" from the normal way the
characters are interpreted by the compiler
\a alert(bell) character \b backspace
\f formfeed \n newline
\r carriage return \t horizontal tab
\v vertical tab \\ backslash
\? question mark \’ single quote
\" double quote \ooo octal number
\xhh hexa decimal number

Abdulkareem V | C Programming - Lecture 3


Sum of two Numbers
16

I Steps to solve the problem

Abdulkareem V | C Programming - Lecture 3


Sum of two Numbers
16

I Steps to solve the problem


I Take two numbers

Abdulkareem V | C Programming - Lecture 3


Sum of two Numbers
16

I Steps to solve the problem


I Take two numbers
I Add the numbers

Abdulkareem V | C Programming - Lecture 3


Sum of two Numbers
16

I Steps to solve the problem


I Take two numbers
I Add the numbers
I Print the result

Abdulkareem V | C Programming - Lecture 3


Sum of two Numbers
16

I Steps to solve the problem


I Take two numbers
I Add the numbers
I Print the result
I How do we give/specify two numbers ?

Abdulkareem V | C Programming - Lecture 3


Sum of two Numbers
16

I Steps to solve the problem


I Take two numbers
I Add the numbers
I Print the result
I How do we give/specify two numbers ?
I There are two ways
1. Give the numbers in the program itself
2. Get the numbers as input from the user

Abdulkareem V | C Programming - Lecture 3


Sum of two Numbers
16

I Steps to solve the problem


I Take two numbers
I Add the numbers
I Print the result
I How do we give/specify two numbers ?
I There are two ways
1. Give the numbers in the program itself
2. Get the numbers as input from the user
I How does the compiler identifies between a number and a
statement like "Welcome to C"

Abdulkareem V | C Programming - Lecture 3


Sum of two Numbers
16

I Steps to solve the problem


I Take two numbers
I Add the numbers
I Print the result
I How do we give/specify two numbers ?
I There are two ways
1. Give the numbers in the program itself
2. Get the numbers as input from the user
I How does the compiler identifies between a number and a
statement like "Welcome to C"
I We have to specify the type of the number

Abdulkareem V | C Programming - Lecture 3


Sum of two Numbers
16

I Steps to solve the problem


I Take two numbers
I Add the numbers
I Print the result
I How do we give/specify two numbers ?
I There are two ways
1. Give the numbers in the program itself
2. Get the numbers as input from the user
I How does the compiler identifies between a number and a
statement like "Welcome to C"
I We have to specify the type of the number
I How many types of numbers are there?

Abdulkareem V | C Programming - Lecture 3


Sum of two Numbers
16

I Steps to solve the problem


I Take two numbers
I Add the numbers
I Print the result
I How do we give/specify two numbers ?
I There are two ways
1. Give the numbers in the program itself
2. Get the numbers as input from the user
I How does the compiler identifies between a number and a
statement like "Welcome to C"
I We have to specify the type of the number
I How many types of numbers are there?
I Numbers with out decimal points (integers) and numbers with
decimal points (floating point numbers)

Abdulkareem V | C Programming - Lecture 3


Sum of two Numbers
17

Abdulkareem V | C Programming - Lecture 3


Sum of two Numbers
17

I How do we tell the computer that our number is an integer?

Abdulkareem V | C Programming - Lecture 3


Sum of two Numbers
17

I How do we tell the computer that our number is an integer?


I Declare the number as an integer data type

Abdulkareem V | C Programming - Lecture 3


Sum of two Numbers
17

I How do we tell the computer that our number is an integer?


I Declare the number as an integer data type
I eg. int a; - a is a variable declared as integer data type

Abdulkareem V | C Programming - Lecture 3


Sum of two Numbers
17

I How do we tell the computer that our number is an integer?


I Declare the number as an integer data type
I eg. int a; - a is a variable declared as integer data type

#include < s t d i o . h>


int main ( )
{
int x , y , z ; // declaration of three variables as integers

return 0 ;
}

Abdulkareem V | C Programming - Lecture 3


Sum of two Numbers
18

I How do we tell the computer that our number is an integer?


I Declare the number as an integer data type
I eg. int a; - a is a variable declared as integer data type

#include < s t d i o . h>


int main ( )
{
int x , y , z ; // declaration of three variables as integers
x=12 ; // initilaization of x
y=4 ; // initilaization of y

return 0 ;
}

Abdulkareem V | C Programming - Lecture 3


Sum of two Numbers
19

I How do we tell the computer that our number is an integer?


I Declare the number as an integer data type
I eg. int a; - a is a variable declared as integer data type

#include < s t d i o . h>


int main ( )
{
int x , y , z ; // declaration of three variables as integers
x=12 ; // initilaization of x
y=4 ; // initilaization of y
z=x+y ; // Finding sum

return 0 ;
}

Abdulkareem V | C Programming - Lecture 3


Sum of two Numbers
20

I How do we tell the computer that our number is an integer?


I Declare the number as an integer data type
I eg. int a; - a is a variable declared as integer data type

#include < s t d i o . h>


int main ( )
{
int x , y , z ; // declaration of three variables as integers
x=12 ; // initilaization of x
y=4 ; // initilaization of y
z=x+y ; // Finding sum
printf ( "Sum of two numbers is %d" , z ) ;

return 0 ;
}

Abdulkareem V | C Programming - Lecture 3


Sum of two Numbers
21

I How do we tell the computer that our number is an integer?


I Declare the number as an integer data type
I eg. int a; - a is a variable declared as integer data type

#include < s t d i o . h>


int main ( )
{
int x , y , z ; // declaration of three variables as integers
x=12 ; // initilaization of x
y=4 ; // initilaization of y
z=x+y ; // Finding sum
printf ( "Sum of two numbers is %d" , z ) ;
/* %d or %i is the format specifier for an integer
data type. %d is associated with the variable
after quotes (eg: z in this case) */
return 0 ;
}

Abdulkareem V | C Programming - Lecture 3


Variable Names
22

Abdulkareem V | C Programming - Lecture 3


Variable Names
22

I Variable names are made up of letters and digits.


I Underscore(_) counts as letter.

Abdulkareem V | C Programming - Lecture 3


Variable Names
22

I Variable names are made up of letters and digits.


I Underscore(_) counts as letter.
I The first character must be a letter.

Abdulkareem V | C Programming - Lecture 3


Variable Names
22

I Variable names are made up of letters and digits.


I Underscore(_) counts as letter.
I The first character must be a letter.
I So we can begin variable names with underscore, but it is better
to avoid, since library routines often use such names.

Abdulkareem V | C Programming - Lecture 3


Variable Names
22

I Variable names are made up of letters and digits.


I Underscore(_) counts as letter.
I The first character must be a letter.
I So we can begin variable names with underscore, but it is better
to avoid, since library routines often use such names.
I Uppercase and lower case letters are distinct.
I Traditional approach is use lowercase for variables and
uppercase for constants.

Abdulkareem V | C Programming - Lecture 3


Variable Names
22

I Variable names are made up of letters and digits.


I Underscore(_) counts as letter.
I The first character must be a letter.
I So we can begin variable names with underscore, but it is better
to avoid, since library routines often use such names.
I Uppercase and lower case letters are distinct.
I Traditional approach is use lowercase for variables and
uppercase for constants.
I Keywords like if, else, for, int, char, ... are reserved, so we can’t
use them as variable names.

Abdulkareem V | C Programming - Lecture 3


Variable Names
Example
23

I Tell whether the following variables names are valid or not


I int name50;
I int 2nd;
I int _area;
I int a_1;
I int char;
I int CHAR;
I int var 1;
I int name.1;

Abdulkareem V | C Programming - Lecture 3


Variable Names
Example
24

I Tell whether the following variables names are valid or not


I int name50;
I int 2nd;
I int _area;
I int a_1;
I int char;
I int CHAR;
I int var 1;
I int name.1;

Abdulkareem V | C Programming - Lecture 3


Area and Circumference of a circle
25

Write a program to find area and circumference of a circle.


#include < s t d i o . h>
int main ( )
{
float rad , area , circ ;

return 0 ;
}

Abdulkareem V | C Programming - Lecture 3


Area and Circumference of a circle
26

Write a program to find area and circumference of a circle.


#include < s t d i o . h>
int main ( )
{
float rad , area , circ ;
rad=2 . 5 ;

return 0 ;
}

Abdulkareem V | C Programming - Lecture 3


Area and Circumference of a circle
27

Write a program to find area and circumference of a circle.


#include < s t d i o . h>
int main ( )
{
float rad , area , circ ;
rad=2 . 5 ;
area=3 . 14∗rad∗rad ;
circ=2∗3 . 14∗rad ;

return 0 ;
}

Abdulkareem V | C Programming - Lecture 3


Area and Circumference of a circle
28

Write a program to find area and circumference of a circle.


#include < s t d i o . h>
int main ( )
{
float rad , area , circ ;
rad=2 . 5 ;
area=3 . 14∗rad∗rad ;
circ=2∗3 . 14∗rad ;
printf ( "\n Area of the circle is %f" , area ) ;
printf ( "\n Circumference of the circle is %f" , circ ) ;
return 0 ;
}

Abdulkareem V | C Programming - Lecture 3


Area and Circumference of a circle
Radius as input from user
29

Write a program to find area and circumference of a circle.


To read input through keyboard, we can use scanf function available
in stdio.h
#include < s t d i o . h>
int main ( )
{
float rad , area , circ ;
printf ( "Enter the value of radius : " ) ;
// To inform user what to do
scanf ( "%f" ,& rad ) ;
// To read the value entered through keyboard
area=3 . 14∗rad∗rad ;
circ=2∗3 . 14∗rad ;
printf ( "\n Area of the circle is %f" , area ) ;
printf ( "\n Circumference of the circle is %f" , circ ) ;
return 0 ;
}

Abdulkareem V | C Programming - Lecture 3


Home Work
30

I Write a program to read three numbers from user and print the
average.

Abdulkareem V | C Programming - Lecture 3


Thank you!

You might also like