explanation of 32 keywords in C

hey guys welcoooome back today we are going to discuss all the

32 keywords in C

 (😅,a lot but ill make it interesting for you so letsss goooo...)




      continue              float                   enum                   for        
          break       double         struct             while
         switch       short         union           do
         case          long        typedef          goto
        default        signed         auto          return
         const       unsigned          extern          sizeof
         int           if        register         void
        char          else         static        volatile

(click on the keyword to know about it)



continue

continue keyword is  also use  as the break does but it is not use in switch and the use of continue is a little diffrent as of break statement ,when the compiler encounters continue statement rest of another statement are skipped and control is transferred to the next continuation of loop,simplest eg is ..






this program will print 1 -10 except the 5 because of continue statement will get executed and the rest of the statements  followed by it are then ignored and then control is transfred to next iteration of for loop.

(Back to top)

break

break keyword is used to stop execution of a loop or it is used in switch statement to stop after execution of the particular case


 


in this case it will print till 2 when value of i will be 3 then it will come out of loop as if will be executed (note- break is not for if it is  use for  for loop)

break in switch
in this case  case 2 will get executed and break will force the control to get out of switch

(Back to top)



 switch  case  and default

switch statement is used in multi-way decision making but is simpified version of if-else (but you cant use the variable in cases,so bad thing about is there is no meant to use of logical and relational operator in it),this is the basic structure..


case and default keywords are use in switch statement case is used to provide different cases inside the switch (in this case are 1,2,3) and default statement specifies that if non of the above case specified gets executed the default will work.



in this case  case 2 will get executed and choice is 2 will get printed on screen.

(Back to top)


const

const is a keyword which is use to declare and variable or pointer of constant type (which means you cannot change value or address pointer is pointing to) for eg

for variable 

the above program will give error because we are trying to change the value of a constant variable 
on line 7

eg of constant pointer:-..


constant pointer
this will not give us error because here the pointer is constant pointer means you cannot change the address it is pointing to but you can change the value at that address but cannot reinitialize .

eg of pointer to constant:-..

pointer to constant integer
this will give us error because here the pointer is to a constant integer , you can change the address or reinitialize it but cant change the value it is pointing to.(you can also write const int *a on line 6 means the same).



(Back to top)



int 

int keyword basically used for defining a variable of integer type of can be used to define a function with return type int (means function returning a integer),it's range lies between -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 and size of integer variable is of 2 or 4 bytes and the format specifier used for displaying or reading is %d and syntax is given by...


where a is variable of integer type 

(Back to top)


 char

char keyword basically use for defining a variable of character type or can be use to define a  function with return type char  (means function returning a character ),it's range lies between -128 to 127 and the size of a character variable is 1 byte and format specifier used for displaying or reading is %c syntax is ..



here, a is variable of character type and vi is function of return type char and this program will print value a.




hey dont forget to  take the break ,drink water ,eat uncle chips and then continue further (kidding 😅),just take a remind of what you had learned from above✌


(Back to top)


float 

float is the (basic data type in c) keyword used to declare a  variable which can hold the decimal values ,whose range lies in between 1.2E-38 to 3.4E+38 ,size in bites taken by a float variable is 4 and format specifier is %f  ,synatax is...



(Back to top)


double

double keyword is used to declare variable which can hold decimal values unlike float it is used to store big floating type values so  you can clearly can think of that range is also bigger which is 2.3E-308 to 1.7E+308 and size in bytes taken by double is 8 and format specifier used in %lf





(Back to top)


short,long,signed and unsigned

short,long,signed and unsigned are the modifier which change the meaning of basic data type when used with them,


Data typesRangeSize in bytesFormat specifier
char-128 to 1271%c
unsigned char0 to 2551%c
int -32768 to 32767 or -2,147,483,648 to 2,147,483,6472%d
unsigned int0 to 655352%u
short int-32768 to 327672%hi
unsigned short
 int 
0 to 655352%hu
long int-2147483648 to 21474836474%ld
long long int-(2^63) to (2^63)-18%lli
unsigned long
int
0 to 42949672954%lu
unsigned long long int0 to 18,446,744,073,709,551,6158%llu
float1.2E-38 to 3.4E+384%f
double2.3E-308 to 1.7E+3088%lf
long double3.4E-4932 to 1.1E+493210%lf

                                                                     


