0% found this document useful (0 votes)
4 views

Lec6

The document provides an overview of scalar variables and arrays in C programming, detailing their definitions, initialization, and operations. It covers array declaration, accessing elements, and performing various operations such as incrementing, adding, and swapping values. Additionally, it discusses string handling, I/O streams, and the use of functions like strcpy for string manipulation.

Uploaded by

apksingh011611
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lec6

The document provides an overview of scalar variables and arrays in C programming, detailing their definitions, initialization, and operations. It covers array declaration, accessing elements, and performing various operations such as incrementing, adding, and swapping values. Additionally, it discusses string handling, I/O streams, and the use of functions like strcpy for string manipulation.

Uploaded by

apksingh011611
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Dr.

Supriyo Mandal,
Ph.D. (IIT Patna)
Postdoc (ZBW, University of Kiel, Germany)
A scalar variable is a single variable whose stored value is an atomic data
type.
An array is a collection of individual data elements that is ordered, fixed in
size, and homogeneous.
An array is considered to be a derived data type.
Array enables the storing and manipulation of potentially huge quantities of
data.
Variables can be assigned values during declaration like the following example.
int x = 7;
Array initialization statements as shown.
(a) int A[10] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
9 8 7 6 5 4 3 2 1 0 ¨ values stored in array elements
0 1 2 3 4 5 6 7 8 9 ¨index values of array elements
(b) double a[5] = {3.67, 1.21, 5.87, 7.45, 9.12}
Automatic sizing While initializing, the size of a one dimensional array can be
omitted as shown.
int arr[] = {3,1,5,7,9};
Here, the C compiler will reduce the size of the array from the initialization
statement.
Integer array example:
int age [5];
int age[5]={0, 1, 2, 3, 4};
age[0]; /*0 is accessed*/
age[1]; /*1 is accessed*/
Array declaration syntax: age[2]; /*2 is accessed*/
data_type arr_name [arr_size];
Array initialization syntax: Character array example:
data_type arr_name [arr_size]=(value1, char str[10];
value2, value3,….); char str[10]={‘H’,‘a’,‘i’};
Array accessing syntax: (or)
arr_name[index]; char str[0] = ‘H’;
char str[1] = ‘a’;
char str[2] = ‘i;
str[0]; /*H is accessed*/
str[1]; /*a is accessed*/
str[2]; /*i is accessed*/
#include<stdio.h>

int main()
{
int i;
value of arr[0] is 10
int arr[5] = {10,20,30,40,50};
value of arr[1] is 20
for (i=0;i<5;i++)
value of arr[2] is 30
{ value of arr[3] is 40
// Accessing each variable value of arr[4] is 50
printf("value of arr[%d] is %d \n", i, arr[i]);
}

}
§ x and y are similar arrays (i.e., of the same data type, dimensionality, and size), then
assignment operations, comparison operations, etc., involving these two arrays must
be carried out on an element-by-element basis.

Determine total of all elements in a number array


§ Examples using the elements of an array named ‘numbers’ are shown here:
numbers [0] = 98;
numbers [1] = numbers [0] – 11
numbers [2] = 2 * (numbers [0] – 6);
numbers [3] = 79;
numbers [4] = (numbers [2] + numbers [3] – 3)/2;
total = numbers[0] + numbers[1] + numbers[2] + numbers[3] + numbers[4];
These operations include the following, for an array named ‘ar’.

• (a) To increment the ith element, the given statements can


be used.
ar[i]++;
ar[i] += 1;
ar[i] = ar[i] + 1;

• (b) To add n to the ith element, the following statements


may be used,
ar[i] += n;
ar[i] = ar[i] + n;

© Oxford University Press 2013. All rights reserved.


These operations include the following, for an array named ‘ar’.
• (e) To exchange the values in ar[i] and ar[k], a
‘temporary’ variable must be declared to hold one value,
and it should be the same data type as the array
elements being swapped.
int temp;
temp = ar[i];
/* save a copy of value in ar[i] */
ar[i] = ar[j];
/* copy value from ar[j] to ar[i] */
ar[j] = temp;
/* copy saved value of ar[i] to ar[j] */

© Oxford University Press 2013. All rights reserved.


Write a program to find largest element stored in an array.
§ Single operations, which involve entire arrays, are not permitted in C.
§ Neither can all elements of an array be set at once nor can one array be
assigned to another.
§ For an array of length L and data type X, the compiler allocates L* sizeof
(X) bytes of contiguous space in memory.
§ char = 1 byte; int = 2 bytes ; float = 4 bytes;
§ Note the arrays
char array_nr1[40]; 40*sizeof(char) = 40 bytes
int array_nr1[10]; 10*sizeof(int) = 20 bytes
int array_nr2[10]; 10*sizeof(int) = 20 bytes
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

The C compiler automatically places the '\0' at the end of the string
when it initializes the array.
char test[100] = "Null terminator will follow me";
sprintf(test,"Null terminator will follow me");
§ Strings can be declared like one-dimensional arrays.
§ For example,
char str[30];
char text[80];

§ An array formed by characters is a string in C.


§ The end of the string is marked with a the null character.
§ When the character array size is explicitly specified and the number of
initializers completely fills the array size, the null character is not
automatically appended to the array.

© Oxford University Press 2013. All rights reserved.


Initiation of a string
char s[]=“Hello, World”;
§ stdin, stdout, and stderr: Each C program has three I/O streams.
• The input stream is called standard-input (stdin); the output stream is
called standard-output (stdout); and the side stream of output
characters for errors is called standard error (stderr).
• Now one might think that calls to fprinf() and fscanf() differ
significantly from calls to printf() and scanf().
• fprintf() sends formatted output to a stream and fscanf() scans and
formats input from a stream.

© Oxford University Press 2013. All rights reserved.


• Since C never lets entire arrays to be assigned, the strcpy() function can be
used to copy one string to another.
• strcpy() copies the string pointed to by the second parameter into the
space pointed to by the first parameter.
• The entire string, including the terminating NUL, is copied and there is
no check that the space indicated by the first parameter is big enough.
• The given code shows the use of the strcpy(str1, str2) function.
#include <string.h>
char s1[] =“Hello, world!”;
char s2[20];
strcpy(s2, s1);
puts (s2);
Print a string using puts
#include <stdio.h>
int main () Accessing Two-Dimensional Array Elements
{
/* an array with 5 rows and 2 columns*/
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
int i, j;
/* output each array element's value */
for ( i = 0; i < 5; i++ ) // for rows
{
for ( j = 0; j < 2; j++ ) //for columns
{
printf("a[%d][%d] = %d\n", i,j, a[i][j] );
} a[0][0]: 0
} a[0][1]: 0
return 0; 0 0 a[1][0]: 1
a[1][1]: 2
} 1 2 a[2][0]: 2
} a[2][1]: 4
2 4
a[3][0]: 3
3 6 a[3][1]: 6
4 8 a[4][0]: 4
a[4][1]: 8
Take input from user and print the matrix
Addition of two matrices

You might also like