Anda di halaman 1dari 34

CS1P001 : Programming and Data Structures Lab

Lab1: Introduction

Joy Mukherjee

School of Electrical Sciences


Computer Science and Engineering
Indian Institute of Technology Bhubaneswar

1 / 34
PDS Time Table

Venue: 1st Year Laboratory Complex

Day PDS Room No. Time


Monday PDS Lab (Gr A) 104 1:30 P.M. - 4:25 P.M.
Tuesday PDS Tutorial 201, 202 10:00 A.M. - 10:55 A.M.
Friday PDS Lab (Gr B) 104 1:30 P.M. - 4:25 P.M.
Table: B.Tech. 1st Semester (Section II) for 2016-17 Autumn Semester

2 / 34
Evaluation Procedure in the PDS Laboratory

Examination % of Marks Date & Time


Attendance 20%
Regular Performance 40%
Class Test 40% TBD
Table: Evaluation: B.Tech. 1st Year (Section II) for 2016-17 Autumn Semester

Regular Attendence is mandatory.


Any student with less than 90% attendance may be deregistered from
the course.
Leave due to medical reasons must be certified by the Hospital.

3 / 34
Outline

1 Why teach C?

2 Write-Compile-Execute

3 C Programs

4 Variables in C

5 More C Programs

4 / 34
Outline

1 Why teach C?

2 Write-Compile-Execute

3 C Programs

4 Variables in C

5 More C Programs

5 / 34
Why teach C?

C is small (only 32 keywords).


C is stable (the language doesnt change much).
C is quick running.
C is the basis for many other languages (Java, C++, awk, Perl).
C is one of the easiest languages to learn.

6 / 34
Outline

1 Why teach C?

2 Write-Compile-Execute

3 C Programs

4 Variables in C

5 More C Programs

7 / 34
Write-Compile-Execute

Problem solving with computers involves four steps:


1 Write the program in a high-level language like C using a text editor.
2 Save the program.
3 Compile your program using a C compiler (cc or gcc).
gcc -Wall myprog.c
If your program compiles successfully, a file named a.out (an
abbreviation of assembler output) is created. This file stores the
machine instructions that can be understood by the particular
computer. If compilation fails, you should check your source code for
syntax errors.
4 Run the machine executable file by typing
./a.out
and then hitting the enter button at the command prompt.

8 / 34
Outline

1 Why teach C?

2 Write-Compile-Execute

3 C Programs

4 Variables in C

5 More C Programs

9 / 34
First.c
// Preprocessor Directive (found in /usr/include)
#include <stdio.h>
// Start Point of any C program
int main ()
{
// A library function declared in stdio.h
printf(Hello world!\n);
// Successful completion of program (0) returned to opeating system
return 0;
}
1 Open a text editor e.g. gedit (Search for Text Editor)
2 Write (and not just copy and paste) a C program in the text editor.
3 Save the program as First.c (Ctrl + S).
4 Open a terminal (or console) to compile or run the C program.
5 To Compile: type gcc -Wall First.c in the terminal. Press Enter key.
6 To Run: type ./a.out in the terminal. Press Enter key.
10 / 34
Add.c
#include <stdio.h>
// Function Declaration or Function Prototype
int add(int , int );
int main()
{
int a, b, c;
scanf(%d%d,&a, &b);
// Function call with actual parameters or actual arguments
c = add(a, b);
printf(%d\n,c);
return 0;
}
// Function Definition or Function Implementation with formal parameters or
formal arguments
int add(int a, int b)
{
return (a+b);
}
11 / 34
ReadWrite.c

#include <stdio.h>
int main()
{
int ii;
float ff;
double dd;
char cc;
scanf(%d%f%lf%c,&ii, &ff, &dd, &cc);
printf(ii = %d sizeof(int) = %d\n, ii, sizeof(int));
printf(ff = %f sizeof(float) = %d\n, ff, sizeof(float));
printf(dd = %lf sizeof(double) = %d\n, dd, sizeof(double));
printf(cc = %c sizeof(char) = %d\n, cc, sizeof(char));
return 0;
}

12 / 34
Some Programmer Jargons
Language: The core part of C central to writing C code such as
keyword, syntactic construction.
Source code: The C program you type through your keybord.
Object code: Taking source code and making a program that the
computer can understand (object code). Object code is the input to
the linker.
Library: Added functions for C programming which are used to do
certain tasks.
Header file: Files ending in .h which are included at the start of
source code.
Linker: A program that links separately compiled modules into one
program. It also combines the functions in the Standard C library
with the code that you wrote. The output of the linker is an
executable program.
Executable: The compiled program that the computer can run.
Compile time: The time during which your program is being compiled.
Run time: The time during which your program is executing.
13 / 34
HLL Executable

High Level Language (HLL) Assembler Assembly Code


Assembly Code Compiler Object Code
Object Code + Libraries Linker Executable Code

14 / 34
The C Character Set

Alphabets {A, . . . , Z}, {a, . . . , z}


Digits {0, . . . , 9}
Special symbols { # & | = ! < > + - * / \ % : ? , . ; ( ) {
} [ ] blank space}

15 / 34
32 Keywords in C

Keywords are reserved words whose meaning has already been explained to
the C compiler. The keywords and C library functions cannot be used as
variables.
Basic Datatypes (5) - int, float, double, char, void
Type Modifiers (4) - signed, unsigned, long, short
Storage classes (4) - auto, register, static, extern
Type Qualifiers (2) - const, volatile
Flow Control (6) - if, else, return, switch, case, default
Loops (5) - for, do, while, break, continue
Structures (3) - struct, typedef, union
Counting and sizing things (2) - enum, sizeof
Evil keywords which we avoid (1) - goto

