0% found this document useful (0 votes)
36 views11 pages

C Notes Sem 1 (1st File)

Uploaded by

sujaymandalslg09
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)
36 views11 pages

C Notes Sem 1 (1st File)

Uploaded by

sujaymandalslg09
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/ 11

Q ) Give the basic structure of a C program .

Ans :-

A C program is divided into different sections. The main sections to a basic c program.

The sections are,

• 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

/*

File Name: myprogram.c

description: a program to display hello world

*/

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

Global Declaration Section


This part of the code is the part where the global variables are declared. All the global
variable used are declared in this part. The user-defined functions are also declared in
this part of the code.

1float area(float r);


2int a=7;

Main Function Section


Every C-programs needs to have the main function. The execution part begins with the
curly brackets and ends with the curly close bracket. Both the declaration and execution
part are inside the curly braces.

1int main(void)
2{
3int a=10;
4printf(" %d", a);
5return 0;
6}

Sub Program Section


All the user-defined functions are defined in this section of the program.

1int add(int a, int b)


2{
3return a+b;
4}
Q) What is type conversion ? Explain implicit and explicit type conversion with
example .
Ans :- Converting one data type into another data type is called type conversions.

• Implicit type conversion


• Explicit type conversion

Implicit type conversion :-


• The compiler provides implicit type conversions when operands are of
different data types.
• It is automatically done by the compiler by converting smaller data type into
a larger data type.

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.

Explicit type conversion:-


• Explicit type conversion is done by the user by using (type) operator.
• Before the conversion is performed, a runtime check is done to see if the
destination type can hold the source value.

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.

Four formatted I/O statements are discussed below


1) scanf()
We use the scanf() function for getting the formatted inputs or standard inputs from the user

Syntax for scanf()


scanf (“format_specifier”, &data_a, &data_b,……); // Here, & refers to the address operator

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().

Syntax for printf()


printf (“format_specifiers”, info_a, info_b,…….. );

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:

1. int fprintf(FILE *stream, const char *format [, argument, ...])

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:

1. int fscanf(FILE *stream, const char *format [, argument, ...])

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.

Example :-// user defined function to calculate factorial of a number.

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 −

return_type function_name( parameter list ) {


body of the function
}

A function definition in C programming consists of a function header and a function


body. Here are all the parts of a function −
• Return Type − The return_type is the data type of the value the function
returns. Some functions perform the desired operations without returning
a value. In this case, the return_type is the keyword void.
• Function Name − This is the actual name of the function. The function name
and the parameter list together constitute the function signature.
• Parameters − A parameter is like a placeholder. When a function is
invoked, you pass a value to the parameter. This value is referred to as
actual parameter or argument.
• Function Body − The function body contains a collection of statements that
define what the function does.

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 −

/* function returning the max between two numbers */


int max(int num1, int num2) {

/* local variable declaration */


int result;

if (num1 > num2)


result = num1;
else
result = num2;

return result;
}
Q ) What is String ? Discuss any 4 string handling functions in C .
Ans :-

Strings are actually one-dimensional array of characters terminated by a null character


'\0'. Thus a null-terminated string contains the characters that comprise the string
followed by a null.
We can initialize a string as follows

char str[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

We can also initialize as follows

char greeting[] = "Hello";


Following is the memory presentation of the above defined string in C

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!";

// Concatenate str2 to str1 (result is stored in str1)


strcat(str1, str2);

// 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];

// Copy str1 to str2


strcpy(str2, str1);
// Print str2
printf("%s", str2);

output : Hello World!

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";

// Compare str1 and str2, and print the result


printf("%d\n", strcmp(str1, str2)); // Returns 0 (the strings are equal)

// Compare str1 and str3, and print the result


printf("%d\n", strcmp(str1, str3)); // Returns -4 (the strings are not
equal)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy