Basic Syntax of C Programming Language

Basic Syntax of C Programming Language
Comments

Comments are ignored by the compiler. We can comment in two ways. 1) Single line comment, start  with // ; 2) Multiple line comment. They start with /* and ends with the */. We can also use it for single line comment.

/* single line comment */

// single line comment

/* multiple line comment.
This is my 1st program.
*/


Identifiers

Identifier is a name, which is used to identify a variable, function, or any other user-defined item. It is started with a letter a to z, A to Z, or an underscore ‘_’ followed by zero or more letters, underscores, and digits (0 to 9).

C language does not allow some characters such as %, $, and @ within identifiers. It’s a case-sensitiveprogramming language. Thus, Himuand himu are two different identifiers. Here are some examples of acceptable identifiers −

mhud            john    def   next_name   c_193

urname50   _name   a      j93b9           petVal

Keywords

auto     break   case    char    const    continue           default             do        double             else     

enum   extern  float    for      goto     if                      int                    long     register             return

short    signed  sizeof  static   struct    switch              typedef         union   unsigned          void

while     volatile            _Packed

The above list shows the reserved words in C. We can’t use these words as variables or constants or any other identifier names.

Variables in C Programming Language

Variable is used to store data, which is a name of memory location. We can reuse it many times; its value can be changed.

Syntax to declare a variable:

type variable_name;  

Example of declaring variable:

ü  int m;  

ü  float n;  

ü  char o;  

We can also provide values while declaring the variables:

ü  int m=10;

ü  float n=20.8, p=10.3;  //declaring 2 integer float variable

ü  char o=’K’;  

ü  C language.

Data Types in C Programming Language

    Type              Data Types
   Basic       int, char, float, double
 Derived       array, pointer, structure, union
Enumeration       enum
    Void       void

Operators in C Programming Language

An operator is a symbol which is used to perform operations. There are many types of operations. Like bitwise, assignment, arithmetic, relational, logical, shift, bitwise, ternary or conditional Operator, misc Operator

Arithmetic Operators

The following are arithmetic operator.

+, −, *, /, %, ++, —

Relational Operators

The following are Relational operator.

==, !=, >, <, >=, <=

Logical Operators

The following are Logical operator.

And: &&, or: ||, not: !

Bitwise Operators

The following are Bitwise operator.

&, |, ^, ~, <<, >>

Assignment Operators

The following are Assignment operator.

=, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=

Misc Operators

The following are Misc operator.

sizeof(),&,pointer: *, ? :

Conditional Operators

The following is Conditional operator.

?:

Constants in C Programming Language

We can define constant in 2ways.

1.       const keyword

const float PI=3.14;  

2. #define preprocessor
Syntax:

#define token value  

Example:

#define PI 3.14  

printf() & scanf()

To print something/output, we have to use printf()function. And if we need to input something from user we need to use scanf() function. Both function are defined in stdio.h (header file). We also called them build in function.
The syntax of printf()& scanf() function:

printf(“My name is Crazy Programmer.”);  

Output: My name is Crazy Programmer.

scanf("format_string",variable_name);  

Here format_string is %d / %s / %c etc.

Post a Comment