C Notes Sem 1 (1st File)
C Notes Sem 1 (1st File)
Ans :-
A C program is divided into different sections. The main sections to a basic c program.
• Documentation
• Link
• Definition
• Global Declarations
• Main functions
• Subprograms
Documentation Section
The documentation section is the part of the program where the programmer gives the
details associated with the program. It gives anyone reading the code the overview of the
code.
Example
/*
*/
Link Section
This part of the code is used to declare all the header files that will be used in the program.
This leads to the compiler being told to link the header files to the system libraries.
Example
1#include<stdio.h>
Definition Section
In this section, we define different constants. The keyword define is used in this part.
1#define PI=3.14
1int main(void)
2{
3int a=10;
4printf(" %d", a);
5return 0;
6}
Example
Following is an example for implicit type conversion −
int x=7.9;
// variable x will be assigned the value 7 implicitly as x is of integer type.
Example
int a,c;
float b;
c = (int) (a + b);
Here, the resultant of ‘a+b’ is converted into ‘int’ explicitly and then assigned to ‘c’.
Q) Explain any four formatted I/O statements in C .
Ans :-
Formatted I/O functions are used to take various inputs from the user and
display multiple outputs to the user. These types of I/O functions can help to
display the output to the user in different formats using the format specifiers.
These I/O supports all data types like int, float, char, and many more.
Example of scanf()
scanf(“%d %c”, &a,&b);
variables a and b can store int and char data respectively .
2) printf()
We use the printf() function for generating the formatted outputs or standard outputs in accordance
with a format specification. The output data and the format specification string act as the
parameters of the function printf().
Example of printf()
printf(“%d %c”, a, b);
3) fprintf() function
The fprintf() function is used to write set of characters into file. It sends formatted output
to a stream.
Syntax:
Example:
1. #include <stdio.h>
2. main(){
3. FILE *fp;
4. fp = fopen("file.txt", "w");//opening file
5. fprintf(fp, "writing data into my file ");//writing data into file
6. fclose(fp);//closing file
7. }
4) fscanf() function
The fscanf() function is used to read set of characters from file. It reads a word from the
file and returns EOF at the end of file.
Syntax:
Example:
1. #include <stdio.h>
2. main(){
3. FILE *fp;
4. char buff[100];//creating char array to store data of file
5. fp = fopen("file.txt", "r");
6. while(fscanf(fp, "%s", buff)!=EOF){
7. printf("%s ", buff );
8. }
9. fclose(fp);
10. }
Q) what is a function in C ? discuss its uses . how many types of functions are
there ? give example .
Ans :-
In c, we can divide a large program into the basic building blocks known as function. The
function contains the set of programming statements enclosed by {}. A function can be
called multiple times to provide reusability and modularity to the C program.
Advantage of functions in C
There are the following advantages of C functions.
o By using functions, we can avoid rewriting same logic/code again and again in a program.
o We can call C functions any number of times in a program and from any place in a
program.
o We can track a large C program easily when it is divided into multiple functions.
o Reusability is the main achievement of C functions.
o Error detection and correction becomes easier.
Types of Functions
There are two types of functions in C programming:
1. Library Functions: are the functions which are declared in the C header files such as
scanf(), printf(), gets(), puts(), ceil(), floor() etc.
2. User-defined functions: are the functions which are created by the C programmer, so
that he/she can use it many times. It reduces the complexity of a big program and
optimizes the code.
int fact(int n)
{
int f=1,I;
for(i=1;i<=n;i++)
f=f*i;
return (f);
}
Q ) Give the general form of function definition in C.
Ans :- A function declaration tells the compiler about a function's name, return type, and
parameters. A function definition provides the actual body of the function.
A function can also be referred as a method or a sub-routine or a procedure, etc.
Defining a Function
The general form of a function definition in C programming language is as follows −
Example
Given below is the source code for a function called max(). This function takes two
parameters num1 and num2 and returns the maximum value between the two −
return result;
}
Q ) What is String ? Discuss any 4 string handling functions in C .
Ans :-
String Functions
C also has many useful string functions, which can be used to perform certain
operations on strings.
To use them, you must include the <string.h> header file in your program:
#include <string.h>
String Length
For example, to get the length of a string, you can use the strlen() function:
Example
char str[] = "hello";
printf("%d", strlen(str));
output : 5
Concatenate Strings
To concatenate (combine) two strings, you can use the strcat() function:
Example
char str1[20] = "Hello ";
char str2[] = "World!";
// Print str1
printf("%s", str1);
output : HelloWorld
Copy Strings
To copy the value of one string to another, you can use the strcpy() function:
Example
char str1[20] = "Hello World!";
char str2[20];
Compare Strings
To compare two strings, you can use the strcmp() function.
It returns 0 if the two strings are equal, otherwise a value that is not 0:
Example
char str1[] = "Hello";
char str2[] = "Hello";
char str3[] = "Hi";