1.) so what does signed means that is no can be -ve and +ve both ,

2.) unsigned means is that it can contain only +ve number but range is extended

3.) short basically signifies that short value of that basic type and range is less and size is also short

4.) long is basically long value of that basic data type ,range is more and size is also large
(don't worry you don't have to summarize them all just summarize the basic data types range and size ,just do the basic ,but remember the size it would be helpful to you)                           
                                                                              

(Back to top)


if  and else 

in c programming if and else keyword are use for decision making ,the statement in if block is basically c valid expressions (you can also write printf and scanf ) ,that may include logical operators and there is no semicolon after the expression (note it is possible to have if statement without else but not else statement without if) ..



so this is the basic eg in which value of a is assigned 100 in which else will be executed as no value matches above .



(Back to top)

enum

enum keyword is used to to define a user define datatypes , in an enumerated type an integer value is assigned to its member , syntax is ....


1.)here week is the name of name of enumeration and day is the name of enumeration variable 
but it is your wish if you wanna specify  the name of enumeration and enumeration variable (means they both are optional) 

2.) only integers value can be assigned or are default assign ,what that mean that if you will print Mon in above given syntax eg  then it will print 0 (assigning by default start from value 0 and then go on incrementing the value by 1).

3.)


as you can see in this program i had assign the values so no default value are give you can assign any particular value to any and the next element would be given 1 incremented value if not assign , here i have  not assign Sun so  here sun is given 7 because sat is assign value =6.

4.) there is one more way to declare variable as shown in the above give program on line 7 where day is variable of enum week type ( i personally don't find any use of enum variable , you can also use an  int variable instead of enum week day that would also give the same result )

5.) you can also print the directly in this i had directly printed Mon that is assign value 1



 that would also work.

6.) you can also assign the same value to two or more given elements in list (let say Mon and Tue are assign value 2 that would also work) and the scope or enum variable follow the same scoping rule of that of normal variable in c .

(Back to top)


struct and union

struct and union are two basically two key words  which are used to define the user defined data types but are very different from the enum which is also user to define user defined data types ,syntax is shown  ...



1.) on line 13 p2 is structure variable and on line 18 p1 is union type variable (you can declare variable for both union and structure in the main like that of union had declare or like that of structure on line 13)

2.) Size of union is the size of  largest member of union in this case both have the same size so size will be 4 only and size of struct is size of its all members in this case size of struct point is 8 as int and float are of size of 4 bytes(but it can vary some time for structure ,this we will discuss when we will go though the topic structure).

3.) in structure you can store the value to the no of members it have but in case of union you can assign only one value to any given member of it because they share the same memory ,so as you will assigne the other member a value your previous assign value would be gone but it is not the case with structure as they have seperate memory assigne to all the members.

(Back to top)


typedef

typedef is a keyword which is used to give an nick name or alias name to exsisting datatype

synatx is 
]

so here on line 5 i had assigned int nick name(that dosen't mean that we cant use int) that is olala which will also declare the integer variable now .
           

(Back to top)


auto

auto is the keyword use to declare the variable with auto storage class (what that means is..) you  can only use that auto variable with in only the block you  have declare in (block can be function block,if block,for loop) and every variable declared inside a block have default auto storage class so you dont have to use the keyword auto.






in this case a is declare as  auto variable
1.1) if you don't initialize it will contain some garbage value.
1.2) you should declare it before using otherwise it will give you error.

(Back to top)


extern

