Tags

, , , , , , , , , ,

In C we use a keyword, an identifier, a constant, a string literal, or a symbol. Here are some basic syntax of C Language:


  • Semicolons ;
    Every C statement must end with a ;. Thus ; acts as a statement terminator. It indicates the end of one logical entity.
    Example:
    printf(“Hello! How r u?”);

  • Comments
    Though comments are not necessary, it is a good practice to begin a program with a comment indicating the purpose of the program, its author and the date on which the program was written. They are ignored by the compiler.Comment about the program should be enclosed within /* */.Any number of comments can be written at any place in the program.

    Example:

    /* my first C program */

  • Identifiers
    A C identifier is a name used to identify a variable, function, or any other user-defined item.
    A identifier name is any combination of 1 to 31 alphabets, digits or underscores. The first character of identifier must be an alphabet or underscore. No commas or blanks are allowed within an identifier.
    C is a case sensitive programming language. C does not allow punctuation characters such as @, $, and % within identifiers.

Some valid identifier are:
ils
ils_training
deepak
first_name
amit_23
myname50
retVal


  • Keywords:
    Keywords are the words whose meaning has already been explained to the C compiler. The keywords cannot be used as variable names because if we do so we are trying to assign a new meaning to the keyword, which is not allowed by the compiler.

Below is the list of 32 keywords:-

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

  • Whitespace in C
    Blank spaces may be inserted between two words to improve the readability of the statement. However, no blank spaces are allowed within a variable, constant or keyword.
    A line containing only whitespace, possibly with a comment, is known as a blank line, and a C compiler totally ignores it.

C Data Types
In the C programming language, variable(data) should be declared before it can be used in program. Data types are the keywords, which are used for ssigning a type to a variable or functions. The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted.


Data types in C:

Fundamental Data Types Derived Data Types
Integer types Floating Type Character types Arrays Pointers Structures Enumeration

Syntax for declaration of a variable
data_type variable_name;

Here, we are focusing upon the Fundamental Data Type. Array, Structure, Pointer, Enumeration will be discussed separately.


Integer data types
Keyword int is used for declaring the variable with integer type. For example:

int var1;

The size of int is either 2 bytes(In older PC’s) or 4 bytes.


Following table gives you details about standard integer types :

Type Storage Size Value Range
int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295

  • Floating-Point Types
    Variables of floating types can hold real values(numbers) such as: 2.34, -9.382 etc. Keywords either float or double is used for declaring floating type variable. For example: float var1;
    double var2;
    Here, both var1 and var2 are floating type variables.In C, floating values can be represented in exponential form as well. For example: float var3=22.442e2;

    Following table gives you details about standard floating-point types:

    Type Storage size Value range Precision
    float 4 byte 1.2E-38 to 3.4E+38 6 decimal places
    double 8 byte 2.3E-308 to 1.7E+308 15 decimal places
    long double 10 byte 3.4E-4932 to 1.1E+4932 19 decimal places

    The header file float.h required to defines these types of value and other details about the binary representation of real numbers in your programs.


  • Character Type:Keyword char is used for declaring the variable of character type. For example: char var4=’h’;
    Here, var4 is a variable of type character which is storing a character ‘h’.

    Type Storage size Value range
    char 1 byte -128 to 127 or 0 to 255
    unsigned char 1 byte 0 to 255
    signed char 1 byte -128 to 127
Advertisements