0% found this document useful (0 votes)
44 views63 pages

Notes Unit1

Uploaded by

supremegamerlm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views63 pages

Notes Unit1

Uploaded by

supremegamerlm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

ES-101\ES-102

Programming In C

Unit - 1
Overview of C

1
Welcome
In
The
World
Of
‘C’
Course Materials

• Text Book:
– Programming In ANSI C (5th Edition) by E Balagurusamy
– C – The Complete Reference by Herbert Schildt
– Let Us C- Yashwant Kanetkar
• Additional Support:
– Class Lecture Slides

3
Course Outline:
• Introduction to C programming
• Constants, Variable & Data Types
• C Operators & Expressions
• Managing I/O Operations in C
• C Control Structure
• C Arrays
• C Characters and Strings
• C Functions
• C Pointers
• Structure and Union in C

4
Today’s Outline:
• Introduction to Programming
• History of C
• Why teach C
• What is C used for?
• Hello World! C Program
• Basic Structure of C Programs
• Programming Style in C
• Executing A ‘C’ Program
• Process for Executing A ‘C’ Program
• Rules to Remember.

5
Introduction to Programming
• What is a program?
– A computer program is a set of instructions that tell
a computer what to do.

• What is programming language?


– A programming language is a formal computer language
designed to communicate and give instructions to a
machine, particularly a computer.

• What is Programming C?
– C is a high-level and general purpose programming language.

6
Types of languages
(I) Lower level languages:-
Languages which are very near to
machine…. I.e. machine language,
Assembly language.

(II) Higher level languages:-


Languages which are very near to
programmer rather than to
machine….
I.e. C++,Visual C++,Visual basic,Java.
FORTRA Pascal

H gh-Level Language

Assembly La.nguage

Machine Language

Hardwae
History of C
• The root of all modern languages is ALGOL, introduced in
1960s.
• In 1967, Martin Rachards developed BCPL.
• In 1970, Ken Thompson created B by using BCPL features.
• In 1972, C was evolved from ALGOL, BCPL and B by Dennis
Ritchie at the Bell Laboratories on DEC PDP-11 machine.
Which referred as "Traditional C“.
• In 1978, introduced K&R C (Kerningham and Dennis Ritchie).
• In 1989, ANSI approved a version of C known as ANSI C.
• In 1990, ISO also approved this version referred as C89.
• In 1999, Another enhanced version of C is introduced C99.

9
Why teach C / Importance of C
• C is small (only 32 keywords).
• C has rich set of built-in functions and support variety of data
types & operators.
• C is highly portable (Machine independent).
• C is structured.
• C has ability to extend itself.
• C is stable (the language doesn’t change much).
• C is quick running (code written in c is efficient & fast).
• C is the basis for many other languages (C++, C#, Java, Perl
etc).
• C is a Programmers Language.

• It may not feel like it but C is one of the easiest language to


learn.

10
What is C used for?
• C is most likely an evergreen language.
• Initially, C widely known as the development language
of the UNIX operating system but today virtually all new
major operating systems are written in C and/or C++.

– Systems programming: OSes, like Linux.

– Microcontrollers: Automobiles and Airplanes.

– Embedded processors: Phones, Portable Electronics etc.

– DSP processors: Digital Audio.

26 November 2022 CSC-183 11


Hello World! Program

12
Format of simple C programs

main() Function Name


{ Start of Program
………….
………….
…………. Program Statements

}
End of Program

13
Basic Structure of C Programs

14
Basic Structure of C Programs (with Example)
/* Program for Print, Author: Mr. X */ Documentation Section
#include<stdio.h> Link Section
#define PI 3.1416 Definition Section
void print_pi(); Global Declaration Section
main() Function Section
int main() {
{
print_pi(); ………………
return 0; ………………
}
}
Subprogram Section
void print_pi() Function1()
{ {
printf("Value of PI is: %.4f",PI);
} }

15
Programming Style
• You should follow one style for programming.
• We must develop the habit of writing programs in
lowercase letters, because C programs statements are
written in lowercase letters.
• Uppercase letters are used only for symbolic constants.
• Braces, {} indicates beginning and end of a functions.
• Need, braces to align for easy readability.
• Try to write one statement into one line, although
C support multiple statement in a single line.

16
Executing A ‘C’ Program
• Executing a program written in C involves a series of
steps. There are:

1. Creating the program;

2. Compiling the program;

3. Linking the program with functions that are needed from


the C library;

4. Executing the program.

17
Process for Executing A ‘C’ Program

Edit
Program

Source Code

Compile

Object
Code

Link Object Code


Library Executable
Files

