Skip to main content

Posts

Showing posts from September 9, 2018

2-D Array In C

2-D Array in C Programming Language In this post, We will learn about the 2-D array in c programming language. Array In C   An array of arrays is known as a 2D array. Declare Two-Dimension Array in C In C program we declare a 2-D array same as an array just at the end we mention the number of the column element we want to store in the array. Syntax to declare a 2-D array data_type array_name[number_of_row element][number_of_column_element]; Example: Declare a 2-D array having 2 row and 2 columns int my_integer_array[2][2]; Initialize an Array in C We can initialize an array of the array by putting values in the curly bracket. Example: Declare an array of array with 3 rows and 4 columns and initialize it. int my_integer_array[3][4] = { {8, 6, 5, 4}, {2, 1, 9, 7}, {3, 6, 4, 2} }; Access Elements of a T...

Constant and Macros in C

Constant and Macros in C In this tutorial, We will learn about constant and macros in C programming language. Constant in C In C program we use the const keyword to define a constant variable. #include <stdio.h> int main() { const int int_const_var = 10; // int constant const float float_const_val = 4.14; // Real constant const char char_const_val = 'A'; // char constant printf("Integer constant :%d \n", intVal ); printf("Floating point constant : %f \n", floatVal ); printf("Character constant : %c \n", charVal ); return 0; } Macros in C A Macro is typically an abbreviated name given to a piece of code or a value. In C program we achieve this with help of keyword #define #include<stdio.h> #define int_var 10 #define float_var 4.5 #define char_var 'A' int main() { printf("Integer Constant: %d\n", int_var); printf("...

Function and Array in C

Function and Array in C In this tutorial, we will learn about function and array in the C programming language like how to pass and use and one dimension and multi-dimension array in C program. One-Dimension Array and Function in C While passing arrays as arguments to the function, only the name of the array is passed #include <stdio.h> float average(float number[]) { int i; float avg, sum = 0.0; for (i = 0; i < 6; ++i) { sum += age[i]; } avg = (sum / 6); return avg; } int main()​ { float avg, number[] = { 23.4, 55, 22.6, 3, 40.5, 18 }; avg = average(number); // Only name of array is passed as argument. printf("Average age=%.2f", avg); return 0; } Two-Dimension Array and Function in C When dimensions available globally we can pass two dimension array and use it similar to one dimension array. Example #include <stdio.h> const int M = 3; const int N = 3; v...

Enum in C Program

Enum in C Programming Language In this post, We will learn about enumeration (enum) and its uses. Introduction An enumeration is a user-defined data type that consists of integral constants. To define an enumeration, the keyword enum is used. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain. Example: #include<stdio.h> enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun}; int main() { enum week day; day = Wed; printf("%d",day); return 0; } Important Points about Enum Two enum names can have the same value  If we do not explicitly assign values to enum names, the compiler by default assigns values starting from 0 We can assign values to some name in any order The value assigned to enum names must be some integral constant Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Scope of Variable in C

Scope of Variable in C In this tutorial, We will learn about scoping rule in C programming language. Introduction A scope is a region of the program, and the scope of variables refers to the area of the program where the variables can be accessed after its declaration. There is three type of variable with respect to the scope of the variable. Local Variables Global Variable Formal Parameter Local Variable Variables that are declared within the function block and can be used only within the function is called local variables. #include <stdio.h> int main () { int x,y,z; // x, y, z local variable definition // actual initialization x = 20; y = 30; z = x + y; printf ("value of x = %d, y = %d and z = %d\n", x, y, z); return 0; } Global Variable Variables that are declared outside of a function block and can be accessed inside the function is called global variables. #include <stdio.h> int z...

Type Casting in C

Type Casting in C In this tutorial, We will learn to typecast in the C programming language. Introduction Typecasting is a way to convert a variable from one data type to another data type. Example : Typecast Integer datatype into Float datatype in C.  /* * C program to typecast integer datatype into float datatype */ #include <stdio.h> int main() { int var_integer = 5; float var_float = (float) var_integer; // typecasting int into float printf("%f\n", var_float); return 0; } Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

File in C Programming Language

File in C Programming Language In this tutorial, We will learn about file management in the C programming language. Introduction File management in C, File operation functions in C, Defining and opening a file, Closing a file, reading a file and writing to a file. File Operation function in C Company Name Oil and Natural Gas Corporation fopen() Creates a new file for use Opens a new existing file for use fclose() Closes a file which has been opened for use getc() Reads a character from a file putc() Writes a character to a file fprintf() Writes a set of data values to a file fscanf() Reads a set of data values from a file getw() Reads a integer from a file putw() Writes an integer to the file fseek() Sets the position to a desired point in the file ftell() Gives the current position in the file rewind() Sets the position to the begining of the file Read File in C /* * C program to illustrate how a file stored on the disk is read */ #include ...