Data Transformation
Programs transform data from one form to another ◦ Input data → Output data ◦ Stimulus → Response Programming languages store and process data in various ways depending on the type of the data; consequently, all data read, processed, or written by a program must have a type Two distinguishing characteristics of a programming language are the data types it supports and the operations on those data types
1. Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
PAPER NAME: PROGRAMMING IN C
PAPER CODE: BCA 105
CLASS: BCA Ist Semester
2. UNIT-I Data Types
Data Transformation
Programs transform data from one form to another
◦ Input data → Output data
◦ Stimulus → Response
Programming languages store and process data in various ways depending on
the type of the data; consequently, all data read, processed, or written by a
program must have a type
Two distinguishing characteristics of a programming language are the data
types it supports and the operations on those data types
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
3. A Data Type
• A data type is
– A set of values AND
– A set of operations on those values
• A data type is used to
– Identify the type of a variable when the variable is declared
– Identify the type of the return value of a function
– Identify the type of a parameter expected by a function
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
4. A Data Type (continued)
• When the compiler encounters a declaration for a variable, it sets up a memory
location for it
• An operator used on a variable or variables is legal only if
– The operator is defined in that programming language for a variable of that
type
– The variable or variables involved with the operator are of the same or
compatible types
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
5. Rules for Constructing Identifiers
Capital letters A-Z, lowercase letters a-z, digits 0-9, and the underscore
character
First character must be a letter or underscore
Usually only the first 32 characters are significant
There can be no embedded blanks
Keywords cannot be used as identifiers
Identifiers are case sensitive
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
6. Two Classifications of Data Types
• Built-in data types
– Fundamental data types (int, char, double, float, void, pointer)
– Derived data types (array, string, structure)
• Programmer-defined data types
– Structure
– Union
– Enumeration
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
7. Basic Data Types
• Integral Types
– Integers are stored in various sizes. They can be signed or unsigned.
– Example
Suppose an integer is represented by a byte (8 bits). Leftmost bit is sign
bit. If the sign bit is 0, the number is treated as positive.
Bit pattern 01001011 = 75 (decimal).
The largest positive number is 01111111 = 27 – 1 = 127.
Negative numbers are stored as two’s complement or as one’s
complement.
-75 = 10110100 (one’s complement).
-75 = 10110101 (two’s complement).
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
8. 8
Basic Data Types
• Integral Types
– char Stored as 8 bits. Unsigned 0 to 255.
Signed -128 to 127.
– short int Stored as 16 bits. Unsigned 0 to 65535.
Signed -32768 to 32767.
– int Same as either short or long int.
– long int Stored as 32 bits. Unsigned 0 to 4294967295.
Signed -2147483648 to 2147483647
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
9. Basic Data Types
• Floating Point Numbers
– Floating point numbers are rational numbers. Always signed numbers.
– float Approximate precision of 6 decimal digits .
• Typically stored in 4 bytes with 24 bits of signed mantissa and 8
bits of signed exponent.
– double Approximate precision of 14 decimal digits.
• Typically stored in 8 bytes with 56 bits of signed mantissa and 8
bits of signed exponent.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
10. Constants
• Numerical Constants
– Constants like 12, 253 are stored as int type. No decimal point.
– 12L or 12l are stored as long int.
– 12U or 12u are stored as unsigned int.
– 12UL or 12ul are stored as unsigned long int.
– Numbers with a decimal point (12.34) are stored as double.
– Numbers with exponent (12e-3 = 12 x 10-3 ) are stored as double.
– 12.34f or 1.234e1f are stored as float.
– These are not valid constants:
25,000 7.1e 4 $200 2.3e-3.4 etc.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
11. Constants
• Character and string constants
– ‘c’ , a single character in single quotes are stored as char.
Some special character are represented as two characters in single
quotes.
‘n’ = newline, ‘t’= tab, ‘’ = backlash, ‘”’ = double quotes.
Char constants also can be written in terms of their ASCII code.
‘060’ = ‘0’ (Decimal code is 48).
– A sequence of characters enclosed in double quotes is called a string
constant or string literal. For example
“Charu”
“A”
“3/9”
“x = 5”
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
12. Variables
• Naming a Variable
– Must be a valid identifier.
– Must not be a keyword
– Names are case sensitive.
– Variables are identified by only first 32 characters.
– Library commonly uses names beginning with _.
– Naming Styles: Uppercase style and Underscore style
– lowerLimit lower_limit
– incomeTax income_tax
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
13. Declarations
• Declaring a Variable
– Each variable used must be declared.
– A form of a declaration statement is
data-type var1, var2,…;
– Declaration announces the data type of a variable and allocates
appropriate memory location. No initial value (like 0 for integers)
should be assumed.
– It is possible to assign an initial value to a variable in the declaration
itself.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
14. Derived Data Types
• Array – a finite sequence (or table) of variables of the same data type
• String – an array of character variables
• Structure – a collection of related variables of the same and/or
different data types. The structure is called a record and the variables
in the record are called members or fields
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
15. Derived Data Types
• Array – a finite sequence (or table) of variables of the same data type
• String – an array of character variables
• Structure – a collection of related variables of the same and/or
different data types. The structure is called a record and the variables
in the record are called members or fields
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
16. UNIT-II Arrays
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
• An array is a collection of data elements that are of the same type (e.g.,
a collection of integers, collection of characters, collection of doubles).
17. Arrays
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
• 1-dimensional array.
• 3-dimensional array (3rd dimension is the day).
18. Array Applications
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
• Given a list of test scores, determine the maximum and minimum scores.
• Read in a list of student names and rearrange them in alphabetical order (sorting).
• Given the height measurements of students in a class, output the names of those
students who are taller than average.
19. Array Declaration
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
• Syntax:
<type> <arrayName>[<array_size>]
Ex. int Ar[10];
• The array elements are all values of the type <type>.
• The size of the array is indicated by <array_size>, the
number of elements in the array.
• <array_size> must be an int constant or a constant
expression. Note that an array can have multiple dimensions.
20. Array Declaration
// array of 10 uninitialized ints
int Ar[10];
-- -- ----Ar -- -- ---- -- --
4 5 630 2 8 971
0 1 2 3 4 5
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
21. Subscripting
• Declare an array of 10 integers:
int Ar[10]; // array of 10 ints
• To access an individual element we must apply a subscript
to array named Ar.
– A subscript is a bracketed expression.
• The expression in the brackets is known as the index.
– First element of array has index 0.
Ar[0]
– Second element of array has index 1, and so on.
Ar[1], Ar[2], Ar[3],…
– Last element has an index one less than the size of the array.
Ar[9]
• Incorrect indexing is a common error.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
22. Subscripting
// array of 10 uninitialized ints
int Ar[10];
Ar[3] = 1;
int x = Ar[3];
-- -- 1--Ar -- -- ---- -- --
4 5 630 2 8 971
Ar[4] Ar[5] Ar[6]Ar[3]Ar[0] Ar[2] Ar[8] Ar[9]Ar[7]Ar[1]
1
-- -- --
--
--
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
23. Initializing arrays with random values
The following loop initializes the array myList with random values
between 0 and 99:
for (int i = 0; i < ARRAY_SIZE; i++)
{
myList[i] = rand() % 100;
}
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
24. Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Semester: FIRST Semester
Name of the Subject:
Programming in C Language
STRUCTURS & UNIONS
25. UNIT-III Structures
•Compound data:
•A date is
– an int month and
– an int day and
– an int year
•Unlike Java, C doesn’t automatically
define functions for initializing and
printing …
struct ADate {
int month;
int day;
int year;
};
struct ADate date;
date.month = 1;
date.day = 18;
date.year = 2018;
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
26. 26
Typedef
• Mechanism for creating new type names
– New names are an alias for some other type
– May improve clarity and/or portability of the program
typedef long int64_t;
typedef struct ADate {
int month;
int day;
int year;
} Date;
int64_t i = 100000000000;
Date d = { 1, 18, 2018 };
Overload existing type
names for clarity and
portability
Simplify complex type
names
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
27. Constants
• Allow consistent use of the same constant throughout the program
– Improves clarity of the program
– Reduces likelihood of simple errors
– Easier to update constants in the program
int array[10];
for (i=0; i<10;
i++) {
…
}
#define SIZE 10
int array[SIZE];
for (i=0;
i<SIZE; i++) {
…
}
Preprocess
or
directive
Constant names are
capitalized by convention
Define once,
use throughout
the program
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
28. Structure Representation & Size
•sizeof(struct …) =
• sum of sizeof(field)
•+ alignment padding
Processor- and compiler-specific
6261 EF BE AD DE
c1 c2 ipadding
struct CharCharInt {
char c1;
char c2;
int i;
} foo;
foo.c1 = ’a’;
foo.c2 = ’b’;
foo.i = 0xDEADBEEF;
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
29. Arrays of Structures
Date birthdays[NFRIENDS];
bool
check_birthday(Date today)
{
int i;
for (i = 0; i < NFRIENDS; i++) {
if ((today.month == birthdays[i].month) &&
(today.day == birthdays[i].day))
return (true);
return (false);
}
ConstantArray declaration
Array index, then
structure field
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
30. Pointers to Structures
Date
create_date1(int month,
int day,
int year)
{
Date d;
d.month = month;
d.day = day;
d.year = year;
return (d);
}
void
create_date2(Date *d,
int month,
int day,
int year)
{
d->month = month;
d->day = day;
d->year = year;
}
Copies date
Pass-by-reference
Date today;
today = create_date1(1, 18, 2018);
create_date2(&today, 1, 18, 2018);
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
31. Pointers to Structures (cont.)
void
create_date2(Date *d,
int month,
int day,
int year)
{
d->month = month;
d->day = day;
d->year = year;
}
void
fun_with_dates(void)
{
Date today;
create_date2(&today, 1, 18, 2018);
}
today.month:
today.day:
today.year:
0x1000
0x1004
0x1008
month: 1
day: 18
year: 2018
0x30A0
0x30A4
0x30A8
d: 0x10000x3098
1
18
2018
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
32. Pointers to Structures (cont.)
void
create_date2(Date *d,
int month,
int day,
int year)
{
d->month = month;
d->day = day;
d->year = year;
}
void
fun_with_dates(void)
{
Date today;
create_date2(&today, 1, 18, 2018);
}
today.month:
today.day:
today.year:
0x1000
0x1004
0x1008
month: 1
day: 18
year: 2018
0x30A0
0x30A4
0x30A8
d: 0x10000x3098
1
18
2018
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
33. Pointers to Structures (cont.)
Date *
create_date3(int month,
int day,
int year)
{
Date *d;
d->month = month;
d->day = day;
d->year = year;
return (d);
}
What is d pointing to?!?!
(more on this later)
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
34. Unions
•Choices:
•An element is
– an int i or
– a char c
•sizeof(union …) =
maximum of sizeof(field)
EF BE AD DE
c
i
padding
union AnElt {
int i;
char c;
} elt1, elt2;
elt1.i = 4;
elt2.c = ’a’;
elt2.i = 0xDEADBEEF;
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
35. Unions
• A union value doesn’t “know” which case it contains
union AnElt {
int i;
char c;
} elt1, elt2;
elt1.i = 4;
elt2.c = ’a’;
elt2.i = 0xDEADBEEF;
if (elt1 currently has a char) …
How should your program keep track
whether elt1, elt2 hold an int or
a char?
?
?
Basic answer: Another variable holds
that info
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
36. Tagged Unions
• Tag every value with its case
• I.e., pair the type info together with the union
• Implicit in Java, Scheme, ML, …
Enum must be external to struct,
so constants are globally visible.
Struct field must be named.
enum Union_Tag { IS_INT, IS_CHAR };
struct TaggedUnion {
enum Union_Tag tag;
union {
int i;
char c;
} data;
};
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
37. UNIT-IV Strings
• C implements the string data structure using arrays of type char.
• You have already used the string extensively.
– printf(“This program is terminated!n”);
– #define ERR_Message “Error!!”
• Since string is an array, the declaration of a string is the same as declaring a
char array.
– char string_var[30];
– char string_var[20] = “Initial value”;
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
38. Memory Storage for a String
• The string is always ended with a null character ‘0’.
• The characters after the null character are ignored.
• e.g., char str[20] = “Initial value”;
n i t i a l v a l u e ? ? …I 0
[0] [13]
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
39. Arrays of Strings
• An array of strings is a two-dimensional array of characters in which each
row is one string.
– char names[People][Length];
– char month[5][10] = {“January”, “February”, “March”, “April”,
“May”};
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
40. Input/Output of a String
• The placeholder %s is used to represent string arguments in printf and scanf.
– printf(“Topic: %sn”, string_var);
• The string can be right-justified by placing a positive number in the
placeholder.
– printf(“%8s”, str);
• The string can be left-justified by placing a negative number in the
placeholder.
– Printf(“%-8s”, str);
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
41. Right and Left Justification of Strings
The “%8s” placeholder displays a string which is right-justified and in 8-
columns width.
If the actual string is longer than the width, the displayed field is expanded
with no padding.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
42. Execution of scanf ("%s", dept);
• Whenever encountering a white space, the scanning stops and scanf
places the null character at the end of the string.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
43. String Library Functions
• The string can not be copied by the assignment operator ‘=’.
– e..g, “str = “Test String”” is not valid.
• C provides string manipulating functions in the “string.h” library.
– The complete list of these functions can be found in Appendix B of the
textbook.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
44. Some String Functions from String.h
Function Purpose Example
strcpy Makes a copy of a string strcpy(s1, “Hi”);
strcat Appends a string to the end of
another string
strcat(s1, “more”);
strcmp Compare two strings alphabetically strcmp(s1, “Hu”);
strlen Returns the number of characters in
a string
strlen(“Hi”) returns 2.
strtok Breaks a string into tokens by
delimiters.
strtok(“Hi, Chao”, “ ,”);
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
45. Functions strcpy and strncpy
• Function strcpy copies the string in the second argument into the first
argument.
– e.g., strcpy(dest, “test string”);
– The null character is appended at the end automatically.
– If source string is longer than the destination string, the overflow
characters may occupy the memory space used by other variables.
• Function strncpy copies the string by specifying the number of characters to
copy.
– You have to place the null character manually.
– e.g., strncpy(dest, “test string”, 6); dest[6] = ‘0’;
– If source string is longer than the destination string, the overflow
characters are discarded automatically.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
46. Functions strcat and strlen
• Functions strcat and strncat concatenate the fist string argument with the
second string argument.
– strcat(dest, “more..”);
– strncat(dest, “more..”, 3);
• Function strlen is often used to check the length of a string (i.e., the number of
characters before the fist null character).
– e.g., dest[6] = “Hello”;
strncat(dest, “more”, 5-strlen(dest));
dest[5] = ‘0’;
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
47. Input/Output of Characters and Strings
• The stdio library provides getchar function which gets the next character from
the standard input.
– “ch = getchar();” is the same as “scanf(“%c”, &ch);”
– Similar functions are putchar, gets, puts.
• For IO from/to the file, the stdio library also provides corresponding functions.
– getc: reads a character from a file.
– Similar functions are putc, fgets, fputs.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
48. The sprintf and sscanf Functions
• The sprintf function substitutes values for placeholders just as printf does
except that it stores the result into a character array
– sprintf(s, “%d%d%d”, mon, day, year);
• The sscanf function works exactly like scanf except that it takes data from the
string as its input argument.
– sscanf(“ 11 22.2 Hello”, “%d%lf%s”, &num, &val, word);
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)