C Keywords

keyword is predefined reserved word. the compiler has special meaning of this keywords. There are use in declare variable and statements. c programming language there are only 32 keywords.

Keyword list

Description of c keywords


int

int is data type. integer variable are capable to store the value of 2 bytes for 32 bit compiler ,and 4 bytes of 64 bit compiler. declare integer variables as follows.

  int variable_list;

int : keyword
variable_list : variable name

/* 
  Example to use int keyword
*/
/*Header file*/
#include <stdio.h>

  /*
    int    : keyword
    global : variable name
  */
int global=12;
int main(){
  /*
    int   : keyword
    local : variable name
  */
  int local=10;

  printf("\n Global : %d ",global);
  printf("\n Local  : %d ",local);
  return 0;
}

Output:

 Global : 12 
 Local  : 10 

int keyword also use when function return integer value

float

float is an data type of c programming language.

/* 
  Example to use float keyword
*/
/*Header file*/
#include <stdio.h>

//function declaration
float sum(float no1,float no2);

//return sum of two float numbers
float sum(float no1,float no2){
  return no1+no2;
}
int main(){
  //float variables
  float num1=12.0f,num2=10.0f;
  printf("Sum of [%f + %f] is : %f",num1,num2,sum(num1,num2));
  return 0;
}

Output

Sum of [12.000000 + 10.000000] is : 22.000000

char

char is an data type. compiler is allocate one byte memory of character variables.

/* 
  Example to use char keyword
*/
/*Header file*/
#include <stdio.h>

int main(){
  //char variables
 char capital_a='A',small_a='a';

  printf("Value of capital_a is : %c ASCII [%d]\n",capital_a,capital_a);
  printf("Value of small_a is   : %c ASCII [%d]\n",small_a,small_a);
  return 0;
}
Value of capital_a is : A ASCII [65]
Value of small_a is   : a ASCII [97]

double

double is an data type.

/* 
  Example to use double keyword
*/
/*Header file*/
#include <stdio.h>
//function declaration
double salary(int);

double salary(int days){
  double count=1000;//per day
  return days*count;//return double value
}
int main(){
  int day=10;
  //double variable
  double money=salary(day);
  printf("Joy is works %d day their salary is %lf ",day,money);
  return 0;
}

Output

Joy is works 10 day their salary is 10000.000000

long

/* 
  Example to use long keyword
*/
/*Header file*/
#include <stdio.h>
int main(){
  //long keyword use before int and double
  long int number=10;
  long double dbl_value=123.33;

  printf("long int number : %ld\n",number);
  printf("long double dbl_value : %Lf\n",dbl_value);
  return 0;
}

Output:

long int number : 10
long int dbl_value : 123.330000

signed

/* 
  Example to use signed keyword
*/
/*Header file*/
#include <stdio.h>
int main(){
  //signed keyword use before int
  signed int ivar=10;
  printf("signed int ivar : %d\n",ivar);
  return 0;
}

Output:

signed int ivar : 10

unsigned

/* 
  Example to use unsigned keyword
*/
/*Header file*/
#include <stdio.h>
int main(){
  //unsigned keyword 
  unsigned int postive_int=10;
  printf("unsigned int postive_int: %i\n",postive_int);
  return 0;
}

Output:

unsigned int postive_int: 10

short

/* 
  Example to use short keyword
*/
/*Header file*/
#include <stdio.h>
int main(){
  //short keyword 
  short int month=10;
  printf("short int month: %d\n",month);
  return 0;
}

Output:

short int month: 10

const

/* 
  Example to use const keyword
*/
/*Header file*/
#include <stdio.h>
//constant PI variable
const float PI=3.14159265;
int main(){
  //constant max_month int variable
  const int max_month=12;
  printf("Value of PI : %f\n",PI);
  printf("Value of max_month : %d",max_month);

  return 0;
}

Output:

Value of PI : 3.141593
Value of max_month : 12

do

/* 
  Example to use do keyword
*/
/*Header file*/
#include <stdio.h>

int main(){
  int month=1;
  //do is keyword

  do{
    printf("%d\n",month);//print month value
    month++;
  }while(month<=12);//terminate condition 
  
  return 0;
}

Output:

1
2
3
4
5
6
7
8
9
10
11
12

while

/* 
  Example to use while keyword
*/
/*Header file*/
#include <stdio.h>

int main(){

  int counter=1;
  //while is keyword
  while(counter<=5){
    printf("counter : %d\n",counter);
    counter++;
  }

  return 0;
}

Output:

counter : 1
counter : 2
counter : 3
counter : 4
counter : 5

for

/* 
  Example to use for keyword
*/
/*Header file*/
#include <stdio.h>

