lb 1 2 ref material
TRANSCRIPT
-
7/30/2019 Lb 1 2 Ref Material
1/42
ICP
Lab -1 & 2
-
7/30/2019 Lb 1 2 Ref Material
2/42
Simplest C Program
Hash
#includemain()
{
printf( Programming in C is Interesting);
return 0;
}
-
7/30/2019 Lb 1 2 Ref Material
3/42
Preprocessor Directive
# include: Is a preprocessor directive whichdirects to include the designated file whichcontains the declaration of library functions(pre defined).
stdio.h (standard input output) : A header filewhich contains declaration for standard input
and output functions of the program.Like printf,scanf
-
7/30/2019 Lb 1 2 Ref Material
4/42
When to use preprocessor directive
When one or more library functions are used,the corresponding header file where the
information about these functions will bepresent are to be included.
When you want to make use of thefunctions(user-defined) present in a differentprogram in your new program the sourceprogram of previous function has to be
included.
-
7/30/2019 Lb 1 2 Ref Material
5/42
Defining main( )
When a C program is executed, system firstcalls the main( ) function, thus a C programmust always contain the function main( )somewhere.
A function definition has:
heading
{declarations ;
statements;
}
-
7/30/2019 Lb 1 2 Ref Material
6/42
Basic Structure of a C program
preprocessor directiveint main( )
{
declarations;
_________________;
______body_______;
_________________;
return 0;}
-
7/30/2019 Lb 1 2 Ref Material
7/42
Concept of Comment
Comments are inserted in program to maintainthe clarity and for future references. They helpin easy debugging.
Comments are NOT compiled, they are just forthe programmer to maintain the readability ofthe source code.
Comments are included as
/* ..
*/
-
7/30/2019 Lb 1 2 Ref Material
8/42
PRE DEFINED
DATA TYPES
-
7/30/2019 Lb 1 2 Ref Material
9/42
Data Types
Every program specifies a set of operations tobe done on some data in a particular sequence.
However, the data can be of many types suchas a number, real, character, string etc.
C supports many data types out of which the
basic types are:
int, float , double and char.
-
7/30/2019 Lb 1 2 Ref Material
10/42
Four Basic Data Types In C, there are 4 basic data types:
1. char,
2. int,
3. Float and
4. Double
Each one has its own properties.For
instance,they all have different sizes.
The size of a variable can be pictures as thenumber of memory slots that are required to
store it.
-
7/30/2019 Lb 1 2 Ref Material
11/42
Char Data type A variable of type charcan store a single
character.
All character have a numeric code associated with
them, so in reality while storing a charactervariable its numeric value gets stored.
The set of numeric code is called as ASCII,American Standard Code for Information
Interchange.
-
7/30/2019 Lb 1 2 Ref Material
12/42
Reading a character
Simplest of all operations is reading character
from stdin and writing to stdout
Single character can be read from getchar()
Writing a character to terminal can be done byputchar()
var_name = getchar();
-
7/30/2019 Lb 1 2 Ref Material
13/42
char name; //name is of character data
type
name = getchar(); // single character is read &
assignedto variable name
putchar(name); //displays character typed by
you onscreen
-
7/30/2019 Lb 1 2 Ref Material
14/42
Fundamental Data Types
Data type Size in bits
integer (int) 16
character (char) 8
floating point (float) 32
double 64
-
7/30/2019 Lb 1 2 Ref Material
15/42
Size of data types
Size of data types is compilerand processortypes dependent.
The size of data type can be determined by
using sizeof keyword specified in betweenparenthesis
For Turbo 3.0 compiler, usually in bytes
char -> 1 bytes int -> 2 bytes
float -> 4 bytes
double -> 8 bytes
-
7/30/2019 Lb 1 2 Ref Material
16/42
Integer Data Type
Variables of the int data type represent wholenumbers.
If at run time you try to assign a fraction to an int
variable,the decimal part is ignored and thevalue assigned is rounded down from the actual
value.
Also assigning a character constant to an int
variable at compile time assigns the ASCII value.
-
7/30/2019 Lb 1 2 Ref Material
17/42
Float data type
Forstoring decimal numbers float data type isused.
Floats are relatively easy to use but problemstend to occur when performing division
In general:
An int divided by an int returns an int.
An int divided by a float returns a float.A float divided by an int returns a float.
A float divided by a float returns a float.
-
7/30/2019 Lb 1 2 Ref Material
18/42
Char data type
Characters (char) To store a character intoa char variable,you must enclose it withSINGLE QUOTE marks i.e. by .
The double quotes are reserved for string(a
collection of character). The character that you assign are called ascharacter constants.
You can assign a char variable an integer.i.e.
its integer value.a, z , A , Z , ?, @, 0, 9
- Special Characters: preceded by\
\n, \t , \0, \, \\ etc.
-
7/30/2019 Lb 1 2 Ref Material
19/42
ASCII codes
-
7/30/2019 Lb 1 2 Ref Material
20/42
Format specifiers There are several format specifiers-The one you
use should depend on the type of the variable youwish to print out.The common ones are as follows:
Format Specifier Type
%d int%c char
%f float
%lf double%s string
To display a number in scientific notation,use %e.
To display a percentage sign,use %%
-
7/30/2019 Lb 1 2 Ref Material
21/42
Declaration of Variable To Declare a variable means to reserve memory
space for it.
In the declaration variable is provided a namewhich is used through out the program to refer tothat character.
Example:
char c;
OR
char c, k, l;
Declaring the characterdoes not initialize thevariable with the desired value.
-
7/30/2019 Lb 1 2 Ref Material
22/42
Memory view
i
c
char type
integer
Each variable has an
unique address of
memory location
associated with it
-
7/30/2019 Lb 1 2 Ref Material
23/42
Important about Variable
Always remember in C , a variable must always be
declared before it used.
Declaration must specify the data type of the value of
the variable.
Name of the variable should match with itsfunctionality /purpose.
Example int count ; for counting the iterations.
float per_centage ; for percentage ofstudent
-
7/30/2019 Lb 1 2 Ref Material
24/42
Initialization
Initializing a variable involves assigning(puttingin) a value for the first time.This is done byusing the ASSIGNMENT OPERATOR, denotedby the equals sign,=.
Declaration and initializing can be done onseparate lines, or on one line.
char c=x; or
char c;
c=x
-
7/30/2019 Lb 1 2 Ref Material
25/42
Printing value of char variable
printf( %c, a);
This causes the %c to be replaced by
value of character variable a.
printf(\n %c, a);
\n is a new line character which will printthe value of variable on newline.
-
7/30/2019 Lb 1 2 Ref Material
26/42
Identifiers and Variables Identifier: A name has to be devised for program
elements such as variables or functions.
Variable:
Variables are memory regions you can use to hold data
while your program is running.
Thus variable has an unique address in memory region.
For your convenience, you give them names (instead of
having to know the address)
Because different types of data are different sizes, thecomputer needs to know what type each variable is so it
can reserve the memory you need
-
7/30/2019 Lb 1 2 Ref Material
27/42
Rules for Identifiers Contains only letters, digits and under score
characters,
example amount, hit_count
Must begin with either a letter of the alphabet or anunderscore character.
Can not be same as the keywords, example it cannot be void, int.
Uppercase letters are different than lowercase,example amount, Amount and AMOUNT all threeare different identifiers.
Maximum length can be 31 characters.
Should not be the same as one already defined inthe library, example it can not be printf/scanf.
No special characters are permitted. e.g. blanks ace eriod semicolon comma etc.
-
7/30/2019 Lb 1 2 Ref Material
28/42
Naming Variables
-
7/30/2019 Lb 1 2 Ref Material
29/42
INPUT & OUTPUT
Input and Output form an important part of anyprogram.
To do anything useful your program needs to be ableto accept input data and report back your results.
In C, the standard library provides routines for inputand output. The standard library has functions for i/othat handle
input, output, and character and string manipulation.
Presently we consider, all the input functionsdescribed read from standard input and all theoutput functions described write to standard output.Standard input is usually the keyboard. Standard
output is usually the monitor.
-
7/30/2019 Lb 1 2 Ref Material
30/42
Display data using printf()
#include
main()
{
int age = 25;printf( %d,age);
}
output:
25
-
7/30/2019 Lb 1 2 Ref Material
31/42
Display data using printf()
#include
main()
{
int age = 25;printf( The age of student: %d,age);
}
output:
The age of student: 25
-
7/30/2019 Lb 1 2 Ref Material
32/42
printf( )
1. It is used to print message or result on the output screen.It is
define in stdio.h header file.
2. Returns the number of characters it outputs on the screen.
Example:printf( Enter the number whose multiplication table you want to
generate);
printf( Balance in your account is %d, bal); /* where bal is int
type*/
printf(Balance =%d,Total tax is %f ,bal, tax); /* where tax is
float type */
-
7/30/2019 Lb 1 2 Ref Material
33/42
scanf( ) scanf( ) : It is used to read data from keyboard that can be
numerical values, characters or strings.It is defined instdio.h
The function returns the number of data items that havebeen entered successfully.
Example:int num1,num2,num3;
char var;
printf( enter the numbers );
scanf(%d%d%d, &num1,&num2,&num3);printf(enter a character);
scanf(%c, &var);
-
7/30/2019 Lb 1 2 Ref Material
34/42
-
7/30/2019 Lb 1 2 Ref Material
35/42
-
7/30/2019 Lb 1 2 Ref Material
36/42
-
7/30/2019 Lb 1 2 Ref Material
37/42
-
7/30/2019 Lb 1 2 Ref Material
38/42
-
7/30/2019 Lb 1 2 Ref Material
39/42
-
7/30/2019 Lb 1 2 Ref Material
40/42
-
7/30/2019 Lb 1 2 Ref Material
41/42
-
7/30/2019 Lb 1 2 Ref Material
42/42