Posts

Structure of a C Program

Image
Lets understand the  basic structure of a C program. A C program is divided into different sections. There are six main sections to a basic c program. The six sections are, Documentation Include/Link/Preprocessor      section Definition Global Declarations ( Functions and variables) Main function Subprogram functions     Now let us learn about each of this layer in detail. Figure: Basic Structure Of C Program Sample Program The C program here will find the area of a circle using a user-defined function and a global variable PI holding the value of pi /* File Name: areaofcircle.c Author: Abhijith date: 06/05/2021   description: a program to calculate area of circle user enters the radius */ #include<stdio.h>    //link section #define PI    3.14;//definition section float area(float r);//global declaration /****************************/ int main()//main function { float r; printf(" Enter the radius:\n"); scanf("%f...

C operators, precedence and expressions

Image
C – Operators and Expressions The symbols which are used to perform logical and mathematical operations in a C program are called C operators. These C operators join individual constants and variables to form expressions. Operators, functions, constants and variables are combined together to form expressions. Consider the expression A + B * 5. where, +, * are operators, A, B are variables, 5 is constant and A + B * 5 is an expression. Types of C operators: C language offers many types of operators. They are, Types of Operators Description Arithmetic operators These are used to perform mathematical calculations like addition, subtraction, multiplication, division and modulus Assignmentoperators These are used to assign the values for the variables in C programs. Relational operators These operators are used to compare the value of two variables. Logical operators These operators are used to perform logical operations on the given two variables. Bit wise operators These operators are use...

Input Output - printf() and scanf() functions

Image
printf() and scanf() function in C   printf() and scanf() functions are inbuilt library functions in C programming language which are available in C library by default.These functions are declared and related macros are defined in “stdio.h” which is a header file in C language. We have to include “ stdio.h ” file as shown in below to make use of these printf() and scanf() library functions in C program. #include <stdio.h>   printf() function in C   In C programming language, printf() function is used to print the “character, string, float, integer, octal and hexadecimal values” onto the output screen. Here’s a quick summary of the available printf format specifiers:   %c           character %d           decimal (integer) number (base 10) %ld          Long integer %e           exponential floating-point number %f        ...

Header Files in C

header file is a file with extension  .h  which contains C function declarations and macro definitions to be shared between several source files.    There are two types of header files: the files that the programmer writes and the files that comes with your compiler.   You request to use a header file in your program by including it with the C preprocessing directive  #include , like you have seen inclusion of  stdio.h  header file, which comes along with your compiler.   Including a header file is equal to copying the content of the header file but we do not do it because it will be error-prone and it is not a good idea to copy the content of a header file in the source files, especially if we have multiple source files in a program. A simple practice in C  is that we keep all the constants, macros, system wide global variables, and function prototypes in the header files and include that header file wherever it is required. Include Syn...

Preprocessor Directives

In a C program, all lines that start with  #  are processed by preprocessor which is a special program invoked by the compiler. In a very basic term, preprocessor takes a C program and produces another C program without any  # . There are 4 main types of preprocessor directives:  Macros File Inclusion Conditional Compilation Other directives All preprocessor commands begin with a hash symbol (#). It must be the first nonblank character, and for readability, a preprocessor directive should begin in the first column. The following section lists down all the important preprocessor directives Sr.No. Directive & Description 1   #define Substitutes a preprocessor macro. 2   #include Inserts a particular header from another file. 3   #undef Undefines a preprocessor macro. 4   #ifdef Returns true if this macro is defined. 5   #ifndef Returns true if this macro is not defined. 6   #if Tests if a compile time condition is true. 7   #else ...

Simple Programs to Begin with

1) area of the circle given the diameter. 2)area of the circle given the circumference. 3)area of the circle given center point (x1,y1) and one point on the perimeter (x2,y2). (Hint: input the two points and compute the distance between them as radius) 4)area of a triangle given three sides a,b,c.( assume they form a triangle) 5)area and perimeter of a right angled triangle given width(w) and height(h). 6)volume and surface area of a sphere given the radius. 7)area of an equilateral triangle given one side (s). 8) volume and surface area of the cylinder given radius(r) and height(h). 9) volume and surface area of a cube given one side ( s). 10)hypotenuse of a right-angled triangle, given the length of the other two sides. Note: assume uniform input values( eg: cm) 11) Find A, the Final Investment Value, using the simple interest formula:  A = P(1 + rt) where P is the Principal amount of money to be invested at an Interest Rate R% per period for t Number of Time Periods. Where r is ...

Bitwise Operators in C

Bit level operators make C useful for many low level applications. Bitwise operations allow one to read and manipulate bits in variables of certain types. The bit wise operators only work on int and char types. The following are the bitwise operators in C Operator Description ~ Bitwise Complement <<   Bitwise Shift Left >>   Bitwise Shift Right & Bitwise AND ^ Bitwise EX-OR | Bitwise OR Bitwise operations helps to work on Boolean variable, which needs only one bit.Set membership can be represented with Boolean variables ( 1 means element in the set 0 means not in the set) and the Bitwise-AND can be used to implement set-intersection and Bitwise-OR to implement set union. The bitwise AND(&) is true only if both bits are set. The bitwise OR(|) is false only when both the bits are false. The bitwise EX-OR operation returns 1 if the input bits are different. The following chart defines these bitwise binary operations Variable Decimal Equivalent B3 B2 B1 B0 x 12...