Programming in C 2 Marks
Programming in C 2 Marks
Programming in C 2 marks
Programming in C
Unit-I
Introduction to C programming
2 Marks
1. Define identifier.
Int
Char
Float
Double
Enum, pointer. array, structure, union, void
6. Define Variables.
A variable is a name given to a storage area that our programs can
manipulate.
Each variable in C has a specific type which determines the size
and layout of the variables memory.
9. Define Statements?
5 Marks
No keyword should access variable name (int for <- invalid because
for is keyword).
Syntax
10 Marks
They are arithmetic types and are further classified into: (a)
integer types and (b) floating-point types.
2 Enumerated types
They are again arithmetic types and they are used to define
variables that can only assign certain discrete integer values
throughout the program.
3 The type void
They include (a) Pointer types, (b) Array types, (c) Structure
types, (d) Union types and (e) Function types.
Integer Types
Floating-Point Types
SOURCE CODE
#include <stdio.h>
int main()
{
int first, second, add, subtract, multiply;
float divide;
printf("Enter two integers\n");
scanf("%d%d", &first, &second);
add = first + second;
subtract = first - second;
multiply = first * second;
divide = first / (float)second; //typecasting
printf("Sum = %d\n",add);
printf("Difference = %d\n",subtract);
printf("Multiplication = %d\n",multiply);
printf("Division = %.2f\n",divide);
return 0;
}
OUTPUT
Unit-II
2 Marks
1. Define Branching
If Statement
The If else Statement
Compound Relational tests
Nested if Statement
Switch Statement
3. Define Looping.
While loop
Do...while loop
For loop
Nested loops
If (condition)
Program statement 1;
Else
Program statement 2;
while(condition)
{
statement(s);
}
5 Marks
switch(expression)
{
case constant-expression :
statement(s);
break;
case constant-expression :
statement(s);
break;
default :
statement(s);
}
The following rules apply to a switch statement
The expression used in a switch statement must have an integral or
enumerated type.
The constant-expression for a case must be the same data type as
the variable in the switch
When a break statement is reached, the switch terminates
Not every case needs to contain a break
A switch statement can have an optional default case, which must
appear at the end of the switch
You can use one or more loops inside any other while, for, or
do..while loop.
10 Marks
Unit-III
Functions
2 Marks
1. Define Functions.
Return type
Function name
Parameters
Function Body
}
10. Define Recursion.
void recursion()
{
recursion();
}
int main()
{
recursion();
}
5 Marks
#include<stdio.h>
int fun()
{
static int count = 0;
count++;
return count;
}
int main()
{
printf("%d ", fun());
printf("%d ", fun());
return 0;
}
OUTPUT
12
Each of the files can be compiled separately, into a *.o file. Later, all
the *.o files can be linked together (still using gcc) into a running program,
an executable object program.
Each file can have access to a set of names that are private to that file,
and to other names that are shared across all the files of the complete
program.
The programmer determines which names are local to each file, and
which are shared, and indicates his/her decisions by constructing
declarations appropriately.
The storage class specifies extern and static play a role in this, as
does the position of each declaration, and whether a declaration is in fact a
“definition”.
10 Marks
Unit-IV
2 Marks
1. Define Array.
5. Define Structure.
the basic set of data types defined in the C language such as int,
float etc. may be insufficient for your application.
In circumstances such as these, you can create your own data
types which are based on the standard ones
5 Marks
You can define a union with many members, but only one member
can contain a value at any given time. Unions provide an efficient way
of using the same memory location for multiple-purpose.
Defining a Union: To define a union, you must use the union
statement in the same way as you did while defining a structure. The
union statement defines a new data type with more than one member
for your program. The format of the union statement is as follows −
#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str[20];
};
int main( ) {
union Data data;
printf( "Memory size occupied by data : %d\n", sizeof(data));
return 0;
}
OUTPUT: Memory size occupied by data : 20
10 Marks
Unit-V
2 Marks
1. Define pointers.
3. Define File.
Create
OPen
Process
Delete
Close
5 Marks
1. Explain the different file types that can be specified by the fopen( )
function.
mode Description
"r" Opens a file for reading. The file must exist.
Creates an empty file for writing. If a file with the same name already
"w" exists, its content is erased and the file is considered as a new empty
file.
Appends to a file. Writing operations, append data at the end of the
"a"
file. The file is created if it does not exist.
"r+" Opens a file to update both reading and writing. The file must exist.
"w+" Creates an empty file for both reading and writing.
"a+" Opens a file for reading and appending.
#include <stdio.h>
#include <time.h>
void getSeconds(unsigned long *par);
int main () {
unsigned long sec;
getSeconds( &sec );
printf("Number of seconds: %ld\n", sec );
return 0;
}
void getSeconds(unsigned long *par) {
*par = time( NULL );
return;
}
OUTPUT: Number of seconds :1294450468
Mode Description
r Opens an existing text file for reading purpose.
Opens a text file for writing. If it does not exist, then a new file is created. Here
w
your program will start writing content from the beginning of the file.
Opens a text file for writing in appending mode. If it does not exist, then a new file
a is created. Here your program will start appending content in the existing file
content.
r+ Opens a text file for both reading and writing.
Opens a text file for both reading and writing. It first truncates the file to zero
w+
length if it exists, otherwise creates a file if it does not exist.
Opens a text file for both reading and writing. It creates the file if it does not exist.
a+ The reading will start from the beginning but writing can only be appended.
10 Marks
Example Program
#include <stdio.h>
int main () {
int var1;
char var2[10];
Advantage of File
It will contain the data even after program exit. Normally we use
variable or array to store data, but data is lost after program exit. Variables
and arrays are non-permanent storage medium whereas file is permanent
storage medium.
struct name {
member1;
member2;
.
.
};
int main()
{
struct name *ptr;
}
#include <stdio.h>
typedef struct person
{
int age;
float weight;
};
int main()
{
struct person *personPtr, person1;
personPtr = &person1; // Referencing pointer to memory address of
person1
scanf("%d",&(*personPtr).age);
return 0;
}