Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C language

Similar presentations


Presentation on theme: "Introduction to C language"— Presentation transcript:

1 Introduction to C language
Department of CS&IT, NED University of Engineering and Technology, Karachi Introduction to C language Lecture by Jameel Asghar IT Manager Department of CS&IT NED University of Engineering and Technology, Karachi

2 Reference Books Let Us C by Yashavant P. Kanetkar
Turbo C by Robert Lafore

3 What is a Program? A Precise sequence of steps to solve a particular problem. Precise=exact Sequence= what should be first, what should be second, and so on.. And there should be any problem to solve.

4 What is a programming Language?
A vocabulary and set of grammatical rules for instructing a Computer to perform specific tasks. Each language has a unique set of keywords (words that it understands) and a special syntax for organizing program instructions.

5 Ask 8 different Computer Scientists, get 8 different answers!
formal notation for computations tool for writing programs means of communicating between programmers vehicle for expressing high-level designs notation for algorithms way of expressing relationships between concepts tool for experimenting with solutions to problems means for controlling computerized devices

6 Introduction to C Language
C is a programming language. It was developed at AT & T’s Bell Laboratories of USA in 1972. It was designed and written by a man named Dennis Ritchie. Dennis Ritchie

7 Reasons to learn C Main reason is its simplicity reliability, and its easy to use and easy to learn

8

9 Some Definitions Syntax(form):-The syntax of a language describes the possible combinations of symbols that form a correct program.  IDE:- It is a screen display with pull down menus. We use menu selections to invoke all the operations necessary to develop our program.

10 Compiler:- It is the part of IDE, that translates our source file into machine language.
Keyword:- Keywords are the words whose meaning has already been explained to the C compiler

11 Download tc3setup.exe file from
How to install Turbo C Download tc3setup.exe file from Exact link is as below:

12

13

14

15

16

17

18

19

20 My first Program #include<stdio.h> void main(void) { printf(“How are you!”); }

21

22 Output How are you!

23 What is #include<stdio.h> ?
Since we are using a function printf(); in our program in order to print a sentence, so it is necessary to give reference of this function. The functionality of printf(); function is defined somewhere in one of many C Standard Libraries, the name of that library is stdio.h. Hence including stdio.h at the top of our program become reference for printf function. We also call it prototype, or header file, since it is at the head of our program. stdio.h, which stands for "standard input/output header", is the header in the C standard library that contains macro definitions, constants, and declarations of functions and types used for various standard input and output operations.

24 What is void main(void)?
C language is function oriented language. In this language we write programs through functions. C provides some built-in functions. We call it built-in, because functions can also be user defined, which we will learn later in this course. Hence main is one of the functions of C. Its name is main, because it is an important function, and must be used in a C program, whether you are using other functions or not.

25 What is void main(void)? Cont..
Void which is written before main means that the function main will not return a value. How a value is Returned? You will learn this ahead in your course. Void which is written after main means that the function main will not send any argument. How arguments are sent/passed? This topic also is covered in coming topics.

26 More on main()…. This is the entry point of a program
When a file is executed, the start point is the main function From main function the flow goes as per the programmers choice. There may or may not be other functions written by user in a program Main function is compulsory for any c program

27 #include<stdio. h> #include<conio
#include<stdio.h> #include<conio.h> void main(void) { printf(“How are you!”); getch(); }

28 Explanation: Note that, we have written an extra function: “getch();”. This function holds the output ”How are you” on the output window, till user presses any key. Getch stands for get character. Getch() fucntion is the member of conio.h library, hence we include this library at the top of our program.

29 #include<stdio.h> #include<conio.h> void main(void) {
clrscr(); printf(“How are you!”); getch(); }

30 What is clrscr();? Clrscr(); is a fucntion used to clear the previous output from output window.

31 My second Program #include<stdio.h> #include<conio.h>
void main(void) { clrscr(); printf(“4 is a number”); getch(); }

32 Output 4 is a number

33 #include<stdio.h> #include<conio.h> void main(void) {
clrscr(); printf(“4+4 is a number”); getch(); }

34 Output 4+4 is a number Explanation: Since we are writing a number inside the double quotes, it is treated as text, not a number. In order to treat numbers as numbers, we use format specifiers inside printf.

35 #include<stdio.h> #include<conio.h> void main(void) {
clrscr(); printf(“%d is a number”,4); getch(); }

36 #include<stdio.h> #include<conio.h> void main(void) {
clrscr(); printf(“%d is a number”,4+4); getch(); }

37 %d is a format specifier, used to indicate integers.
Integer is a Data Type, used to represent non decimal numbers.

38 Data types in C Primitive data types Aggregate data types
int, float, double, char Aggregate data types Arrays come under this category Arrays can contain collection of int or float or char or double data User defined data types Structures and enum fall under this category.

39 Variables Variables are data that will keep on changing Declaration
<<Data type>> <<variable name>>; int a; Definition <<varname>>=<<value>>; a=10; Usage <<varname>> a=a+1; //increments the value of a by 1

40 Variable names- Rules Should not be a reserved word like int etc..
Should start with a letter or an underscore(_) Can contain letters, numbers or underscore. No other special characters are allowed including space Variable names are case sensitive A and a are different.

41

42 Rules for Constructing Integer Constants Rules for Constructing Integer Constants
An integer constant must have at least one digit. It must not have a decimal point. It can be either positive or negative. If no sign precedes an integer constant it is assumed to be positive. No commas or blanks are allowed within an integer constant. The allowable range for integer constants is to

43 How we use an Integer in C Program
#include<stdio.h> #include<conio.h> void main(void) { clrscr(); int d; d=84; printf(“%d is a number”,d); getch(); }

44 A constant is quantity, that doesn’t change.
A variable can be considered as a name given to the locationin memory, where this constant is stored. d=84; In above instruction, d is a variable, and 84 is constant.

45 We can use mathematical manipulations/ calculations with integers, some of the examples are given:

46 #include<stdio.h> #include<conio.h> void main(void) {
clrscr(); int d; d=84+2; printf(“%d is a number”,d); getch(); }

47 #include<stdio.h> #include<conio.h> void main(void) {
clrscr(); int d; d=84; printf(“%d is a number”,d+2); getch(); }

48 #include<stdio.h> #include<conio.h> void main(void) {
clrscr(); int d=84; printf(“%d is a number”,d); getch(); }

49 #include<stdio.h> #include<conio.h> void main(void) {
clrscr(); int d=84, e=50, f; f=d+e; printf(“%d is a number”,f); getch(); }

50 #include<stdio.h> #include<conio.h> void main(void) {
clrscr(); int d=84, e=50, f; f=d-e; printf(“%d is a number”,f); getch(); }

51 #include<stdio.h> #include<conio.h> void main(void) {
clrscr(); int d=84, e=50, f; f=d/e; printf(“%d is a number”,f); getch(); }

52 #include<stdio.h> #include<conio.h> void main(void) {
clrscr(); float d=84, e=50, f; f=d/e; printf(“%f is a number”,f); getch(); }

53 Getting input from User
#include<stdio.h> #include<conio.h> void main(void) { clrscr(); int a; printf(”Please type a number:”); scanf(“%d”,&a); printf(“You typed %d”, a ); getch(); }

54 #include<stdio.h> #include<conio.h> void main(void) {
clrscr(); int a,b; printf(”Please type two numbers:”); scanf(“%d%d”,&a,&b); printf(“You typed %d and %d”, a , b ); getch(); }

55 Task Write a Program, that gets input two numbers from user, and calculates their sum.


Download ppt "Introduction to C language"

Similar presentations


Ads by Google