int main(){
//for is keyword
 for(int i=1;i<=10;i++){
  printf("%d\n",i*2);//print
 }
  return 0;
}

Output:

2
4
6
8
10
12
14
16
18
20

if

/* 
  Example to use if keyword
*/
/*Header file*/
#include <stdio.h>

int main(){

  if(10>5){
    printf("10 is greater than 5");
  }
  return 0;
}

Output:

10 is greater than 5

else

/* 
  Example to use else keyword
*/
/*Header file*/
#include <stdio.h>

int main(){

  if(10>15){
    printf("10 is greater than 15");
  }else{
    printf("10 is less then 15");
  }
  return 0;
}

Output:

10 is less then 15

continue

/* 
  Example to use continue keyword
*/
/*Header file*/
#include <stdio.h>

int main(){
  int status=8;
  for(int i=1;i<status;i++){
    if(i==3||i==5) continue;//continue statement
    printf("%d\n",i);
  }
  return 0;
}

Output:

1
2
4
6
7

break

break statement are use inside a loop and switch case. that are stop the execution of loop and switch case.

/* 
  Example to use break keyword
*/
/*Header file*/
#include <stdio.h>

int main(){
  int i=1;
  for(i;;i++){
    printf("%d\n",i);
    if(i==4){
      break;//break statement
    }
  }
  
  switch(i){
    case 1:
    printf("Case 1 execute\n");
    break;//break statement
    case 3:
    printf("Case 3 execute\n");
    break;
    case 4:
    printf("Case 4 execute\n");
    break;
    default:
    printf("Missing case\n");
  }
  return 0;
}

Output:

1
2
3
4
Case 4 execute

switch

/* 
  Example to use switch keyword
*/
/*Header file*/
#include <stdio.h>

//function declaration
void message(int,int,int);
void sum(int,int);
void sub(int,int);
void mul(int,int);

void sum(int value1,int  value2){
  printf("Sum [%d + %d] is : %d\n",value1,value2,value1+value2);
}
void sub(int value1,int  value2){
  printf("Sub [%d - %d] is : %d\n",value1,value2,value1-value2);
}
void mul(int value1,int  value2){
printf("Mul [%d * %d] is : %d\n",value1,value2,value1*value2);
}

void message(int i,int num1,int num2){
  //switch case
  switch(i){
    case 1:
    sum(num1,num2);
    break;
    case 2:
    sub(num1,num2);
    break;
    case 3:
    mul(num1,num2);
    break;
    default:
    printf("Invalid option\n");
  }
}
int main(){
  
  message(1,5,2);
  message(3,5,4);
  message(5,2,3);
  return 0;
}

Output:

Sum [5 + 2] is : 7
Mul [5 * 4] is : 20
Invalid option

default

/* 
  Example to use default keyword
*/
/*Header file*/
#include <stdio.h>

void task(int info){
  switch(info){
    case 1:
    printf("value is 1\n");
    break;
    //default
    default :
    printf("This is default case value is: %d\n",info);
  }
}

int main(){
  task(1);
  task(3);
  return 0;
}

Output:

value is 1
This is default case value is: 3

sizeof

/* 
  Example to use sizeof 
*/
/*Header file*/
#include <stdio.h>

int main(){
  printf("size of int : %zu\n",sizeof(int));
  printf("size of char : %zu\n",sizeof(char));
  printf("size of float : %zu\n",sizeof(float));
  printf("size of double : %zu\n",sizeof(double));
  return 0;
}

Output:

size of int : 4
size of char : 1
size of float : 4
size of double : 8

union

/* 
  Example to use union 
*/
/*Header file*/
#include <stdio.h>

int main(){
  union Employee{
    int id,age;
   double salary;
   
  };
  union Employee s1;
  //basic example to print size of union
  printf("sizeof union Employee s1 : %zu",sizeof(s1));
 
  return 0;
}

Output:

sizeof union Employee s1 : 8

enum

/* 
  Example to use enum 
*/
/*Header file*/
#include <stdio.h>
enum weekDay{mon=1, tue, wed, thur, fri, sat, sun}; 
int main(){
  int i=0;
 for(i=mon;i<=sun;i++){
  printf("day : %d\n",i);
 }
  return 0;
}

Output:

day : 1
day : 2
day : 3
day : 4
day : 5
day : 6
day : 7

struct

