Skip to main content

Posts

Showing posts with the label C

C: 13. File Handling

In programming, we may require some specific input data to be generated several numbers of times. Sometimes, it is not enough to only display the data on the console. The data to be displayed may be very large, and only a limited amount of data can be displayed on the console, and since the memory is volatile, it is impossible to recover the programmatically generated data again and again. However, if we need to do so, we may store it onto the local file system which is volatile and can be accessed every time. Here, comes the need of file handling in C. File handling in C enables us to create, update, read, and delete the files stored on the local file system through our C program. Writing File:- #include <stdio.h>   main(){      FILE *fp;      fp = fopen("file.txt", "w");//opening file      fprintf(fp, "Hello file by fprintf...\n");//writing data into file      fclose(fp);//closing file   }   Reading File:-  #include <stdio.h>   main(){      F

C: 12. Structures

Structure is another user defined data type available in C that allows to combine data items of different kinds. Structures are used to represent a record. Suppose you want to keep track of your books in a library. You might want to track the following attributes about each book − Title Author Subject Book ID Defining a Structure:-  struct Books {    char  title[50];    char  author[50];    char  subject[100];    int   book_id; } book;  Example:-  #include <stdio.h> #include <string.h>   struct Books {    char  title[50];    char  author[50];    char  subject[100];    int   book_id; };   int main( ) {    struct Books Book1;        /* Declare Book1 of type Book */    struct Books Book2;        /* Declare Book2 of type Book */      /* book 1 specification */    strcpy( Book1.title, "C Programming");    strcpy( Book1.author, "Nuha Ali");     strcpy( Book1.subject, "C Programming Tutorial");    Book1.book_id = 6495407;    /* book 2 specification */

C: 11. Functions

  A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), A function can be called multiple times to provide reusability and modularity to the C program.  Function Aspects:-  Function declaration A function must be declared globally in a c program to tell the compiler about the function name, function parameters, and return type. Function call Function can be called from anywhere in the program. The parameter list must not differ in function calling and function declaration. Function definition It contains the actual statements which are to be executed. It is the most important aspect to which the control comes when the function is called. Example:-  #include<stdio.h>   void printName();   void main ()   {       printf("Hello ");       printName();   }   void printName()   {       printf("Javatpoint");   }   Output:-  Hello Javatpoint Exercises:- Try the previous codes by using functions

C: 10. Array

 An array is defined as the collection of similar type of data items stored at contiguous memory locations. For example, if we want to store the marks of a student in 6 subjects, then we don't need to define different variables for the marks in the different subject. Instead of that, we can define an array which can store the marks in each subject at the contiguous memory locations. An array index starts with 0. Syntax:- data_type array_name[array_size];   int marks[5];     Example:- #include<stdio.h>   int main(){       int i=0;     int marks[5];//declaration of array        marks[0]=80;//initialization of array     marks[1]=60;     marks[2]=70;     marks[3]=85;     marks[4]=75;     //traversal of array     for(i=0;i<5;i++){       printf("%d \n",marks[i]);     }//end of for loop      return 0;   }   Output:-  80 60 70 85 75 Exercises:-  Write a C program to read and print elements of array .  Write a C program to print all negative elements in an array . Write a

C: 9. Strings

Strings are actually one-dimensional array of characters terminated by a null character '\0'. Each character in the array occupies one byte of memory, and the last character must always be 0. Syntax:- char ch[10]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};   Exercises:-  Write a C program to find length of a string. Write a C program to copy one string to another string. W rite a C program to concatenate two strings. Write a C program to find reverse of a string. Write a C program to convert lowercase string to uppercase. Write a C program to convert uppercase string to lowercase.

C: 8. Loops

The looping can be defined as repeating the same process multiple times until a specific condition satisfies.  C programming language provides the following types of loops to handle looping requirements:-  while loop The while loop in c is to be used in the scenario where we don't know the number of iterations in advance. It tests the condition before executing the loop body. Syntax:- while(condition){   //code to be executed   }        Exercises:-  Write a C program to print all natural numbers from 1 to n. Write a C program to print all even numbers between 1 to 100. do-while loop It works the same as while loop except it executes once even the given condition fails. Syntax:-  do{   //code to be executed   }while(condition);     Exercises:-  Write a C program to print all natural numbers in reverse (from n to 1) . for loop The for loop is also called as a per-tested loop. It is better to use for loop if the number of iteration is known in advance. Syntax:-  for(initialization;con

