SlideShare a Scribd company logo
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 !

More Related Content

PPTX
Unit I - 1R introduction to R program.pptx
PPSX
Esoft Metro Campus - Certificate in c / c++ programming
PPTX
4 Introduction to C.pptxSSSSSSSSSSSSSSSS
PPTX
C language
PPTX
PDF
C programming language
PPTX
PPTX
Basic Concepts of C Language.pptx
Unit I - 1R introduction to R program.pptx
Esoft Metro Campus - Certificate in c / c++ programming
4 Introduction to C.pptxSSSSSSSSSSSSSSSS
C language
C programming language
Basic Concepts of C Language.pptx

Similar to Console I/o & basics of array and strings.pptx (20)

PPTX
C programming basic concepts of mahi.pptx
PPTX
PPS_unit_2_gtu_sem_2_year_2023_GTUU.pptx
PPTX
cmp104 lec 8
PPTX
COm1407: Character & Strings
PPTX
C programming language tutorial
PDF
EC2311-Data Structures and C Programming
PPT
DOCX
PPTX
C for Engineers
PPTX
Review of C.pptx
PPTX
programming language in c&c++
PPTX
basic C PROGRAMMING for first years .pptx
PPT
270 1 c_intro_up_to_functions
PPT
270_1_CIntro_Up_To_Functions.ppt
PPT
270_1_CIntro_Up_To_Functions.ppt
PPT
Survey of programming language getting started in C
PPTX
Unit No 2.pptx Basic s of C Programming
PPTX
Fundamentals of Programming Constructs.pptx
PPTX
c unit programming arrays in detail 3.pptx
C programming basic concepts of mahi.pptx
PPS_unit_2_gtu_sem_2_year_2023_GTUU.pptx
cmp104 lec 8
COm1407: Character & Strings
C programming language tutorial
EC2311-Data Structures and C Programming
C for Engineers
Review of C.pptx
programming language in c&c++
basic C PROGRAMMING for first years .pptx
270 1 c_intro_up_to_functions
270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt
Survey of programming language getting started in C
Unit No 2.pptx Basic s of C Programming
Fundamentals of Programming Constructs.pptx
c unit programming arrays in detail 3.pptx
Ad

Recently uploaded (20)

PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Geodesy 1.pptx...............................................
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
additive manufacturing of ss316l using mig welding
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
CYBER-CRIMES AND SECURITY A guide to understanding
Embodied AI: Ushering in the Next Era of Intelligent Systems
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Lecture Notes Electrical Wiring System Components
Strings in CPP - Strings in C++ are sequences of characters used to store and...
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Geodesy 1.pptx...............................................
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Model Code of Practice - Construction Work - 21102022 .pdf
Arduino robotics embedded978-1-4302-3184-4.pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
bas. eng. economics group 4 presentation 1.pptx
CH1 Production IntroductoryConcepts.pptx
additive manufacturing of ss316l using mig welding
Lesson 3_Tessellation.pptx finite Mathematics
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Ad

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 ARRAY AND STRINGS • TWO DIMENSIONAL ARRAY • ARRAY OF STRINGS • MULTIDIMENSIONAL ARRAYS • ARRAY INITIALIZATION • VARIABLE LENGTH ARRAYS
  • 3. CONSOLE I/O • READING AND WRITING CHARACTERS • READING AND WRITING STRINGS • FORMATTED CONSOLE I/O • PRINTF() • SCANF()
  • 4.  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];
  • 5.  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.
  • 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 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.
  • 9.  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
  • 10.  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.
  • 11.  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,……);
  • 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.