0% found this document useful (0 votes)
16 views9 pages

C Notes 12

Uploaded by

grgtenzin6
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)
16 views9 pages

C Notes 12

Uploaded by

grgtenzin6
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/ 9

1. Define the term "constant", "Identifier" and "keyword" in C program.

Constant [5]
Ans. A constant is a name given to a variable whose values can't be changed. A constant can hold only a single
variable during the execution of a program. This quantity can be stored at a location in the memory of the
computer. Constants refer to fixed values that may not be altered by the program. Example of some constants
are 5, 10, 'a', 3.41, "e programming" Chas four basic types of constants. They are:
Integer constant: It is an integer-valued number. It can be positive or negative. It can also be a decimal, octal,
or hexadecimal integer constant.
Floating point constant: It is a fractional number. It can be written either in decimal form or an exponent
form or both. It can also be a decimal, octal, or hexadecimal floating-point constant.
Character constant: It is a single character, enclosed in apostrophes (single quotation marks).
String constant: It consists of any number of consecutive characters enclosed in double quotation marks.
Identifier
The identifier is the name given to various program elements such as variables, functions, labels, and other
user-defined item. It can be a combination of numbers, alphabets, and special symbols [underscore ()]. The
first character of an identifier must be an alphabet or an underscore. In an identifier, uppercase and lowercase
alphabets are treated as differently (because of case sensitivity). Identifiers are strings of alphanumeric
characters that don't begin with numbers. There are 53 alphabetic characters: 52 letters and one is the
underscore () character. e.g., sum, Sum, a_b, temp, field4, Year2011, roll_no is valid identifiers.
Keyword
The keyword is a reserved word that has standard and predefined meanings in C. We cannot use a keyword
for any other purpose other than as a predefined task in a C program. Its meaning has already been defined by
the C compiler. ANSI C defines 32 keywords. The keywords can't be used as variable names because if we
try to do so, we are trying to assign a new meaning to the keyword, which is not allowed by the compiler. A
list of some keywords is: double, float, int, struct, break, continue, else, for, switch, void, case, default, goto,
register, char, do, if, and while.
What is variable? List the major data types which is used in C program. [2+3]
Variable
A variable is an identifier used to represent data in memory. The value of the variable may change during the
execution of the program. Every variable must be declared before it is used.
Example: int i = 5;
i=i+1;
Here, i is the variable.
For Second part
Date type is an instruction that is used to declare the category or type of variable being used in the program.
Any variable used in the program must be declared before using it.
Major Data Types which is used in C program
Data type defines
Type of data used in the program
• Memory requirement of the data
Operations that can be performed on the data.
Different data types used in C-programming are: int, float, char, void.
Int: It represents integer numbers (numbers without fraction e.g. 546). It requires 2 By memory space. It can
be either signed or unsigned. Derived data types from int are shoe int, long int.
float: It represents real numbers (numbers with fraction eg. 3.14). It requires 4 Byt memory space. It can also
be signed or unsigned. Derived data types form float
double, long double char. It represents single alphabet and symbols (e.g. 'A', 'f, '@'). It requires 1 Byte memory
space. It can also be either signed or unsigned.
void: This data type has no values. It is used as return type for functions that do not return a value.
What is structure in C Program? Explain.
Structure is a kind of user defined data type which can store the data of various types such as character, integer,
float, etc. It is a heterogeneous collection of data.
Example for declaring structure Syntax for declaring structure

struct student struct structure_name{


{ data type memberl;
char name[20]; data_type member2;
int roll; data_type member;
int age; }
}

List out the different types of operator in C. Describe the logical operator with example.
An operator is a symbol that instructs C to perform some operation on one or more operands. An operand is
something that an operator acts on. The different types of types of operators are:
 Arithmetic operator
 Relational operator
 Logical operator
 Assignment operators
 Increment/Decrement operator
 Ternary operator
 Nitwise operator
 Special operator
Relational operator:
C's relational operators is used to compare expressions. It is used in conditional statement. Different relational
operators are:
Operator Symbol Example
Equal == x==y
Greater than > x>y
Less than < x<y
Greater than or equal to >= x>=y
Less than or equal to <= x<=y
Not equal to != x!=y

Logical operator:
C's logical operators let you combine two or more relational expression into a single expression that evaluates
to either true or false. Different logical operators used are:
Operator Symbol Example
AND && exp 1 && exp2
OR || exp 1 || exp 2
NOT !`` ! exp 1

Define pointer in C with example.


A pointer is a variable which stores the address number of reference number of a cell, instead of storing the
actual value of the cell. It is really a very powerful variable which is used to directly deal with memory of
computer. A pointer is denoted by (indirection operator) and has to be declared in the similar way as we
declare other variables. Example: int *p;
Describe the different mode of the file handling
Different modes of file handling concept are:
1. r mode: It is used to open file for reading purpose.
2. w mode: It is used to open file for writing purpose. If a file already exists, it will erase the previous content
and write the new one and it the file doesn't exist, it will create a new file and store data.
3. a mode: It is used to open file for appending data. If a file already exists, it will add the new data at the end
of the file and if the file doesn't exist, it will create a new file and store data. r+ mode: It is used for opening
the file for both reading and writing. However, to use this the file must already exist. 5. w+ mode: It is used
for both writing and reading. If the files already exists, its previous contents will be erased.
6. a+ mode: It is used for both reading and appending. If the file already exist, it will add data at the end of
the file.
Explain the file handling concept in C.
In C programming, file is used for storing data permanently. While handling file in C has to be created/opened,
perform read/write operation and finally close the file. Ple handling also includes renaming and deleting the
file.
Creating/opening the file
Syntax: file pointer("file_name","file_mode");
Example: fp("student.dat", "r");
Writing data in the file
To write data in the file, it has to be opened in either writing or appending mode. Syntax, fprintf(file pointer,
"list_of_format specifiers", list_of_variables);
Example: fprintf(fp."%s%s%d",name,add,roll);
Reading the data from the file
To read data from the file, it has to be opened in either reading mode.
Syntax: fscanf(file pointer, "list_of_format_specifiers",list_of_variables);
Example: fscanf(fp,"%s%s%d",name,add,&roll);
Closing the file
Syntax: fclose(file_pointer);
Example: fclose(fp);
Renaming the file
Syntax: rename(old_file_name","new_file_name");
Example: rename("student.dat","new.dat");
Removing/Deleting the file
Syntax: remove("file_name");
Example: remove("new.dat");

LEMENTS OF
ARRAY STRUCTURE
COMPARISON
An array can be defined as a container where a
A structure can be defined as a container where
collection of variables can be stored that are of
Definition a collection of variables can be stored that are
the same data type as the array and also support
of both (same and different) data types.
contiguous memory allocation.
struct struct_name{ type element1; type
Syntax type array_name[size];
element2; ... } variable1, variable2, ...;
The operator that is used while we declare an The operator that is used while we declare a
Operator array and the elements accessing it is a square structure and the elements accessing it is a dot
bracket "[ ]". operator".".
The array stores its data in a contiguous memory The structure may not store its data in a
Memory
location. contiguous memory location.
The array name is referred to as the first element The structure name does not highlight the first
Pointer in the array hence we can say that an array is a element of the structure hence, the structure
pointer. name is not a pointer.
All the elements in the array are accessed by All the elements in the structure are accessed
Access
their index number. by their actual names.
The elements found in the array are of the same The elements found in the structure are of
Size
size. different data types.
No objects or any instances are created in the Instances or structure variables can be created
Objects
array. in the structure.
To access a structure it comparatively takes
Accessing To access an array it usually takes less time
longer.
Bit field In an array no bit field can be defined In a structure bit field can be defined
"struct" is a keyword that is always used while
Keyword No keyword is used to declare an array
declaring a structure.
As an array is directly declared we don't need to
User-defined Structure is user-defined datatype.
specifically define it.
Performance
The performance for an array is always higher The performance for a structure is always lesser
(searching and
as the data types are of the same type which than the array's performance as the data types
accessing)
LEMENTS OF
ARRAY STRUCTURE
COMPARISON
makes searching and accessing any element are of different data types which makes
faster. searching and accessing any elements slower.

Write a C program to enter name, grade, age and address of 10 students in a structure and display the
information.
#include <stdio.h>
struct Student {
char name[50];
char grade;
int age;
char address[100];
};
int main() {
struct Student students[10];
int i;
printf("Enter information for 10 students:\n");
for (i = 0; i < 10; i++) {
printf("\nStudent %d:\n", i + 1);
printf("Name: ");
scanf("%s", students[i].name);
printf("Grade: ");
scanf(" %c", &students[i].grade);
printf("Age: ");
scanf("%d", &students[i].age);
printf("Address: ");
scanf("%s", students[i].address);
}
printf("\n\nInformation of 10 students:\n");
for (int i = 0; i < 10; i++) {
printf("\nStudent %d:\n", i + 1);
printf("Name: %s\n", students[i].name);
printf("Grade: %c\n", students[i].grade);
printf("Age: %d\n", students[i].age);
printf("Address: %s\n", students[i].address);
}
return 0;
}
Describe file handling concept in C. Write a C program to enter names and address of the student and
store them in a data file" student.dat".
#include <stdio.h>

void main() {
FILE *fp;
char name[50];
char address[100];
fp = fopen("student.dat", "w");
printf("\nEnter name for student %d: \n");
scanf("%s", name);
printf("Enter address for student %d: \n");
scanf("%s", address);
fprintf(fp, "%s %s\n", name, address);

fclose(fp);
getch();
}
What is structure? Write a program to input roll, name and age of 5 studetns and display then properly
structure.
#include <stdio.h>

// Define a structure for student


struct Student {
int roll;
char name[50];
int age;
};

int main() {
struct Student students[5];
printf("Enter information for 5 students:\n");
for (int i = 0; i < 5; i++) {
printf("\nStudent %d:\n", i + 1);
printf("Roll: ");
scanf("%d", &students[i].roll);
printf("Name: ");
scanf("%s", students[i].name);
printf("Age: ");
scanf("%d", &students[i].age);
}
printf("\n\nInformation of 5 students:\n");
for (int i = 0; i < 5; i++) {
printf("\nStudent %d:\n", i + 1);
printf("Roll: %d\n", students[i].roll);
printf("Name: %s\n", students[i].name);
printf("Age: %d\n", students[i].age);
}
getch();
}
Write a program to read the marks of any 5 students in a subject and count how many students are
passed or failed.
#include<stdio.h>
#include<conio.h>
void main() {
int marks[5], i, count = 0, failed;
for (i = 0; i < 5; i++) {
printf("Input mark of student %d: ", i + 1);
scanf("%d", &marks[i]);
}
for (i = 0; i < 5; i++) {
if (marks[i] >= 35) {
count++;
}
}
failed = 5 - count;
printf("Number of passed students = %d\n", count);
printf("Number of failed students = %d\n", failed);
getch();
}
Write a program to enter ten numbers into an array, sort and display them in ascending order.
#include <stdio.h>

int main() {
int numbers[10], i, j, temp;

printf("Enter ten numbers:\n");


for (i = 0; i < 10; i++) {
printf("Number %d: ", i + 1);
scanf("%d", &numbers[i]);
}
for (i = 0; i < 9; i++) {
for (j = 0; j < 9 - i; j++) {
if (numbers[j] > numbers[j + 1]) {
// Swap the numbers
temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
printf("\nNumbers in ascending order:\n");
for (i = 0; i < 10; i++) {
printf("%d\n", numbers[i]);
}

return 0;
}
Write down the syntax to create a new file in c .
FILE *filePointer;
filePointer = fopen("filename", "mode");
example
#include <stdio.h>

int main() {
FILE *filePointer;
filePointer = fopen("example.txt", "w");
if (filePointer == NULL) {
printf("Failed to create the file.\n");
return 1;
} else {
printf("File created successfully.\n");
fclose(filePointer);
return 0;
}
}

You might also like