C: 7. Decision Making Switch

The switch statement in C allows us to execute multiple operations for the different possibles values of a single variable called switch variable. Here, We can define various statements in the multiple cases for the different values of a single variable. Syntax:-  switch(expression){     case value1:      //code to be executed;      break;  //optional   case value2:      //code to be executed;      break;  //optional   ......          default:       code to be executed if all cases are not matched;     }     Exercises:- Write a C program to print day of week name using switch case. Write a C program print total number of days in a month using switch case. Write a C program to create Simple Calculator using switch case .  

C: 6. Decision Making If

The if-else statement in C is used to perform the operations based on some specific condition. The operations specified in if block are executed if and only if the given condition is true.  There are the following variants of if statement in C language. If statement An if statement consists of a Boolean expression followed by one or more statements Syntax:- if(expression){   //code to be executed   }   Exercises:-  Write a C program to find maximum between two numbers. Write a C program to check whether a number is negative, positive or zero. If-else Statement The if-else statement is used to perform two operations for a single condition. The if-else statement is an extension to the if statement using which, we can perform two different operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness of the condition. Syntax:- if(expression){   //code to be executed if condition is true   }else{   //code to be executed if condition is false   }   E

C: 5. printf() and scanf()

The printf() and scanf() functions are used for input and output in C language. Both functions are inbuilt library functions, defined in stdio.h (header file). Let's Better Understand via the following code:-  #include<stdio.h>     int main(){     int x=0,y=0,result=0;      printf("enter first number:");   scanf("%d",&x);   printf("enter second number:");   scanf("%d",&y);      result=x+y;   printf("sum of 2 numbers:%d ",result);      return 0;   }     Output:-  enter first number:9 enter second number:9 sum of 2 numbers:18

C: 4. Operators

  An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. It tells the computer to perform some mathematical or logical manipulations. Such as + is an arithmetic operator used to add two integers. C language provides a rich set of operators. Operators are classified into the following categories based on their usage:- Arithmetic Operators Let's Suppose  A holds 10 and variable B holds 20 then − Operator Description Example + Adds two operands. A + B = 30 − Subtracts second operand from the first. A − B = -10 * Multiplies both operands. A * B = 200 / Divides numerator by de-numerator. B / A = 2 % Modulus Operator and the remainder of after an integer division. B % A = 0 ++ The increment operator increases the integer value by one. A++ = 11   -- Decrement operator decreases the integer value by one.   A-- = 9 Relational Operators Let's Suppose  variable A holds 10 and variable B holds 20 then − Operator Description Example == Checks

C: 3. Variables

  A variable is a name of the memory location. It is used to store data. Its value can be changed, and it can be reused many times . The example of declaring the variable is given below: int a;   float b;   char c;   Rules for defining variables A variable can have alphabets, digits, and underscore. A variable name can start with the alphabet, and underscore only. It can't start with a digit. No whitespace is allowed within the variable name. A variable name must not be any reserved word or keyword, e.g. int, float, etc. Invalid variable names: int double;   float month name;   char 2number;  

C: 2. Data Types

  A data type specifies the type of data that a variable can store such as integer, floating, character, etc. There are the following data types in C. Types Data Types Basic Data Type int, char, float, double Derived Data Type array, pointer, structure, union Void Data Type void

C: 1. Introduction

C programming is a general-purpose, procedural computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX operating system. C language is a MUST for students and working professionals to become a great Software Engineer. C programming is considered as the base for other programming languages, that is why it is known as mother language. Why C Programming? Easy to learn Structured language Produces efficient programs Can be compiled on a variety of computer platforms Application of C Operating Systems Language Compilers Text Editors Databases How to Use Use any one of the link to directly see it in action in your browser:- https://www.programiz.com/c-programming/online-compiler/ https://www.tutorialspoint.com/compile_c_online.php https://www.onlinegdb.com/online_c_compiler First C Program #include <stdio.h>                     //Header File int main() {                                      //Main Driver Function    pr