/* 
  Example to use struct 
*/
/*Header file*/
#include <stdio.h>
//define structure of book
struct Book{
  int pages;
  float price,weight;
};
int main(){
  //start execution
  struct Book b1,b2;
  b1.pages=200;
  b1.price=500.24;
  b1.weight=450;

  b2.pages=100;
  b2.price=300.34;
  b2.weight=350;

  printf("Book b1 info\n");
  printf("Pages %d\n",b1.pages );
  printf("Weight %f\n",b1.weight );
  printf("price %f\n",b1.price );
  
  
  printf("\nBook b2 info\n");
  printf("Pages %d\n",b2.pages );
  printf("Weight %f\n",b2.weight );
  printf("price %f\n",b2.price );
  return 0;
}

Output:

Book b1 info
Pages 200
Weight 450.000000
price 500.239990

Book b2 info
Pages 100
Weight 350.000000
price 300.339996

typedef

/* 
  Example to use typedef 
*/
/*Header file*/
#include <stdio.h>
//define structure of book
typedef struct Book{
  int pages;
  float price,weight;
}bookinfo;
int main(){
  //start execution
  bookinfo b1,b2;
  b1.pages=20;
  b1.price=60.24;
  b1.weight=250;

  b2.pages=100;
  b2.price=300.34;
  b2.weight=350;

  printf("Book b1 info\n");
  printf("Pages %d\n",b1.pages );
  printf("Weight %f\n",b1.weight );
  printf("price %f\n",b1.price );
  
  
  printf("\nBook b2 info\n");
  printf("Pages %d\n",b2.pages );
  printf("Weight %f\n",b2.weight );
  printf("price %f\n",b2.price );
  return 0;
}

Output:

Book b1 info
Pages 20
Weight 250.000000
price 60.240002

Book b2 info
Pages 100
Weight 350.000000
price 300.339996

auto

/* 
  Example to use auto 
*/
/*Header file*/
#include <stdio.h>

int main(){
  //auto storage class
  auto int value= 10;
  printf("Value : %d",value);

  return 0;
}

Output:

Value : 10

register

/* 
  Example to use register 
*/
/*Header file*/
#include <stdio.h>

int main(){
  //register storage class
  register int value= 10;
  printf("Value : %d",value);

  return 0;
}

Output:

Value : 10

extern

/* 
  Example to use extern 
*/
/*Header file*/
#include <stdio.h>

int main(){
  //extern storage class
  extern int no1,no2;
  printf("no1 : %d\n",no1);
  printf("no2 : %d\n",no2);

  return 0;
}
int no1=10,no2=12;

Output:

no1 : 10
no2 : 12

static

/* 
  Example to use static 
*/
/*Header file*/
#include <stdio.h>

void func_call(){
  static int status=1;
  printf("status : %d\n",status);
  status++;
}
int main(){
  //calling function
  func_call();
  func_call();
  return 0;
}

Output:

status : 1
status : 2

goto

goto statement is a jump statement. there are capable to change the execution flow within program. look at view this example.

/* 
  Example to use goto 
*/
/*Header file*/
#include <stdio.h>

int main(){
  //start execution of program
  int num=1;
  lable:
  printf("%d\n",num);
  num++;
  //condition
  if(num<5)
  goto lable;//goto level

  return 0;
}

Output:

1
2
3
4

return

/* 
  Example to use return 
*/
/*Header file*/
#include <stdio.h>
//function declaration
int sum(int ,int );

int sum(int no1,int no2){
  return no1+no2;// return sum of two number
}
int main(){
  //start execution of program
  int num1=3,num2=10;
  printf("\nSum is : %d",sum(num1,num2));
  return 0;
}

Output:

Sum is : 13

volatile

volatile is a qualifiers that is applied to c variable when declared variable. compiler and understand that behaviour. the volatile variable are change at any time without any action required. look at view this example we are change constant variable value.

/* 
  Example of volatile 
*/
/*Header file*/
#include <stdio.h>

int main(){
  
  const volatile int num=10;
  
  //typecast then assign const volatile variable address
  int *ptr=(int*)&num ;

  printf("\n Num is : %d",num);
  *ptr=12;
  
  printf("\n*ptr : %d",*ptr);
  return 0;
}

Output:

 Num is : 10
*ptr : 12

void

void is data type there are use in following situations.
1) when function are not returning any value.
2) when function are not accept any arguments.
3) using pointer data type

Example:

/* 
  Example of void 
*/
/*Header file*/
#include <stdio.h>
//function declaration
void info(int);
void message(void);
//in this case void are use function return type
void info(int num){
  printf("Value of num is : %d\n",num);
}
//in this case void are use function return type and paramter list
void message(void){
  printf("Hello programmers\n");
}
int main(){
  message();
  int num=10;
  info(num);
 
  void *ptr=&num ;
  printf("Value of void *ptr : %d\n",*((int*)ptr));
  return 0;
}

Output:

Hello programmers
Value of num is : 10
Value of void *ptr : 10

© 2019, regularcodes.com, All rights reserved