1
CSE-1102: INTRODUCTION TO PROGRAMMING
LECTURE 3: FUNDAMENTALS
Md. Nazmul Abdal
Lecturer
Department of CSE
University of Liberal Arts Bangladesh (ULAB)
Type Conversion
◻ A type cast is basically a conversion from one type to another. There are two
types of type conversion:
◻ Implicit Type Conversion Also known as ‘automatic type conversion’.
Done by the compiler on its own, without programmers intervention.
All the data types of the variables are upgraded to the data type of the
variable with largest data type.
#include<stdio.h> #include <stdio.h>
int main() main ()
{ {
int x = 10; // integer x int a;
char y = 'a'; // character c a = 15/6;
x = x + y; // ‘a’ ascii is 97 printf("%d",a);
float z = x + 1.0; }
printf("x = %d, z = %f", x, z);
return 0;
}
Output: Output:
x = 107, z = 108.000000 2 2
Implicit Type Conversion
i (int) = 7, f (float)= 5.5, c ( Char) = ‘w’.
Expression Value Type
i+f 12.5 Float
i+c 126 Integer
i + c – ‘0’ 78 Integer
(i + c) – (2 * f / 5) 123.8 Float
◻ An operation between char and int will result in int.
◻ An operation between float and double will result in double.
◻ An operation between float and long double will result in long double.
◻ An operation between float and char or int will result in float 3
Explicit Type Conversion
◻ This process is also called type casting and it is user defined.
◻ Here the user can type cast the result to make it of a particular data type.
#include<stdio.h>
int main()
{
double x = 1.2;
x = (int)x + 1; // Explicit conversion from double to int
printf(“Sum = %lf ", x);
return 0;
}
Output:
4
Sum = 2.000000
Take Input Using C
5
◻ getchar() function reads the next available character from the screen
and returns it as an integer.
◻ putchar(int c) function puts the passed character on the screen and
returns the same character.
◻ gets(char *s) function reads a line from stdin into the buffer pointed to
by s until either a terminating newline or EOF (End of File).
◻ puts(char *s) function writes the string 's' and 'a' trailing newline
to stdout.
#include <stdio.h> #include <stdio.h>
int main( ) { int main( ) {
char c; char str[100];
printf( "Enter a value :"); printf( "Enter a value :");
c = getchar( ); // scanf(“%c”, &c); gets( str ); // scanf(“%s”, str);
printf( "\nYou entered: "); printf( "\nYou entered: ");
putchar( c ); // printf(“%c”, c); puts( str ); // printf(“%s”, str);
return 0; return 0;
} }
scanf() and printf() Functions
6
◻ Two in-built functions printf() and scanf() to perform I/O task in C programming.
◻ printf() displays the formatted output.
◻ scanf() reads the formatted input and stores them.
◻ For integer variable use %d, for character %c, for string %s, for float %f, for double
%ld, for Unsigned integer %u, Long integer %ld, Long double %lf
#include <stdio.h> #include <stdio.h>
int main( ) { int main()
char str[100]; {
int i; int number;
printf( "Enter a value :"); printf("Enter an integer: ");
scanf("%s %d", str, &i); scanf("%d", &number);
printf( "\nYou entered: %s %d ", str, i); printf("You entered: %d", number);
return 0; return 0;
} }
Output Output
Enter a value : seven 7 Enter an integer: 7
You entered: seven 7 You entered: 7
Taking Input using scanf() Function
7
◻ When a character is entered in the program, the character itself is not
stored. Instead, a numeric value(ASCII value) is stored.
◻ When we display that value using "%c" text format, the entered character
is displayed.
◻ When we display that value using "%d" text format, the ASCII value of
that character is displayed.
#include <stdio.h> Output
int main() Enter a character: g
{ You entered g.
char chr; ASCII value of g is 103.
printf("Enter a character: ");
scanf("%c", &chr);
// When %c text format is used, char is displayed in case of char types
printf("You entered %c.\n", chr);
// When %d text format is used, int is displayed in case of character types
printf("ASCII value of %c is %d.", chr, chr);
return 0;
}
ASCII Table
8
◻ ASCII stands for American Standard Code for Information Interchange.
◻ Computers can only understand numbers, so an ASCII code is the numerical
representation of a character
ESCAPE SEQUENCES IN C
9
◻ Escape sequences are used to represent certain special characters within string
literals and character literals. Escape sequences always begin with a backslash.
Escape sequence Description
\' single quote #include <stdio.h>
int main()
\" double quote {
\? question mark printf("This\nis\na\ntest\n\nShe said, \"How
\\ backslash are you?\"\n");
}
\a audible bell
\b backspace Output:
\f form feed - new page
\n line feed - new line
\r carriage return
\t horizontal tab
\v vertical tab
EXPRESSION
10
In programming, an expression is any legal combination of symbols that represents a
value.
C Programming Provides its own rules of Expression, whether it is legal expression
or illegal expression. For example, in the C language x+5 is a legal expression. + + a
+ b is illegal expression.
Every expression consists of at least one operand and can have one or more
operators.
Operands are values and Operators are symbols that represent particular actions.
◻ Types of Expression :
Type Explanation Example
Infix Operand1 Operator Operand2 a>b
Prefix Operator Operand1 Operand2 >ab
Postfix Operand1 Operand2 Operator ab>
STATEMENT
11
▪ A statement causes the computer to carry out some action.
◻ Types of Statement : There are three types of statement in C.
1. Expression Statement: The simplest kind of statement in C is an expression
(followed by a semicolon, the terminator for all simple statements). Examples:
a=3; c= a+b; ++i; printf(“area= %f”, area);
2. Compound Statement: A compound statement consists of several individual
statements enclosed within a pair of braces { }. Examples:
if(radius>0)
{
float pi = 3.1416;
circumference = 2 * pi * radius;
Area = pi * radius * radius;
}
STATEMENT
12
3. Control Statement: A control statement are used to create special program features,
such as logical tests, loops, and branches.
Examples:
while(count<= n )
{
printf(“x=“);
scanf(“%f”, &x);
sum += x;
++count;
}
EXPRESSION AND STATEMENT
13
Expression Statement
a+b a = 3;
x=y x = y;
c=a+b c = a + b;
x<=y printf (“%d”, n);
x == y ; // NULL Statement
++i ++i;
Library Function in C
14
◻ C Standard library functions or simply C Library functions are inbuilt functions in
C programming.
◻ The prototype and data definitions of the functions are present in their respective
header files and must be included in your program to access them.
stdio.h: I/O functions: string.h: String functions
getchar() returns the next character typed on the keyboard. strcat() concatenates a copy of str2 to str1
putchar() outputs a single character to the screen. strcmp() compares two strings
printf() strcpy() copies contents of str2 to str1
scanf()
math.h: Mathematics functions: ctype.h: Character functions
acos() returns arc cosine of arg isdigit() returns non-0 if arg is digit 0 to 9
asin() returns arc sine of arg isalpha() returns non-0 if arg is a letter of the
atan() returns arc tangent of arg alphabet
cos() returns cosine of arg isalnum() returns non-0 if arg is a letter or digit
exp() returns natural logarithim e islower() returns non-0 if arg is lowercase letter
fabs() returns absolute value of num isupper) returns non-0 if arg is uppercase letter
sqrt() returns square root of num
pow() returns the power of a value
time.h: Time and Date functions stdlib.h:Miscellaneous functions
time() returns current calender time of system malloc() provides dynamic memory allocation,
difftime() returns difference in secs between two times covered in future sections
rand() for random function generate
THANK YOU