extern keyword is used to give a reference of global variable that has been declare in another file so that we can use that global variable in the current program (ya i know thats too confusing ,so lets take a eg..

in the below given program you can see i have declared a function print whose definition is in another file vasihi.c and on line 4 i had included the file vasihi.c(i had used quotes because my file is in current directory so quotes will make it search in current directory ).



this is my file vasihi.c in which print function is defined and on line 2 i had use extern to give reference to global variable a that had been declared in the vasi hi 13.c or the above program.


so when the program in file vasi hi 13.c gets executed it will print the value 2  and then function print will be called which is in file vasihi.c and then again value 2 is printed.

(note:- define the main function only once otherwise it will give you error)

(Back to top)


register

register keyword is used to declare a variable using register storage class what that mean is that it is stored in cpu register rather than RAM, so register variable does not have address ,register variable are use for qucik access of to variable when needed,the scope of register is same as that of auto
eg:..

program using register variable
in the above given program im calculating the 5 rasie to power 3 so in funciton expp i had declared res as a register variable as im using it  in for loop .

(note :- it is the suggestion to compiler to declare register variable but some time it reject because of the hardware and implementation restriction ,as a girl reject you when you propose her😂,so after it getting  reject it behave as normal variable)😆


bbreak timmee🕙,dont forget to miss you'r crush😁 (hard timess..)


(Back to top)


static

static keyword is used to declare the static variable what that means  is that it has a life time  over the entire program but the scope is limited to only the block you had declared , the default storage class for all global variable is static,a common eg is ...

static variable 
in the above given program i had define a function fun in which i had declared the static variable a so when the first time fun function is called the a is initialize to 2 and after incrementing it will be printing 3 and when the next time fun function is called the it will not reinitialize but it will contain the previous value 3 and the after incrementing again it will print 4 .

(note::- if you will not initialize it at compile time the it will default contain the value 0).

(Back to top)


for 

for,while and do are three loops which means to actually repeat the specific code for given no of times..

syntax for for loop is



step1 :-  step 1 is to intialize the variable to a value 0 or what you like which is placed at the first  position and will be used  only once when you enter the for loop for first time

step2:- step 2 is the condition if the condition gets true then the loops statement will be executed otherwise statement after the loop will get executed

(after step 2 the statement placed inside the for loop are executed before incrementation)

step3 :- step 3 is the incrementation statement which is placed at the 3rd position

step4 :- condition is checked and statements inside loop are executed

(form the second iteration of for loop above written step3 and step4 are executed until the condition of loop become false)

(one interesting thing about for loop is you can place any no of incrementation statements ,initialization ,and conditions separated by , but  for conditions logical operator should be used).


(Back to top)


while

synatx for while loop is ...



step1 :-  step 1 is to intialize the variable to a value 0 or what you like which is placed outside the loop

step2:- step 2 is the condition if the condition gets true then the loops statement will be executed otherwise statement after the loop will get executed

step3 :- step 3 is the incremtation statement which is placed inside the loop,if you forget that the loop will be executed infinite times.




do

do while is another type of loop which is simmilar to while but where the condition is checked after the execution of statements in it (so if your condition is false for the first time then also your statement inside the loop will be executed for 1 time) ,syntax is...





goto

goto statements are used to transfer control form 1 statement  to another statement synatx for goto statement is....



forward jump goto statement
goto  Label 
        ............... 
         ...............
Label:
                             statements under Label

backward jump goto statement


Label:
                             statements under Label
        goto  Label 
                 ...............
                 ...............

eg for goto statement is


in this program if condition will gets true until value is greater than 10 so goto statement will be execute and the control will goes to repeat on line 8 and the again for loop will execute (this is eg of backward jump goto).

(Back to top)


return 

what basically return does is that it return a value  to the calling function or it basically  signifies the end of function if no return statement is written  then after executing the last statement of function the control automaticaly goes to calling function ,eg :-


in the above given example i had specified return statement in function fun on line 7 so the printf statement on line 10 will never be executed as return will be transfered to calling function and a will get will 7 and will be printed on screen.

(note:- you can also write expression in return statement and if the expression value does not matches with the return types of function then it will implicitly type caste it).


(Back to top)


sizeof

sizeof operator gives the size of data(variable,constant,datatype),syntax

 sizeof(variable,constant,datatype);

eg



(Back to top)


void

void keyword when used with the function means it is returning no value , if it is used with pointer then it will indicate that it is universal pointer (what that mean is you can take the address of any data type variable you want but when you want you use it's value you have to implicitly type cast it)eg..




(Back to top)


volatile

if you use volatile keyword with the declaration of variable it will tell the compiler to stop its optimization (now what that means is .. for a simple code for eg..)


where unit32 status is a special variable which can be changed form outside  of current program,but compiler does not know about it , because of so much technology development compiler will optimize this code in such a way that less machine code is needed in executable by removing the unnecessary machine code which is  not changing variable value , so compiler will see this that status is not changing value so it will directly display infinite loop and machine code is not needed so to stop compiler optimization we use volatile keyword 


(Back to top)


(leave the comment down below if you have a doubt or suggestions ,thnks🙂)

No comments:

Post a Comment

Contact Form

Name

Email *

Message *