1introduction To C and Datatypes
1introduction To C and Datatypes
Books
Balaguruswamy,
g y, Programming
g g in ANCI C”,, 2nd Edition
Gcc compiler
Use any editor based on gcc compilers
- codeblocks
- dev C++
Or you can use any online compiler like
- codechef
-and others
Your First
C PROGRAM !!!!!
Simplest C Program
Hash
#include<stdio.h>
int main()
{
printf(“ Programming in C is Interesting
printf( Interesting”););
return 0;
}
Preprocessor Directive
# include: Is a preprocessor directive which directs to
include the designated file which contains the declaration
of library functions (pre defined).
Don’t
Don t try to display a decimal number using the integer format
specifier,%d, as this displays unexpected values.
#include<stdio.h>
Similarly Write a program to
int main() find out what does printf( )
{ returns????
int n;
printf("enter
printf( enter the value
value");
);
printf("%d", scanf("%d",&n));
return 0;
}
Char Data type
A variable of type char can store a single character.
Example:
char c;
OR
char c, k, l;
D
Declaring
l i the
th character
h t does
d nott initialize
i iti li the
th variable
i bl with
ith the
th
desired value.
Memory view
char type
Declaration must specify the data type of the value of the variable.
char c=‘x’;
c= x ; or
char c;
c= x
c=‘x’
Printing value of char variable
printf( “%c”
%c , a);
Integers (int) -- %d
0 1 1000 -1 -10 666
Floating point numbers (float) -- %f
1.0 .1 1.0e-1 1e1
Program
#include<stdio.h>
#include<stdio h>
main( )
{
return 0; float a=3.0; a=3.000000
fl t b=
float b 4.00
4 00 + 77.00;
00 b=11 000000
b=11.000000
printf(“a=%f “,a);
printf(“\n
i tf(“\ b=%f
b %f ““,b);
b)
}
int and float
1 + 2 * 3 - 4.0
4 0 / 5
= 1 + (2 * 3) - (4.0 / 5)
= 1 + 6 - 0.8
= 6.2
Example 2
(1 + 2) * (3 - 4) / 5
= ((1 + 2) * (3 - 4)) / 5
= (3 * -1) / 5
= -3
3 / 5
= 0
Multiple Format specifiers
• You could
Y ld use as many fformatt specifiers
ifi as you wantt
with printf-just as long as you pass the correct number of
a gu e ts
arguments.
• The ordering of the arguments matters.The first one
should corresponding to the first format specifier in the
string and so on. Take this example:
printf( “a=%d,b=%d, c=%d\n”, a , b, c);
If a,b
b andd c were iintegers,this
t thi statement
t t t will
ill print
i t th
the
values in the correct order.
• Rewriting the statement as
printf( “a=%d,b=%d, c=%d\n”, c,a,b);
Would still cause the program to compile OK,but
OK but the values
of a,b and c would be displayed in the wrong order.
Statements
Statements
General ppurpose
p and Structure pprogramming
g g language.
g g
It gives user the flexibility to create the functions, which give C immense
power and scope.
C – Middle Level Language
printf(“%6d”,9876) 9 8 7 6
printf(
printf(“%2d”
%2d ,9876)
9876) 9 8 7 6
printf(
printf(“%-6d”,9876)
% 6d ,9876) 9 8 7 6
printf(“%06d”,9876) 0 0 9 8 7 6
Example
printf(“%7.4f”,98.7654) 9 8 . 7 6 5 4
printf(“%7.2f”,98.7654) 9 8 . 7 7
printf(“%-7.2f”,98.7654) 9 8 . 7 7
printf(“%f”,98.7654) 9 8 . 7 6 5 4 0 0
#include<stdio.h>
main()
{
pprintf(":%s:\n",
( % , "Hello,, world!");
);
printf(":%15s:\n", "Hello, world!");
printf(":%.10s:\n", "Computer");
printf(":%-10s:\n",
printf( :% 10s:\n , "Computer");
Computer );
printf(":%-15s:\n", "Computer");
printf(":%.15s:\n", "Computer");
printf(":%15.10s:\n",
printf( :%15.10s:\n , "Computer");
Computer );
printf(":%15.10s:\n", "Computer Science");
printf(":%-15.10s:\n", "Computer");
printf(":%-15.10s:\n",
printf( :% 15.10s:\n , "Computer
Computer Science
Science"););
}
Backslash ( \ ) character constants
\n : To include new line
\b : backspace
\r : carriage return
\t : horizontal tab
\a : alert
\” : double quote
\’ : single quote
\ : vertical
\v ti l tab
t b
\ \ : backslash
Exercise