C Programming Code Examples
C > Beginners Lab Assignments Code Examples
Void (void) data type in C programming language
1
2
3
4
5
6
7
8
9
10
11
12
13
/* Void (void) data type in C programming language
void in C means "nothing" or "no-value". This is used either with pointer declarations or with function declarations. */
// declares function which takes no arguments but returns an integer value
int status(void)
// declares function which takes an integer value but returns nothing
void status(int)
// declares a pointer p which points to some unknown type
void * p
Pointers in C Language
Pointers in C are easy and fun to learn. Some C programming tasks are performed more easily with pointers, and other tasks, such as dynamic memory allocation, cannot be performed without using pointers. So it becomes necessary to learn pointers to become a perfect C programmer. Let's start learning them in simple and easy steps.
As you know, every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator, which denotes an address in memory. Consider the following example, which prints the address of the variables defined:
#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;
}
Syntax for Pointer variable declaration in C
type *var-name;
int *ip; /* pointer to an integer */
double *dp; /* pointer to a double */
float *fp; /* pointer to a float */
char *ch /* pointer to a character */
if(ptr) /* succeeds if p is not null */
if(!ptr) /* succeeds if p is null */
Advantage of Pointer
1) Pointer reduces the code and improves the performance, it is used to retrieving strings, trees, etc. and used with arrays, structures, and functions.
2) We can return multiple values from a function using the pointer.
3) It makes you able to access any memory location in the computer's memory.
Usage of Pointer
There are many applications of pointers in c language.
1) Dynamic memory allocation: In c language, we can dynamically allocate memory using malloc() and calloc() functions where the pointer is used.
2) Arrays, Functions, and Structures: Pointers in c language are widely used in arrays, functions, and structures. It reduces the code and improves the performance.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* working of pointers in C Language */
#include <stdio.h>
int main()
{
int* pc, c;
c = 22;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 22
pc = &c;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 22
c = 11;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 11
*pc = 2;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 2
return 0;
}
Functions in C Language
A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions. You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division is such that each function performs a specific task.
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.
The C standard library provides numerous built-in functions that your program can call. For example, strcat() to concatenate two strings, memcpy() to copy one memory location to another location, and many more functions.
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
}
Return Type
A function may return a value. 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. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.
Function Body
The function body contains a collection of statements that define what the function does.
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;
}
Function Declarations
A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately.
A function declaration has the following parts:
return_type function_name( parameter list );
int max(int num1, int num2);
int max(int, int);
Calling a Function
While creating a C function, you give a definition of what the function has to do. To use a function, you will have to call that function to perform the defined task.
When a program calls a function, the program control is transferred to the called function. A called function performs a defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns the program control back to the main program.
To call a function, you simply need to pass the required parameters along with the function name, and if the function returns a value, then you can store the returned value.
Function Arguments
If a function is to use arguments, it must declare variables that accept the values of the arguments. These variables are called the formal parameters of the function.
Formal parameters behave like other local variables inside the function and are created upon entry into the function and destroyed upon exit.
While calling a function, there are two ways in which arguments can be passed to a function:
Call by value: This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.
Call by reference: This method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument.
There are the following advantages of C functions.
• By using functions, we can avoid rewriting same logic/code again and again in a program.
• We can call C functions any number of times in a program and from any place in a program.
• We can track a large C program easily when it is divided into multiple functions.
• Reusability is the main achievement of C functions.
• However, Function calling is always a overhead in a C program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/* creating a user defined function addition() */
#include <stdio.h>
int addition(int num1, int num2)
{
int sum;
/* Arguments are used here*/
sum = num1+num2;
/* Function return type is integer so we are returning
* an integer value, the sum of the passed numbers.
*/
return sum;
}
int main()
{
int var1, var2;
printf("Enter number 1: ");
scanf("%d",&var1);
printf("Enter number 2: ");
scanf("%d",&var2);
/* Calling the function here, the function return type
* is integer so we need an integer variable to hold the
* returned value of this function.
*/
int res = addition(var1, var2);
printf ("Output: %d", res);
return 0;
}
Comments in C Language
In the C Programming Language, you can place comments in your source code that are not executed as part of the program.
Comments provide clarity to the C source code allowing others to better understand what the code was intended to accomplish and greatly helping in debugging the code. Comments are especially important in large projects containing hundreds or thousands of lines of source code or in projects in which many contributors are working on the source code.
A comment starts with a slash asterisk /* and ends with a asterisk slash */ and can be anywhere in your program. Comments can span several lines within your C program. Comments are typically added directly above the related C code. /* comment here */
Syntax for Single Line Comments in C
// single line comment
Syntax for Multi Line Comments in C
/*Comment starts
continues
continues
..
.
Comment ends*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* place comments in your source code that are not executed as part of the program by multi line comment example */
#include <stdio.h>
int main()
{
/*int value=100;*/
int x=10;
printf("Value of x= %d\n",/*value*/x);
// single line comment example
int x=13;
printf("Value of x= %d\n",/*value*/x);
/*
multi line comment
*/
return 0;
}
C Program the tm structure contains the following members. Seconds, 0-60, minutes, 0-59, hours, 0-23, day of the month, 1-31, Jan, 0-11, years from 1900, days since Sunday...
The Factorial of a Positive Number n is given by: factorial of n (n!) = 1*2*3*4...n. Factorial of a negative number doesn't exist. And, the factorial of 0 is 1, 0! = 1. The program takes a
C Get ngrams for first word. Get ngrams for second word. Compare two arrays, count duplicates. Calculate score. Clean up. Return array of ngrams. Padd word according to one
Take two numbers as input and store it in the variables j1 and j2 respectively. Call function swap and pass the variables j1 and j2 as parameters to the function swap. In function
C Program uses recursive function & reverses the nodes in a Linked list and displays the list. Linked list is an ordered set of data elements, each 'containing a link' to its successor. This C