SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
SriRaghavendraEducationalInstitutionsSociety(R)
www.skit.org.in
Title: - Introduction to C
COaddressed:CO2
Course: BESCK204E –Introduction to C
Presented by: Dr Nirmala S Guptha
Department:CSE
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
2
/skit.org.in
Introduction to C Programming :
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
3
/skit.org.in
HISTORY OF C :
(Europe)
ALGOL1960
International Group (Europe)
Combined BCPL Lang.)
1967 Martin Richards
(Basic Programming Combined
B 1970
Version of UNIX)
Ken Thompson( first version of
C 1972
Dennis Ritchie (C)
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
4
/skit.org.in
History of C --- contd..
K&R C 1978
Kernighan & Ritchie
ANSI C 1989 ANSI committee
(American National Standards Institute) ANSI/ISO C 1990 ISO committee (International Standards Organization)
K&R C 1978
Kernighan & Ritchie
ANSI C 1989 ANSI committee
(American National Standards Institute) ANSI/ISO C 1990 ISO committee (International Standards Organization)
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
5
/skit.org.in
WHY LEARN C?
C is easy to learn
C is portable
C is middle level language
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
6
/skit.org.in
Characteristics of C :
software and business
Characteristics
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
7
/skit.org.in
Characteristics of C :
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
8
/skit.org.in
USES of C :
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
9
/skit.org.in
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
STRUCTURE : C PROGRAM
1
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
ILLUSTRATED VERSION OF A PROGRAM:
1
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Writing the first C Program:
# Include <stdio.h> Int main()
{
printf(“\n welcome to C programming”);
}
1
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Parts of a Program:
#include <stdio.h>
int x;
int main () {
int y; Local Declaration
printf("Enter x and y: "); scanf(&x,&y);
printf("Sum is %d\n",x+y);
}
Preprocessor Directive
Global Declaration
Function
Statements
1
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Comments:
Text between /* and */
Used to “document” the code for the human reader Ignored by compiler (not part of program)
Have to be careful
comments may cover multiple lines
ends as soon as */ encountered (so no internal comments - /* An /* internal */ comment */)
// single line
1
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Comment Example:
#include <stdio.h>
/* This comment covers
*/
int main () /* The main header */ {
/* No local declarations */
printf(“Too many comments\n”);
} /* end of main */
1
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Files used in a C program:
Files in a C program
1
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Source Code File:
1
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Header file:
1
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Standard Header Files:
Predefined files
String.h – string handling functions
Stdlib.h – Some library function (miscellaneous functions)
Stdio.h – Standardized input and output functions
Math.h – Mathematical functions Alloc.h – Dynamic memory allocation
Conio.h – Clearing the screen and character input
1
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Object File:
file by combining the object files together
2
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Binary Executable File:
2
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
C Tokens:
2
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
2
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Character set in C:
2
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
2
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
2
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
2
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
2
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
2
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
3
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
3
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
3
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
3
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
KEY WORDS
3
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
3
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
3
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
3
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
3
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
CLASSIFICATION :DATA TYPE
3
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Data type | Keywords Used | Size in bytes | Range | Use |
Character | char | 1 | -128 to 127 | To store character |
Integer | int | 2 | -32768 to 32767 | To store integer number |
Floating Point | float | 4 | 3.4E-38 to 3.4E+38 | To store decimal number |
Double | double | 8 | 1.7E-308 to 1.7E308 | To store big floating-point number |
Valueless | void | 0 | Valueless | - |
4
Basic Data Types
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Examples - Character
#include <stdio.h> void main()
{
char c = 'b';
printf("The character value is: %c \n", c);
}
4
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Example - Integer
#include <stdio.h> void main()
{
int i = 5;
printf("The integer value is: %d \n", i);
}
4
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Examples - Float
#include <stdio.h>
void main()
{
float f = 7.2357;
printf("The float value is: %f \n", f);
}
4
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Example – Double
#include <stdio.h> void main()
{
double d = 71.2357455;
printf("The double value is: %lf \n", d);
}
4
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
4
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
4
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
4
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Variables Types
Variables
Numeric Variable
4
Character Variables
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
VARIABLES
4
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
5
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
CONSTANT
A constant is a value or an identifier whose value cannot be altered in a program. For example: 1, 2.5
An identifier also can be defined as a constant. eg. const double PI = 3.14
Here, value of PI is a constant.
5
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Constant Declaration
const float pi = 3.14;
define.
#define PI 3.14159
When the preprocessor reformats the program to be compiled by the compiler, it replaces each defined name with its corresponding value wherever it is found in the source program.
5
3/19/2025
/skit.org.in
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
TYPES OF CONSTANTS
Integer constant eg: 1
Floating-point
constant eg: 2.5 Character constant eg: ’a’
String constant eg:
”good”
5
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Input/Output in C
5
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
STREAMS
memory representation.
Keyboard
Data
Monitor
Input text stream
Data
Input text stream
Streams in a C program
Text Stream
Binary Stream
5
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Data I/O
5
3/19/2025
/skit.org.in
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
scanf() - input function
printf() - output function
5
Formatted Input and Output Functions
scanf() - input function
Formatted Input and Output Functions
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Printf()
Output function
General form: printf(“control_string or format
specifier”,variable1,variable2, variable3,...);
Example:
printf(“The numbers are %d and
%d”,number1,number2);
5
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Format Specifiers
5
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Parts of conversion specifier field for printf()
precision indicates the number of characters used after the decimal point. The precision option is only used with floats or strings.
Default precision value is 6
•
The percent sign and conversion code are required
Other modifiers such as width and precision are optional.
width specifies the total number of characters used to display the value
6
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Size modifiers used in printf()
6
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Examples
6
printf(“number=%3d\n”, 10);
printf(“number=%2d\n”, 10);
printf(“number=%1d\n”, 10);
printf(“number=%7.2f\n”, 5.4321);
printf(“number=%.2f\n”, 5.4391);
printf(“number=%.9f\n”, 5.4321);
printf(“number=%f\n”, 5.4321);
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Example
# include <stdio.h>
int main()
{
printf(“number=%3d\n”, 10);
printf(“number=%2d\n”, 10);
printf(“number=%1d\n”, 10);
printf(“number=%7.2f\n”, 5.4321);
printf(“number=%.2f\n”, 5.4391);
printf(“number=%.9f\n”, 5.4321);
printf(“number=%f\n”, 5.4321);
}
6
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Flag | Meaning |
- | Left justify the display |
+ | Display positive or negative sign of value |
Space | Display space if there is no sign |
0 | Pad with leading zeros |
# | Use alternate form of specifier |
6
Flag characters used in printf()
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Example
printf(“number=%06.1f\n”, 5.5);
printf(“%+6.1f=number\n”, 5.5);
6
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Example with strings
#include<stdio.h> int main()
{
printf(“%s”,“hello”);
printf(“\n%3s”,“hello”);
printf(“\n%10s”,“hello”);
printf(“\n%-10s”,“hello”);
printf(“\n%10.3s”,“hello”); return 0; }
}
6
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
scanf()
General form:
scanf(“control_string”, variable1_address, variable2_address,...);
scanf() returns the number of input fields successfully scanned, converted, and stored
Example: scanf(“%d%d”,&num1,&num2);
6
3/19/2025
/skit.org.in
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
gets() getchar() getch() getche()
Output functions puts() putchar() putch() puts()
6
Input functions
•
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
Output:
Enter a character k c = k
6
getchar()
Reads a single character from the input data stream; but does not return the character to the program until the ‘\n’ ( or ) enter key is pressed.
Syntax:-
variable = getchar();
Example:- char c;
c = getchar();
Program #include<stdio.h> void main()
{
char c;
printf(“Enter a character”); c=getchar();
printf(“c = %c ”,c);
}
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
getch() and getche()
These functions read any alphanumeric character from the standard input device
The character entered is not displayed by the getch() function until enter is pressed
The getche() accepts and displays the character.
The getch() accepts but does not display the character. Syntax:
getche();
getch();
7
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
getch() and getche() - Program
#include<stdio.h> void main()
{
printf(“Enter two alphabets:”); getche();
getch();
}
Output
Enter two alphabets a b a
7
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
putchar()
7
This function prints one character on the screen at a time which is read by standard input.
Syntax:
putchar( variable name); Example:
char c= ‘c’; putchar(c);
#include<stdio.h> void main()
{
char ch;
printf(“Enter a character: ”); scanf(“%c”, &ch); putchar(ch);
}
Output
Enter a character: r r
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
putch()
This function prints any alphanumeric character taken by the standard input device
Syntax:
putch( variable name);
7
Example: char c= ‘c’; putch(c);
Program #include<stdio.h> void main()
{
char ch;
printf(“Press any key to continue”); ch = getch();
printf(“ you pressed:”); putch(ch);
}
Output:
Press any key to continue You pressed : e
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
3/19/2025
/skit.org.in
puts()
This function prints the string or character array.
It is opposite to gets()
Syntax
char str[length of string in number];
gets(str); puts(str);
7
SriKrishnaInstituteofTechnology
(ApprovedbyAICTE,AccreditedbyNAAC,AffiliatedtoVTU,Karnataka)
gets()
String I/O
This function is used for
accepting any string( stream of characters) until enter key is
pressed
Syntax
char str[length of string in number];
gets(str); Example:
char name[15]; gets(name);
3/19/2025
/skit.org.in
7
Program #include<stdio.h> void main()
{
char ch[30];
printf(“Enter the string:”); gets(ch);
printf(“Entered string: %s”, ch);
}
Output:
Enter the string: Use of data! Entered string: Use of data!