KEMBAR78
Console I/o & basics of array and strings.pptx
CONSOLE I/O & BASICS OF
ARRAY AND STRINGS
Mr. Prasenjit More
Mr . Rajesh Walke
BASICS OF ARRAY AND STRINGS
• TWO DIMENSIONAL ARRAY
• ARRAY OF STRINGS
• MULTIDIMENSIONAL ARRAYS
• ARRAY INITIALIZATION
• VARIABLE LENGTH ARRAYS
CONSOLE I/O
• READING AND WRITING CHARACTERS
• READING AND WRITING STRINGS
• FORMATTED CONSOLE I/O
• PRINTF()
• SCANF()
 Two dimensional Array
• The two-dimensional array can be defined as an array of arrays. The 2D array is organized as matrices which can be
represented as the collection of rows and columns. However, 2D arrays are created to implement a relational database
lookalike data structure. It provides ease of holding the bulk of data at once which can be passed to any number of
functions wherever required.
• Syntax : data_type array_name[rows][columns];
 Array of strings
◦ A string is a 1-D array of characters, so an array of strings is a 2-D array of characters. Just like we can create a 2-D array
of int, float etc; we can also create a 2-D array of character or array of strings. Here is how we can declare a 2-D array of
characters.
◦ It is important to end each 1-D array by the null character, otherwise, it will be just an array of characters. We can't use
them as strings.
 Multidimensional Arrays
• C programming language allows multidimensional arrays. Here is the general form of a multidimensional array declaration
−
• type name [size1], [size2], ………., [sizeN];
• For example, the following declaration creates a three dimensional integer array −
• int threedim [5],[10],[4];
 Array Initialization
• Different ways to Initialize all members of an array to the same value in C
• Some of the different ways in which all elements of an array can be initialized to the same value:
• Initializer List
• Designer Initializer
• Macros
• Using For Loop
 Variable Length Arrays
• Variable length arrays are also known as runtime sized or variable sized arrays. The size of such arrays is defined at run-
time.
• Variably modified types include variable length arrays and pointers to variable length arrays. Variably changed types must be
declared at either block scope or function prototype scope.
• Variable length arrays is a feature where we can allocate an auto array (on stack) of variable size. It can be used in a typedef
statement. C supports variable sized arrays from C99 standard. For example, the below program compiles and runs fine in
C.
 Reading and writing characters
In C programming language the reading and writing characters are as follows −
• The simplest of the console I/O functions are getche (), which reads a character from the keyboard, and putchar (), which
prints a character to the screen.
• The getche () function works on until a key is pressed and then, returns its value. The key pressed is also echoed to the
screen automatically.
• The putchar () function will write its character argument to the screen at the current cursor position.
• The header file for getche () and putchar () is in CONIO.H
 Reading and writing strings
• Format conversion ”%s” can be used in scanf for reading strings not containing white spaces: scanf("%s", str)
• ’&’ not required before str as it is a pointer.
• C string library provides gets(str) to read a string.
• It does not skip white spaces like scanf does.
• But, scanf and gets have no way to know the size of the array in advance.
• So potentially, they could attempt to store character past the array.
• scanf can use "%.ns" conversion to avoid this problem.
• But gets is dangerous.
 Formatted Console I/O
• C provides standard functions scanf() and printf(), for performing formatted input and output .These functions accept, as
parameters, a format specification string and a list of variables.
• The function printf() is used for formatted output to standard output based on a format specification. The format
specification string, along with the data to be output, are the parameters to the printf() function.
• Syntax : printf (format, data1, data2,……..);
• The function scanf() is used for formatted input from standard input and provides many of the conversion facilities of the
function printf().
• Syntax : scanf (format, num1, num2,……);
 Printf() function
◦ In C programming language, printf() function is used to print the (“character, string, float, integer, octal and hexadecimal
values”) onto the output screen.
◦ We use printf() function with %d format specifier to display the value of an integer variable.
◦ Similarly, %c is used to display character, %f for float variable, %s for string variable, %lf for double and %x for
hexadecimal variable
◦ To generate a newline,we use “n” in C printf() statement.
 Scanf() function
• In C programming language, scanf() function is used to read character, string, numeric data from keyboard
• Consider below example program where user enters a character. This value is assigned to the variable “ch” and then
displayed.
• Then, user enters a string and this value is assigned to the variable “str” and then displayed.
Thank You !

