Skip to main content

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("Floating point Constant: %f\n", float_var);   
   printf("Character Constant: %c\n", char_var);   
   return 0;   
 }   

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Comments

Popular posts from this blog

Function

Function A function is a relation between a set of inputs and a set of permissible outputs with the property that each input is related to exactly one output. Representation - Cartesian  product  of 2 sets is mapping every element to every element of other set. It is generally represented as X A={"a","b"}, B={"y","z"} then A X B= {("a","y"),("a","z"),(b,y),(b,z)} Relation: A Relation R from a non-empty set A to a non empty set B is a subset of cartesian product A X B. n(A)= number of element in set A. n(B)= number of element in set B. Let 's say n(A)=p, n(B)=q n(A X B)=pq then no of relations possible from A to B is 2^pq A relation f from a set A to a set B is said to function if every element of set A has one and only one image in set B. Relation: A function f is X to Y then f: X->Y X is called domain set, Y is called range function. Functions are generally represented as  f ( x ) L...

OFFSET in Computer Science

OFFSET in Computer Science In the post, We will understand what is the meaning of OFFSET in computer science with an example. In computer science offset comes picture at may place like packing a data structure, encoding the data etc in some standard protocol so that other parties can deserialise it and understand the data. We will understand its important how does it work with example. suppose we have a data structure for a student like { name:string[20] roll: int[4] course: string[25] batch: int[4] } We want to serialise below student data  { name:"Amit Gupta" roll: 1234 course:Computer Science" batch: 2018 } Our protocol follow the rule that we will insert a value and if space will remain empty we will fill it with $ for example length of "Amit Gupta" is 10 but standard size is 20 so will fill 10 $ after "Amit Gupta". So serialisation of above data will be Amit Gupta$$$$$$$$$$1234Computer Sc...

Install Virtual Environment

VIRTUAL ENV Virtualenv is a tool to create an isolated Python environment. In this post, we will learn how to setup virtualenv on ubuntu. Prerequisite pip Install Virtualenv If you have pip3  sudo pip3 install virtualenv if you have pip sudo pip install virtualenv Create a Virtualenv virtualenv venv venv is a directory in which virtual environment will be created. Working with Virtualenv source venv/bin/activate Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

What is the difference between library and framework?

What is the difference between library and framework? In this post, We will learn about the very confusing term library and framework that widely used in software. Library and Framework Library: Library is a code repository that your code call. For example, curl is a library that can be called by your code. Framework: In a framework, framework call your code. For example, Django is a framework in which you keep your code in a particular structure can Django call your code compilation and execution purpose. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.