Format of C

The Format of C 

1
2
3
4
5
6
7
#include <stdio.h> 				/* comment */ 
int main(void)
{
  printf("Hello\n");  	
  printf("Welcome to the Course !\n");    	
  return 0;
}

  

  • Statements are terminated with semicolons
  • Indentation is ignored by the compiler like Comment
  • C is case sensitive - all keywords and Standard Library functions are lowercase
  • Strings are placed in double quotes
  • Newlines are handled via \n
  • Programs are capable of flagging success or error, those forgetting to do so have one or other chosen randomly! 

 Another Example

1
2
3
4
5
6
7
8
9
#include<stdio.h>
int main(void)
{
  int a,b;  	
  printf("Enter two Numbers :");  	
  scanf("%i %i", &a,&b);  	
  printf("%i - %i = %i\n",a,b,a-b);  	
  return 0;
}

 here int keyword is used to create integer variables. here two variables a and b are created.

Variables

  • Variables must be declared before use immediately after begin “{“.
  • valid characters are letters, digits and ”_”.
  • First character cannot be a digit.
  • 31 characters recognised for local variables (more can be used, but are ignored).
  • Some implementations recognise only 6 characters in global variables (and function names)!.
  • Upper and lower case letters are distinct.

printf and scanf

  • printf writes integer values to screen when %i is used.
  • scanf reads integer values from the keyboard when %i is used.
  • “&“ VERY important with scanf (required to change the parameter, this will be investigated later) - absence will make program very ill.
  • “&“ not necessary with printf because current value of parameter is used.

Integer Types in C

  • C supports different kinds of integers
  • maxima and minima defined in “limits.h”

type                             format         bytes  
char                             %c                   1         
signed char                  %c                   1         
unsigned char             %c                   1         
short [int]                    %hi                  2         
unsigned short             %hu                 2         
int                                %i                    2         
unsigned int                %u                   2or4
long [int]                     %li                   4
unsigned long              %lu                  4

Integer Example

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
#include <limits .h>
int main(void)
{
  unsigned long big = ULONG_MAX;   
  Printf(”minimum int = %i,  INT_MIN);   
  printf(”maximum int = %i\n, INT_MAX);   
  printf(”maximum unsigned = %u\n, UINT_MAX);  
  printf(”maximum long mnt = %li\n, LONG_MAX);  
  printf(”maximum unsigned 1ong = %lu\n", big);
  return 0; 
}

Output

minimum int = -32768, maximum int = 32767

maximum unsigned = 65535 

maximum long int = 2147483647

maximum unsigned long = 4294967295

Integer With Different Bases

It is possible to work in octal (base 8) and hexadecimal (base 16)

1
2
3
4
5
6
7
8
#include <stdio.h>
int main(void)
{   
	int dec = 20, oct = 020, hex = 0x20;
    printf("dec=%d, oct=%d, hex=%d\n", dec, oct, hex);  
    printf("dec=%d, oct=%o, hexe%x\n", dec, oct, hex);
    return 0; 
}

 Output

dec=20, oct=16. hex=32
dec=20, oct=20, hex=20

Named Constants

Named constants may be created using const 

1
2
3
4
5
6
7
8
9
#include <stdio.h> 
int main(void) 
{   
	const  long double pi = 3.141592653590L;
  	const int days_in_week = 7;   
  	const sunday = 0;   
  	days_in_week = 5;   
  	return 0; 
} 

Preprocessor Constants

Named constants may also be created using the Preprocessor
Needs to be in “search and replace” mode
Historically these constants consist of capital letters 

1
2
3
4
5
6
#include <stdio.h>
#define	PI            3.141592653590b 
#define	DAYS_IN_WEEK   	     7 
#define	SUNDAY               0 
int day = SUNDAY;
long flag = USE_API;

 

 

About Us

  • Micromagine Embedded System Design
    3rd Floor,
    Triveni Sangam Appartment,
    Opp. Divyabhaskar Office,
    Kishan Para Chowk,
    Rajkot - 360001

  • info@micromagine.com
    info@micromagine.com

  • (+91) 9429160113

Services & Support

Premium Joomla Templates