C Important Question Answers
C Important Question Answers
What do you mean by data types and what are the different
types of data types available in C?
In C programming, data types define the type of data that a variable can hold. They determine the
memory size and type of operations that can be performed on the data.
o float → Used for decimal numbers with less precision (e.g., float pi = 3.14;).
o structure → Collection of different data types (e.g., struct student {int roll; char
name[20];};).
4. Void Data Type: Represents "no type" (used in functions with no return value).
o void functionName();
int a = 10, b = 5;
int sum = a + b; // sum = 15
2. Relational Operators (==, !=, >, <, >=, <=)
o Example:
if (a > b) {
printf("A is greater");
}
3. Logical Operators (&&, ||, !)
o Example:
a += 5; // Equivalent to a = a + 5;
6. Increment & Decrement Operators (++, --)
o Example:
a++; // Increments a by 1
7. Ternary Operator (? :)
o Example:
int x = (a > b) ? a : b;
1. if Statement:
if (num > 0) {
printf("Positive Number");
2. IF Else-Statement:
if (num > 0) {
printf("Positive");
} else {
printf("Negative");
3. nested if:
int num = 10;
if (num > 0) {
if (num % 2 == 0) {
printf("Positive Even");
4. switch Statement:
int choice = 2;
switch (choice) {
default: printf("Invalid");
1. Compiler
A compiler translates the entire source code into machine code at once before execution.
Advantages:
Disadvantages:
Examples of Compilers:
Example:
c
CopyEdit
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
2. Interpreter
An interpreter translates and executes the code line by line, stopping at errors.
Advantages:
Disadvantages:
Examples of Interpreters:
• PHP Interpreter
python
CopyEdit
print("Hello, World!")
3. Assembler
Advantages:
Disadvantages:
assembly
CopyEdit
section .data
Error Example
Translator Translation Type Speed
Detection Languages
All errors at
Compiler Full program at once Fast C, C++, Java
once
o for loop:
o while loop:
int i = 1;
while (i <= 5) {
i++;
}
o do-while loop:
int i = 1;
do {
i++;
10 String Methods in C
1. strlen() – Find String Length
This function calculates and returns the number of characters in
a string, excluding the null character (\0).
2. strcpy() – Copy String
It copies the content of one string into another string, replacing
the existing content in the destination string.
3. strncpy() – Copy N Characters of a String
Similar to strcpy(), but it allows copying only a specified number
of characters from one string to another.
4. strcat() – Concatenate (Join) Two Strings
It appends the content of one string to the end of another
string, forming a combined string.
5. strncat() – Concatenate N Characters
It performs the same function as strcat() but allows
concatenation of only a specified number of characters.
6. strcmp() – Compare Two Strings
This function compares two strings character by character and
returns a value indicating whether they are equal or which one
is greater.
7. strncmp() – Compare N Characters of Strings
It works like strcmp() but compares only a specified number of
characters between two strings.
8. strchr() – Find First Occurrence of a Character
This function searches for the first occurrence of a specific
character within a string and returns a pointer to its position.
9. strrchr() – Find Last Occurrence of a Character
Similar to strchr(), but it searches for the last occurrence of the
specified character in the string.
10. strstr() – Find a Substring Within a String
It searches for a substring inside another string and returns a
pointer to where the substring begins.
return 0;
}
12. What do you mean by package? Also, write the steps to
create a user-defined package in C.