Arrays
Arrays
Here, we declared an array, i, of integer type. And its size is 20. Meaning, it can
hold 20 integer values.
float marks[10];
Here, we declared an array, marks, of floating-point type. And its size is 10.
Meaning, it can hold 10 floating-point values.
Access array elements
You can access elements of an array by indices.
Suppose you declared an array mark as above. The first element is mark[0], the
second element is mark[1] and so on.
● Arrays have 0 as the first index, not 1. In this example, mark[0] is the first
element.
● If the size of an array is n, to access the last element, the n-1 index is used. In
this example, mark[4]
● Suppose the starting address of mark[0] is 2120d. Then, the address of the
mark[1] will be 2124d. Similarly, the address of mark[2] will be 2128d and so
on.
This is because the size of a float is 4 bytes.
How to initialize an array
It is possible to initialize an array during declaration. For example,
Here, we haven't specified the size. However, the compiler knows its size is 5 as
we are initializing it with 5 elements.
Changing value of array elements
int mark[5] = {19, 10, 8,17,9};
mark[2] = 3;
mark[4]= 0;
Input and Output Array elements
// take input and store it in the 3rd element
scanf("%d", &mark[2]);
// take input and store it in the ith element
scanf("%d", &mark[i-1]);
// print the first element of the array
printf("%d", mark[0]);
// print the third element of the array
printf("%d", mark[2]);
// print ith element of the array
printf("%d", mark[i-1]);
Problems
● WAP to take 5 values from the user and store them in an array and print the
results from the array.
● WAP to find the average of n numbers using array.
● WAP to find the largest among three numbers
● WAP to find largest element in an array.
What is the sizeof(char) in a 32-bit C compiler?
a) 1 bit
b) 2 bits
c) 1 Byte
d) 2 Bytes
What is #include <stdio.h>?
What will happen if the following C code is
a) Preprocessor directive executed?
b) Inclusion directive 1. #include <stdio.h>
c) File inclusion directive 2. int main()
3. {
d) None of the mentioned 4. int main = 3;
Which of the following are C preprocessors? 5. printf("%d", main);
6. return 0;
a) #ifdef 7. }
b) #define
a) It will cause a compile-time error
c) #endif b) It will cause a run-time error
c) It will run without any error and prints 3
d) all of the mentioned d) It will experience infinite looping
What will be the output of the What is the difference between the following 2 C codes?
following C code?
1. #include <stdio.h> //Program 1
1. #include <stdio.h> 2. int main()
2. int main() 3. {
3. { 4. int d, a = 1, b = 2;
4. signed char chr; 5. d = a++ + ++b;
5. chr = 128; 6. printf("%d %d %d", d, a, b);
6. printf("%d\n", chr); 7. }
7. return 0; 1. #include <stdio.h> //Program 2
2. int main()
8. }
3. {
a) 128 4. int d, a = 1, b = 2;
b) -128 5. d = a++ +++b;
6. printf("%d %d %d", d, a, b);
c) Depends on the compiler 7. }
d) None of the mentioned a) No difference as space doesn’t make any difference, values of
a, b, d are same in both the case
b) Space does make a difference, values of a, b, d are different
c) Program 1 has syntax error, program 2 is not
Problems
● WAP to input a character in a variable and display it using other variable.
● WAP to check whether two numbers are equal, greater or lesser than each
other.
● WAP to print the odd numbers from 1 to 20.
● WAP to check the prime number.
● WAP to calculate the factorial of a number.
● WAP to check whether a number is palindrome or not.
● Print the pattern:
1
12
123
1234
12345
MultiDimensional Array
In C programming, you can create an array of arrays. These arrays are known as
multidimensional arrays. For example, float x[3][4].
Here, x is a two-dimensional (2d) array. The array can hold 12 elements. You can
think the array as a table with 3 rows and each row has 4 columns.
Similarly, you can declare a three-dimensional (3d) array. For example,
Initialization of a 3d array
int test[2][3][4] = {
An infinite loop is useful for those applications that accept the user input and generate the
output continuously until the user exits from the application manually. In the following situations,
this type of loop can be used:
● All the operating systems run in an infinite loop as it does not exist after performing some
task. It comes out of an infinite loop only when the user manually shuts down the system.
● All the servers run in an infinite loop as the server responds to all the client requests. It
comes out of an indefinite loop only when the administrator shuts down the server
manually.
● All the games also run in an infinite loop. The game will accept the user requests until the
user exits from the game.
For loop (infinite)
Syntax: for(; ;)
1. #include <stdio.h>
2. int main()
3. {
4. for(;;)
5. {
6. printf("Hello World\n");
7. }
8. return 0;
9. }
While loop (infinite)
Syntax: while(1)
1. #include <stdio.h>
2. int main()
3. {
4. int i=0;
5. while(1)
6. {
7. i++;
8. printf("Value of i is :%d\n",i);
9. }
10. return 0;
11. }
Order of precedence with regards to operators in C
● Unary Operators (++,--,....)
● Mathematical Operators (* / % followed by + - )
● Relational Operators ( <, <=, >, >=) followed by (== and !=)
● Logical Operators (&& ||)
● Assignment Operator (=)
Questions
● What would happen to X in this expression: X += 15; (assuming the value of X is 5).
● In C language, the variables NAME, name, and Name are all the same. TRUE or
FALSE?
● What will be the outcome of the following conditional statement if the value of variable s
is 10? s >=10 && s < 25 && s!=12
● What does the format %10.2 mean when included in a printf statement?
● How do you access the values within an array?
● What is the difference between =value and ==value?
● What is the equivalent code of the following statement in WHILE LOOP format?
●
#include <stdio.h>
int main()
{
char a,b,c;
int d;
a='A';
b='4';
c=a+b;
d=a+b;
printf("C has value = %c\n",c);
printf("d has value = %d",d);
return 0;
}
ASCII
The full form of ASCII is the American Standard Code for information interchange. It is a
character encoding scheme used for electronics communication. Each character or a special
character is represented by some ASCII code, and each ascii code occupies 7 bits in
memory.
In C programming language, a character variable does not contain a character value itself
rather the ascii value of the character variable. The ascii value represents the character
variable in numbers, and each character variable is assigned with some number range from
0 to 127. For example, the ascii value of 'A' is 65.
In the above example, we assign 'A' to the character variable whose ascii value is 65, so 65
will be stored in the character variable rather than 'A'.
Example
#include <stdio.h>
int main()
{
char ch;
printf("Enter a character:");
scanf("%c",&ch); // user input
printf("\n The ascii value of the ch variable is : %d", ch);
return 0;
}
Strings
In C programming, a string is a sequence of characters terminated with a null
character \0. For example:
char c[ ] = “c string”
printf("Enter name: "); Thus, the name in scanf() already points to the
address of the first element in the string, which
scanf("%s", name); is why we don't need to use &
#include <stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
scanf("%[^\n]",name);