Presentation is loading. Please wait.

Presentation is loading. Please wait.

Getting Started With Coding

Similar presentations


Presentation on theme: "Getting Started With Coding"— Presentation transcript:

1 Getting Started With Coding
KIC 11/20/2019 Department of Information Technology. Introduction to Computer Programming Lec 3 Getting Started With Coding Introduction to Computer Programming 11/20/2019 Introduction to Computer Programming

2 Outlines Introduction to Programming
Stages & Steps of Writing Programs Variables in C Data types Declarations Format Specifiers Assignment Statements Input Statement

3 Introduction to Programming
A language which is used to instruct computer to perform different tasks according to the requirements. Every programming language has following basic things. Declarations Assignments Control Statements I/O (Input/output) Statements

4 Introduction to Programming
Any programming language consists of its own keywords and syntax rules that distinguishes it from other programming languages. If you want to program in a specific language, you need the compiler of this specific language. For example, C language compiler translates programs written in C language to machine language.

5 Stages & Steps of Writing Programs
Specification Design Coding Testing Documentation Keep it clear & simple Keep it short Think it through in advance Maintenance

6 Sample Program Explanation
#include<stdio.h> int main() { printf(“Hello to programming”); printf(“\n Welcome at KIC”); printf(“\t at Spring2014”); } Header File used for Input / Output Main Function Necessary to start any C program printf is used to display the text & variable values stored in the memory locations. Function Start Output Statement used to display on screen \n Start New Line \t Give Space between text Function End

7 Sample Program Explanation
#include <stdio.h> int main() { int a,b,c; a=2; b=5; c=a+b; printf("c=%d",c); } Header File Main Function Variable Declaration Function Body Assignment Statements Output

8 Variables in C 1. int 2. float 3. char
In computer programming, a variable is a name given to some known or unknown quantity or value, for the purpose of allowing the name to be used independently of the value it represents [Data Type] [Variable Name] ; Data Types: In C programming there are more data types but we generally use the following data types. 1. int 2. float 3. char

9 Variables in C Variable Name: You can give any name to the variables in c but there are few rules for writing variable names. (A-Z) The first character must be a letter Space is not allowed between names of identifiers Special symbols are not allowed in identifier names - Hyphens are not allowed in identifier names however underscores _ can be used (int, float, private etc.) The reserve words are not allowed in identifier names.

10 Variables in C Some valid identifiers / variable names
hello j box22a get_data bin3d count Some reserve words (can’t be used as identifier): private else static int const float

11 Ex Variable Names velocity velocity freda freda time-of-day
int tax_rate x2 4x first value emp. velocity freda time_of_day INVALID tax_rate x2 x4 first_value radio4 emp RIGHT RIGHT RIGHT RIGHT RIGHT WRONG WRONG WRONG WRONG

12 Ex Identifiers X_ray 7_UP x AR*EA x _No A9 a-a x b x

13 Data Types In C Type Length Range int 16 bits -32768 to 32767 float
1e to 1e+38 char 8 bits to 127 unsigned int to long int to unsigned long 0 to double 64 bits 1.7*(10**-308) to 1.7*(10**+308) 1.7*(E-308) to 1.7*(E+308) long double 80 bits 4.4*(10**-4932) to 1.1*(10**+4932)

14 Declarations The declaration means list all the variables (used for inputs and outputs) for one program in start. In declaration we have to tell the types of variables also. If we have more than one types of variables in one program then we have to mention them separately in the start. int x ; int x,y; The difference in variables and constants is, the value of variable can be changed within a program but the value of constant can’t be changed within a program.

15 Ex Declarations We usually declare all variables inside main function for example: #include<stdio.h> int main() { int avar ; int salary ; float percentage ; } Data Type Variable Name Semi Colon (every C statement ends with a semicolon ;)

16 Data Type Format Specifiers
int %i , %d float %f char %c double long int %li These format specifiers we use in Input / Output statements.

17 Floating Point Use the keyword float to declare float variables
Use the float data type when you know the variable will hold values that has decimal point. The range of float numbers is from 1e-38 to 1e38 To display float numbers use %f printf(“%f”, 12.1); 12.1 Show the output obtained from: printf(“%.3f”, ); 41.574

18 Double Numbers Charachters
The keyword double is used to define double numbers Double values have the range 1E-308 to 1E+308 printf(“%lf”, ); Charachters Characters are letters and symbols When you need to store letters, use character variable. Use the keyword char to declare character variables. Character variables can store only one letter e.g. A or B or C etc. printf(“%c”, ‘t’);

19 Displaying Variable Value
%d These are called Format Specifiers used to display the value stored in the variable i.e. Inside computer memory.

20 Assignment Statements
Before using variables in assignment statements you should define / declare them first e.g. int x; Assignment Statements x = 35; y = 21; sum = x + y; The first two statements assigns the values 35 and 21 to the variables x and y respectively The next statement assigns the sum of the two values to the variable sum.

21 Input Statement Scanf()
Scanf is an input statement which is used to store values in different memory locations using variables For example: int X; scanf(“%d ”, &X); format specifier to display Integer values Address of Symbol If X string (No ‘&’ symbol on this one!)

22 Multiple Variables in one Scanf
scanf(“%d %d”, &num1, &num2); It means that it would stop here and wait for the user to type in two values. Once they have been typed in & the Enter Key pressed, the values are assigned to num1 & num2.

23 Combining input & output Statements
printf(“Input Two Integer Values ”); scanf(“%d %d”, &num1, &num2); In the first place, there is a problem that how the user would come to know about the program is waiting for two values? The program should prompt user to enter two values. Now it will be easy for user to understand which type of values he suppose to enter.

24 Things to Remember Make sure that the specifications agree with the data to be read. Make sure to put the & character before the variable name so that you specify the address of the variable in memory not the variable itself. The prompt message should be short and clear.

25 KIC/Introduction to Computer Programming
11/20/2019 KIC/Introduction to Computer Programming


Download ppt "Getting Started With Coding"

Similar presentations


Ads by Google