Programming in C
Programming in C
Ans1) Keywords and identifiers are two important concepts in C programming language. Keywords are
predefined, reserved words that have special meanings to the compiler. They are part of the syntax and
cannot be used as identifiers. For example, int, char, if, while, etc. are keywords in C12. Identifiers are
unique names that are assigned to variables, functions, structures, and other entities. They are used to
uniquely identify the entity within the program. For example, money, account Balance, is Even, etc. are
identifiers in C23.
There are some rules for naming identifiers in C, such as:
An identifier can include letters (a-z or A-Z), digits (0-9), and underscores (_).
An identifier cannot start with a digit or include any special characters except the underscore.
An identifier cannot be the same as a keyword.
An identifier must be unique in its namespace and case-sensitive.
An identifier can be of any length, but some compilers may have a limit23.
Ans2) There are five primary or primitive data types in C language: int, char, float, double, and void12.
These data types are used to store basic values, such as integers, characters, and real numbers. There are
also some modifiers, such as short, long, signed, and unsigned, that can be used with some of the primary
data types to change their size or range12.Besides the primary data types, there are also some derived or
secondary data types, such as arrays, pointers, structures, unions, and enumerations13. These data types
are used to store complex or composite values, such as collections, addresses, records, etc.
Ans3) In C programming, a control statement is a statement that alters the flow of execution of a
program. Control statements enable us to specify the flow of program control — that is, the order in which
the instructions in a program must be executed. They make it possible to make decisions, to perform tasks
repeatedly, or to jump from one section of code to another1. There are four types of control statements
in C:
1. Simple if Statement: This statement enables a programmer to choose various instruction sets on the basis
of the available condition. The instruction sets will only get executed when the evaluation of the condition
turns out to be true. Here is an example:
#include <stdio.h>
int main () {
int gfg = 9;
if (gfg < 10) {
printf("%d is less than 10", gfg);
}
return 0;
}
Programming in C
2. if-else Statement: The if-else statement is used to carry out a logical test and then take one of two
possible actions depending on the outcome of the test (i.e., whether the outcome is true or false). Here
is an example:
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// True if the remainder is 0
if (number%2 == 0) {
printf("%d is an even integer.",number);
} else {
printf("%d is an odd integer.",number);
}
return 0;
}
3. Nested if-else Statement: It is also possible to embed or to nest if-else statements one within the
other. Nesting is useful in situations where one of several different courses of action needs to be selected.
4. else-if Ladder: The if...else ladder allows you to check between multiple test expressions and execute
different statements. Here is an example:
int main() {
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
//checks if the two integers are equal.
if(number1 == number2) {
printf("Result: %d = %d",number1,number2);
}
//checks if number1 is greater than number2.
else if (number1 > number2) {
printf("Result: %d > %d", number1, number2);
}
//checks if both test expressions are false
else {
printf("Result: %d < %d",number1, number2);
}
return 0;
}
These are the basic types of if statements in C. Each of them serves a different purpose and can be used
based on the specific requirements of your program. Remember, the key to using these statements
effectively is understanding the logic behind your program’s requirements and implementing the correct
control statement accordingly.
Ans4) Here is the sample of code that will determine whether a given character is uppercase or lowercase
digit or symbol
int main() {
Programming in C
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if(ch >= 'A' && ch <= 'Z') {
printf("%c is an uppercase letter.\n", ch);
} else if(ch >= 'a' && ch <= 'z') {
printf("%c is a lowercase letter.\n", ch);
} else if(ch >= '0' && ch <= '9') {
printf("%c is a digit.\n", ch);
} else {
printf("%c is a symbol.\n", ch);
}
return 0;
}
This program first takes a character as input from the user. Then it checks whether the character is an
uppercase letter (by checking if it lies in the ASCII range for uppercase letters), a lowercase letter (by
checking if it lies in the ASCII range for lowercase letters), or a digit (by checking if it lies in the ASCII range
for digits). If the character does not fall into any of these categories, the program assumes it is a symbol.
The result is then printed to the console. Please note that this program does not account for non-English
characters or other special characters that may fall outside these ranges. It’s a simple demonstration of
how you can use ASCII values to categorize characters in C. You may need to adjust it based on your
specific needs.
Ans5) While Loop: A while loop is a control flow statement that allows code to be executed repeatedly
based on a given Boolean condition. The while loop can be thought of as a repeating if statement. The
loop will continue as long as the condition is true. If the condition is false at the start, the loop body might
not execute even once.
Here is the syntax and an example in C:
while (boolean condition) {
// loop statements...
}
// Example
int main () {
int i = 5;
while (i < 10) {
printf("GFG\\n");
i++;
}
return 0;
}
Ans6) Pointer: A pointer is a variable that holds the memory address of another variable1. In other words,
a pointer points to a location in memory where a variable is stored. Pointers are often used to manipulate
data directly in memory, which can be more efficient than working with copies of the data.
Here is an example of a pointer in C:
int var = 10; // A variable
int *ptr; // A pointer
ptr = &var; // The pointer stores the address of the variable
In this example, ptr is a pointer that stores the address of the variable var. We can access the value stored
at that address using *ptr.
Call by Value vs Call by Reference:
Call by Value: In the call by value method, the values of actual parameters are copied to the function’s
formal parameters. Any changes made inside functions are not reflected in the actual parameters of the
caller. Here’s an example in C:
void swapx (int x, int y) {
int t;
t = x;
x = y;
y = t;
printf("Inside Function:\nx = %d y = %d\n", x, y);
}
int main () {
int a = 10, b = 20;
swapx (a, b);
Programming in C
Ans7) Here is a simple C program that checks if a given number is prime or not:
// Function to check if a number is prime
bool isPrime(int n) {
if (n <= 1)
return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
Programming in C
if (isPrime(num))
printf("%d is a prime number.", num);
else
printf("%d is not a prime number.", num);
return 0;
}
In this program, the is Prime function checks if a number is prime by checking divisibility from 2 to the
square root of the number. If the number is divisible by any of these, it is not prime. The main function
reads a number from the user and uses the is Prime function to check if it’s prime or not.
Ans8) Here is a simple C program that checks if a given number is an Armstrong number or not:
// Function to check if a number is an Armstrong number
int isArmstrong(int num) {
int originalNum, remainder, n = 0;
double result = 0.0;
originalNum = num;
originalNum = num;
int main() {
int num;
printf("Enter a number: ");
Programming in C
scanf("%d", &num);
if (isArmstrong(num))
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
return 0;
}
In this program, the Armstrong function checks if a number is an Armstrong number. An Armstrong
number is a number that is equal to the sum of the cubes of its own digits. For example, 153 is an
Armstrong number since 1^3 + 5^3 + 3^3 = 153. The main function reads a number from the user and
uses the Armstrong function to check if it’s an Armstrong number or not.
Ans9) An array is a collection of items of the same data type stored at contiguous memory locations. It is
a simple and fast way of storing multiple values under a single name. For example, if you want to store
100 integers, you can create an array for it.
In C, we declare an array by specifying its name, the type of its elements, and the size of its dimensions4.
The syntax for declaring an array in C is as follows:
dataType arrayName[arraySize];
For example, to declare an array of 5 floating-point numbers, you would write
float mark[5];
Here, mark is an array that can hold 5 floating-point values.
Adding Two Matrices in C: Here is a simple C program that adds two matrices:
#include <stdio.h>
#define SIZE 3 // Size of the matrix
int main() {
int A[SIZE][SIZE], B[SIZE][SIZE], C[SIZE][SIZE];
int i, j;
return 0;
}
In this program, A, B and C are matrices (2D arrays). The program asks the user to enter the elements of
two matrices, adds them together, and then prints the result. The size of the matrices is defined by the
SIZE constant. You can change this value to work with larger or smaller matrices. The scanf function is
used to read the matrix elements from the user, and the printf function is used to display the result. The
actual addition of the matrices happens in the nested for loop. Each element in the A matrix is added to
the corresponding element in the B matrix, and the result is stored in the C matrix. The indices i and j are
used to traverse the matrices. Note that matrix indices in C start at 0, not 1. So, the first element of the
matrix A is A[0][0], not A[1][1].
Ans10) A structure in C is a user-defined data type that can be used to group items of possibly different
types into a single type. The struct keyword is used to define the structure in the C programming language.
The items in the structure are called its members and they can be of any valid data type.
We declare a structure in C using the struct keyword. Here is the syntax for declaring a structure:
struct structure_name {
data_type member_name1;
data_type member_name2;
// ...
};
Creating Instances of Structures: To use a structure in our program, we have to define its instance. We
can do that by creating variables of the structure type. Here’s how we can define structure variables:
struct structure_name {
data_type member_name1;
data_type member_name2;
Programming in C
// ...
} variable1, variable2, ...;
Structure Variable Declaration after Structure Template:
// structure declared beforehand
struct structure_name variable1, variable2, ...;
Accessing Structure Members: We can access structure members by using the dot operator (.)1. Here is
the syntax1:
structure_name.member1;
structure_name.member2;
In the case where we have a pointer to the structure, we can also use the arrow operator (->) to access
the members3.
// Declare a structure
struct Person {
char name[50];
int age;
float salary;
};
int main() {
// Create an instance of the structure
struct Person person1;
return 0;
}
In this program, we first declare a structure Person with three members: name, age, and salary. We then
create an instance of the structure called person1 and assign values to its members. Finally, we print the
values of the structure members.