16 / 34
Do not Afraid Kids

Basic Datatypes (5) - int, float, double, char, void


Counting and sizing things (1) - sizeof
Flow Control (3) - if, else, return
Loops (3) - for, do, while.

17 / 34
Outline

1 Why teach C?

2 Write-Compile-Execute

3 C Programs

4 Variables in C

5 More C Programs

18 / 34
Variables in C

A variable is a named location in memory that is used to hold a value that


can be modified by the program.

int i;

19 / 34
Name of a Variable

First character must be an alphabet, or an underscore ( ).


From second character onwards, each character may be an alphabet,
or an underscore ( ), or a digit.
C is case-sensitive: C can distinguish uppercase and lowercase
alphabets.

20 / 34
Variable Declaration

All variables must be declared before they can be used. For declaring one
or more variables of a given data type do the following:
1 First write the data type of the variable.
2 Then put a space (or any other white character).
3 Then write your comma-separated list of variable names.
4 At the end put a semi-colon.
int i;
char c;
float f;
double d;
int i, j, k, abcD 12;
char d, e, f, CHAR, Char, chAr, char;
float f1, f2, f3, f4;
double d1, d2, D3, d3;

21 / 34
Variable Initialization
Once you declare a variable, the compiler allocates the requisite
amount of memory to be accessed by the name of the variable.
C does not make any attempt to fill that memory with any particular
value. You have to do it explicitly.
An uninitialized memory may contain any value (but it must contain
some value).
Put an equality sign immediately after the variable name followed by
a constant value before closing the declaration by a semicolon.
int i = 3;
char c = p;
float f = 1.0f;
double d = 1.0;
int i = 3, j, k = 0;
char c = p, d = Z, e, f = 0;
float f1 = 1f, f2, f3 = 3.3f, f4 =4.0f;
double d1 = 1.0, d2, D3, d3 = 4.5;
22 / 34
Outline

1 Why teach C?

2 Write-Compile-Execute

3 C Programs

4 Variables in C

5 More C Programs

23 / 34
Square.c

#include <stdio.h>
int main()
{
int n;
scanf(%d,&n);
printf(%d\n,n*n);
return 0;
}

24 / 34
Reciprocal1.c

#include <stdio.h>
int main()
{
int n;
scanf(%d,&n);
printf(%d\n,1/n);
return 0;
}
Is it a correct C program. Why?

25 / 34
if: Reciprocal2.c

#include <stdio.h>
int main()
{
int n;
scanf(%d,&n);
if(n ! = 0)
printf(%d\n,1/n);
return 0;
}
Is it a correct C program. Why?

26 / 34
if-else: Reciprocal3.c

#include <stdio.h>
int main()
{
int n;
scanf(%d,&n);
if(n ! = 0)
printf(%d\n,1/n);
else
printf(Naughty Kid\n);
return 0;
}
Is it a correct C program. Why?

27 / 34
Reciprocal4.c

#include <stdio.h>
int main()
{
int n;
scanf(%d,&n);
if(n ! = 0)
printf(%f\n, 1.0/n);
else
printf(Naughty Kid\n);
return 0;
}

28 / 34
Add.c

#include <stdio.h>
int main()
{
int a, b, c;
scanf(%d%d,&a, &b);
c = a + b;
printf(%d\n,c);
return 0;
}

29 / 34
LargestOfThree1.c

#include <stdio.h>
int main()
{
int x, y, z, max;
scanf (%d %d %d, &x, &y, &z);
if (x>y)
max = x;
else
max = y;
if (max > z)
printf(Largest is %d\n, max);
else
printf(Largest is %d\n, z);
return 0;
}

30 / 34
LargestOfThree2.c

#include <stdio.h>
int main()
{
int x, y, z;
scanf (%d %d %d, &x, &y, &z);
if (x>y && x>z)
printf(Largest is %d\n, x);
else
if (y > z)
printf(Largest is %d\n, y);
else
printf(Largest is %d\n, z);

return 0;
}

31 / 34
while loop: Factorial1.c
#include <stdio.h>
int main()
{
int i = 1, n, fact = 1;
scanf (%d, &n);
if(n < 0){
printf(Factorial of %d is not defined\n, n);
return 0;
}
if(n == 0){
printf(Factorial of %d is %d\n, n, fact);
return 0;
}
while( i <= n) {
fact = fact * i;
i = i + 1;
}
printf(Factorial of %d is %d\n, n, fact);
return 0;
} 32 / 34
do-while loop: Factorial2.c
#include <stdio.h>
int main()
{
int i = 1, n, fact = 1;
scanf (%d, &n);
if(n < 0){
printf(Factorial of %d is not defined\n, n);
return 0;
}
if(n == 0){
printf(Factorial of %d is %d\n, n, fact);
return 0;
}
do {
fact = fact * i;
i = i + 1;
}while( i <= n);
printf(Factorial of %d is %d\n, n, fact);
return 0;
} 33 / 34
for loop: Factorial3.c
#include <stdio.h>
int main()
{
int i, n, fact = 1;
scanf (%d, &n);
if(n < 0){
printf(Factorial of %d is not defined\n, n);
return 0;
}
if(n == 0){
printf(Factorial of %d is %d\n, n, fact);
return 0;
}
for( i = 1; i <= n; i++) {
fact = fact * i;
}
printf(Factorial of %d is %d\n, n, fact);
return 0;
}
34 / 34

Anda mungkin juga menyukai