18
Rules to Remember:
• Every C program requires a main() program (more
than one main() illegal)
• Execution of a function begins at the { opening brace and
ends at the corresponding } closing brace.
• C programs are written in lowercase letters. Higher case
used for symbolic name and output strings.
• Every program statement in a C language must end
with a semicolon.
• include and define are special instruction s to
compiler, they do not end with a semicolon.
• Using comment /* Comment */ is a good
programming habit.

19
20
21
22
23
24
25
26
27
28
29
30
31
32
Write a C program to display following variables.
a+ c, x + c,dx + x, ((int) dx) + ax, a + x, s + b, ax + b, s + c, ax + c, ax + ux

33
#include <stdio.h>
void main()
{
int a = 125, b = 12345;
long ax = 1234567890;
short s = 4043;
float x = 2.13459;
double dx = 1.1415927;
char c = 'W';
unsigned long ux = 2541567890;

printf("a + c = %d\n", a + c);


printf("x + c = %f\n", x + c);
printf("dx + x = %f\n", dx + x);
printf("((int) dx) + ax = %ld\n", ((int) dx) + ax);
printf("a + x = %f\n", a + x);
printf("s + b = %d\n", s + b);
printf("ax + b = %ld\n", ax + b);
printf("s + c = %hd\n", s + c);
printf("ax + c = %ld\n", ax + c);
printf("ax + ux = %lu\n", ax + ux);

} 34
Sample Output:

a + c = 212
x + c = 89.134590
dx + x = 3.276183
((int) dx) + ax = 1234567891
a + x = 127.134590
s + b = 16388
ax + b = 1234580235
s + c = 4130
ax + c = 1234567977
ax + ux = 3776135780

35
36
37
38
C Quiz
1. Who is the father of C language?
a) Steve Jobs
b) James Gosling
c) Dennis Ritchie
d) Rasmus Lerdorf

39
Answer: C

Explanation: Dennis Ritchie is the father of C Programming


Language. C programming language was developed in 1972 at
American Telephone & Telegraph Bell Laboratories of USA.

26 November 2022 CSC-183 40


C Quiz
1. Who is the father of C language?
a) Steve Jobs
b) James Gosling
c) Dennis Ritchie
d) Rasmus Lerdorf

41
C Quiz
2. Which of the following is true for variable names in C?
a) They can contain alphanumeric characters as well as
special characters
b) It is not an error to declare a variable to be one of the
keywords(like goto, static)
c) Variable names cannot start with a digit
d) Variable can be of any length

42
C Quiz

Answer: c

Explanation: According to the syntax for C variable name, it


cannot start with a digit.

43
C Quiz
3. Which of the following cannot be a
variable name in C?
a) volatile
b) true
c) friend
d) export

44
C Quiz
Answer: a

Explanation: volatile is C
keyword.

45
C Quiz
4. Which of the following declaration is
not supported by C language?

a) String str;
b) char *str;
c) float str = 3e2;
d) Both String str; & float str = 3e2

46
C Quiz
Answer: a

Explanation: It is legal in Java, but not in C


language.

47
C Quiz
5. How is search done in #include and #include
“somelibrary.h” according to C standard?

a) When former is used, current directory is searched and when


latter is used, standard directory is searched
b) When former is used, standard directory is searched and when
latter is used, current directory is searched
c) When former is used, search is done in implementation
defined manner and when latter is used, current directory is
searched
d) For both, search for ‘somelibrary’ is done
in implementation-defined places

48
C Quiz

Answer: d

49
C Quiz
6. What will be the output of the following C code?

#include <stdio.h>
int main()
{
int y = 10000;
int y = 34;
printf("Hello World! %d\n", y);
return 0;
}
a) Compile time error
b) Hello World! 34
c) Hello World! 1000
d) Hello World! followed by a junk value
50
C Quiz
Answer: a

Explanation: Since y is already defined, redefining it


results in an error.
Output:
$ cc pgm2.c
pgm2.c: In function ‘main’:
pgm2.c:5: error: redefinition of ‘y’
pgm2.c:4: note: previous definition of ‘y’ was here

51
C Quiz
Q: Identify the Valid Variables and Invalid
Variables

52
C Quiz

53
C Quiz
Algorithm:
Algorithm of this program is to find
of two numbers.

START
Step 1 → Define two variables - A, B
Step 2 → Set loop from 1 to max of A, B
Step 3 → Check if both are completely divided by same
loop number, if yes, store it
Step 4 → Display the stored number is

STOP
54
C Quiz
An H.C.F or Highest Common Factor, is the largest
common factor of two or more values.

For example factors of 12 and 16 are −

12 → 1, 2, 3, 4, 6, 12
16 → 1, 2, 4, 8, 16

The common factors are 1, 2, 4 and the highest


common factor is 4.

55
Food Of
Thought

56
57
Program: Temperature
Conversion Problem

58
59
60
61
Thank You All

62

You might also like