A ‘C’ program contains executable statements. A compiler helps to translate the executable statements into machine language.When a user runs the program, he/she machines the language statements which are executed by the compiler.Types of executable statementsThe types of executable statements in C language are as follows −Input – output statementsAssignment statementsInput-output statementsStoring a value into memory is called ‘input operation’.After executing the computations, the results are stored in memory and the results can be displayed to the user by ‘output operation’.All i/o operations are performed using input / output functions.The most common I/O functions are supplied through the preprocessor directive ... Read More
"Recursive function" is something which calls itself again in the body of the function.For example, A function fact ( ), which computes the factorial of an integer ‘N’, which is the product of all whole numbers from 1 to N.fact ( ) with an argument of 1 (or) 0, the function returns 1. otherwise, it returns n*fact (n-1), this happens until ‘n’ equals 1.Fact (5) =5* fact (4) =5*4*3* fact (3) =5*4*3*2* fact (2) =5*4*3*2*1 fact (1) =5*4*3*2*1 = 120.ExampleFollowing is the C program for use of recursive function to reverse a number −#include main ... Read More
Let us first understand, what is a variable.VariableIt is the name for memory location that may be used to store a data value.A variable may take different values at different times during execution.A variable name may be chosen by the programmer in a meaningful way, so as to reflect its function (or) nature in the program.For example, sum, avg, total etc.Rules for naming variableThe rules for naming a variable are explained below −They must begin with a letter.Maximum length of variable is 31 characters in ANSI standard. But, first eight characters are significant by many compilers.Upper and lowercase characters are ... Read More
The link and definition sections are called as preprocessor directives. It gives instructions to the compiler to link function from the system library.For example, the definition section defines all the symbolic constants.#includeFor example, #define PI 3.1415The preprocessor directives must start with # symbol.Without link definition the program will not execute for some compilers. It helps the compiler to link the predefined functions from system library.Predefined FunctionsThe predefined functions present in stdio.h are as follows −FunctionDescriptionprintf()Print the character, string, float, integer, octal onto the screen.scanf()Read a character, string, numeric data from keyboard.getc()Reads character from file.gets()Reads line from keyboard.getchar()Reads character from keyboard.puts()Writes ... Read More
In this program, we are trying to sort out the odd numbers and even numbers that are present in one file. Then, we try to write all odd numbers in ODD file and even numbers into EVEN file.Open a file DATA in write mode and write some numbers into the file and later on close it.Again, Open DATA file in read mode.Open ODD file in write mode.Open EVEN file in write mode.Then, perform the operations to check odd and even numbers by using while loop.After that close all files.ExampleFollowing is the C program to handle integer data files using file ... Read More
A pointer is a variable whose value is the address of an another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address.Consider the following statement −int qty = 179;The representation of the variable in memory is as follows −You can declare a pointer as follows −Int *p;It means ‘p’ is a pointer variable that holds the address of another integer variable.Address operator (&) is used to initialize a pointer variable.For Example −int qty = 175; int *p; p= &qty;To access the value of ... Read More
Enter two numbers at console during runtime. Then, declare the flag variable which is used to check whether the number is prime or not with the help of for loop condition.Whenever, the flag is zero, it prints the prime number and if flag is one, it exists from the loop.ProgramFollowing is the C program to display the prime numbers in between two intervals − Live Demo#include int main(){ int number1, number2, i, j, flag; printf("enter the two intervals:"); scanf("%d %d", &number1, &number2); printf("prime no’s present in between %d and %d:", number1, number2); for(i=number1+1;iRead More
The sum of first and last digit for a number can be calculated if we use the below mentioned algorithm in C programming language.AlgorithmRefer an algorithm given herewith −START Step 1: Declare no, sum variables of type int Step 2: Read a number at runtime Step 3: Compute sum=no%10 Step 4: While loop no>9 No=no/10 Step 5: Compute sum=sum+no; Step 6: Print sum STOPExampleFollowing is the C program to find the sum of first and last digit for a number − Live Demo#include int main(){ unsigned int no, sum; printf("enter any number:"); scanf("%u", &no); sum=no%10; ... Read More
C program is defined by set of protocols that are to be followed by a programmer, while writing the code.SectionsThe complete program is divided into different sections, which are as follows −Documentation Section − Here, we can give commands about program like author name, creation or modified date. The information written in between/* */ or // is called as comment line. These lines are not considered by the compiler while executing.Link section − In this section, header files that are required to execute the program are included.Definition section − Here, variables are defined and initialised.Global declaration section − In this ... Read More
If we are generating random numbers in a program, it is necessary to control the series of numbers.The randomize() and srand() functions are used to seed the random number generator.The process of assigning the random number generators starting number is called seeding the generators.The randomize() uses PC’s clock to produce a random seed.srand() allows us to specify the random number generator’s starting value.ProgramGiven below is the C program on rand − Live Demo#include int main(){ // create same sequence of // random numbers on every time the program runs for(int i = 0; i