Programming in C - SAE1A
Programming in C - SAE1A
Unit-I
Introduction to C programming
2 Marks
1. Define identifier.
Int
Char
Float
Double
Enum, pointer. array, structure, union, void
5. What is meant by constants?
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
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
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
4. Discuss about Constants in C.
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;
6. Define Switch Statement.
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
2. Explain the loop with its types.
You can use one or more loops inside any other while, for, or
do..while loop.
10 Marks
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
2. Describe Multi-file programs.
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
2 Marks
1. Define Array.
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
#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
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.
#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];
printf("Address of var1 variable: %x\n", &var1 );
printf("Address of var2 variable: %x\n", &var2 );
return 0;
}
OUTPUT
Address of var1 variable: bff5a400
Address of var2 variable: bff5a3f6
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
printf("Displaying: ");
printf("%d%f",(*personPtr).age,(*personPtr).weight);
return 0;
}