Console I/o & basics of array and strings.pptx

  • 1.
    CONSOLE I/O &BASICS OF ARRAY AND STRINGS Mr. Prasenjit More Mr . Rajesh Walke
  • 2.
    BASICS OF ARRAYAND STRINGS • TWO DIMENSIONAL ARRAY • ARRAY OF STRINGS • MULTIDIMENSIONAL ARRAYS • ARRAY INITIALIZATION • VARIABLE LENGTH ARRAYS
  • 3.
    CONSOLE I/O • READINGAND WRITING CHARACTERS • READING AND WRITING STRINGS • FORMATTED CONSOLE I/O • PRINTF() • SCANF()
  • 4.
     Two dimensionalArray • The two-dimensional array can be defined as an array of arrays. The 2D array is organized as matrices which can be represented as the collection of rows and columns. However, 2D arrays are created to implement a relational database lookalike data structure. It provides ease of holding the bulk of data at once which can be passed to any number of functions wherever required. • Syntax : data_type array_name[rows][columns];
  • 5.
     Array ofstrings ◦ A string is a 1-D array of characters, so an array of strings is a 2-D array of characters. Just like we can create a 2-D array of int, float etc; we can also create a 2-D array of character or array of strings. Here is how we can declare a 2-D array of characters. ◦ It is important to end each 1-D array by the null character, otherwise, it will be just an array of characters. We can't use them as strings.
  • 6.
     Multidimensional Arrays •C programming language allows multidimensional arrays. Here is the general form of a multidimensional array declaration − • type name [size1], [size2], ………., [sizeN]; • For example, the following declaration creates a three dimensional integer array − • int threedim [5],[10],[4];
  • 7.
     Array Initialization •Different ways to Initialize all members of an array to the same value in C • Some of the different ways in which all elements of an array can be initialized to the same value: • Initializer List • Designer Initializer • Macros • Using For Loop
  • 8.
     Variable LengthArrays • Variable length arrays are also known as runtime sized or variable sized arrays. The size of such arrays is defined at run- time. • Variably modified types include variable length arrays and pointers to variable length arrays. Variably changed types must be declared at either block scope or function prototype scope. • Variable length arrays is a feature where we can allocate an auto array (on stack) of variable size. It can be used in a typedef statement. C supports variable sized arrays from C99 standard. For example, the below program compiles and runs fine in C.
  • 9.
     Reading andwriting characters In C programming language the reading and writing characters are as follows − • The simplest of the console I/O functions are getche (), which reads a character from the keyboard, and putchar (), which prints a character to the screen. • The getche () function works on until a key is pressed and then, returns its value. The key pressed is also echoed to the screen automatically. • The putchar () function will write its character argument to the screen at the current cursor position. • The header file for getche () and putchar () is in CONIO.H
  • 10.
     Reading andwriting strings • Format conversion ”%s” can be used in scanf for reading strings not containing white spaces: scanf("%s", str) • ’&’ not required before str as it is a pointer. • C string library provides gets(str) to read a string. • It does not skip white spaces like scanf does. • But, scanf and gets have no way to know the size of the array in advance. • So potentially, they could attempt to store character past the array. • scanf can use "%.ns" conversion to avoid this problem. • But gets is dangerous.
  • 11.
     Formatted ConsoleI/O • C provides standard functions scanf() and printf(), for performing formatted input and output .These functions accept, as parameters, a format specification string and a list of variables. • The function printf() is used for formatted output to standard output based on a format specification. The format specification string, along with the data to be output, are the parameters to the printf() function. • Syntax : printf (format, data1, data2,……..); • The function scanf() is used for formatted input from standard input and provides many of the conversion facilities of the function printf(). • Syntax : scanf (format, num1, num2,……);
  • 12.
     Printf() function ◦In C programming language, printf() function is used to print the (“character, string, float, integer, octal and hexadecimal values”) onto the output screen. ◦ We use printf() function with %d format specifier to display the value of an integer variable. ◦ Similarly, %c is used to display character, %f for float variable, %s for string variable, %lf for double and %x for hexadecimal variable ◦ To generate a newline,we use “n” in C printf() statement.
  • 13.
     Scanf() function •In C programming language, scanf() function is used to read character, string, numeric data from keyboard • Consider below example program where user enters a character. This value is assigned to the variable “ch” and then displayed. • Then, user enters a string and this value is assigned to the variable “str” and then displayed.
  • 14.