Skip to main content

Basic C Language Notes

 First C Program

Before starting the abcd of C language, you need to learn how to write, compile and run the first c program.

To write the first c program, open the C console and write the following code:

1.     #include <stdio.h>    

2.     int main()

{    {

3.     printf("Hello C Language");    

4.     return 0;   

5.     }  

#include <stdio.h> includes the standard input output library functions. The printf() function is defined in stdio.h .

int main() The main() function is the entry point of every program in c language.


Compilation process in c

What is a compilation?

The compilation is a process of converting the source code into object code. It is done with the help of the compiler. The compiler checks the source code for the syntactical or structural errors, and if the source code is error-free, then it generates the object code.


The c compilation process converts the source code taken as input into the object code or machine code. The compilation process can be divided into four steps, i.e., Pre-processing, Compiling, Assembling, and Linking.

The preprocessor takes the source code as an input, and it removes all the comments from the source code. The preprocessor takes the preprocessor directive and interprets it. For example, if <stdio.h>, the directive is available in the program, then the preprocessor interprets the directive and replace this directive with the content of the 'stdio.h' file.

The following are the phases through which our program passes before being transformed into an executable form:

  • Preprocessor
  • Compiler
  • Assembler
  • Linker

Preprocessor

The source code is the code which is written in a text editor and the source code file is given an extension ".c". This source code is first passed to the preprocessor, and then the preprocessor expands this code. After expanding the code, the expanded code is passed to the compiler.

Compiler

The code which is expanded by the preprocessor is passed to the compiler. The compiler converts this code into assembly code. Or we can say that the C compiler converts the pre-processed code into assembly code.

Assembler

The assembly code is converted into object code by using an assembler. The name of the object file generated by the assembler is the same as the source file. The extension of the object file in DOS is '.obj,' and in UNIX, the extension is 'o'. If the name of the source file is 'hello.c', then the name of the object file would be 'hello.obj'.

Linker

Mainly, all the programs written in C use library functions. These library functions are pre-compiled, and the object code of these library files is stored with '.lib' (or '.a') extension. The main working of the linker is to combine the object code of library files with the object code of our program. Sometimes the situation arises when our program refers to the functions defined in other files; then linker plays a very important role in this. It links the object code of these files to our program. Therefore, we conclude that the job of the linker is to link the object code of our program with the object code of the library files and other files. The output of the linker is the executable file. The name of the executable file is the same as the source file but differs only in their extensions. In DOS, the extension of the executable file is '.exe', and in UNIX, the executable file can be named as 'a.out'. For example, if we are using printf() function in a program, then the linker adds its associated code in an output file.

Variables in C

variable is a name of the memory location. It is used to store data. Its value can be changed, and it can be reused many times.

It is a way to represent memory location through symbol so that it can be easily identified.

Let's see the syntax to declare a variable:

 

1.      type variable_list;  

The example of declaring the variable is given below:

1.      int a;  

2.      float b;  

3.      char c;  

Here, a, b, c are variables. The int, float, char are the data types.

We can also provide values while declaring the variables as given below:

 

1.      int a=10,b=20;//declaring 2 variable of integer type  

2.      float f=20.8;  

3.      char c='A';  

Rules for defining variables

  • A variable can have alphabets, digits, and underscore.
  • A variable name can start with the alphabet, and underscore only. It can't start with a digit.
  • No whitespace is allowed within the variable name.
  • A variable name must not be any reserved word or keyword, e.g. int, float, etc.

Valid variable names:

 

1.      int a;  

2.      int _ab;  

3.      int a30;  

Invalid variable names:

 

1.      int 2;  

2.      int a b;  

3.      int long;  

Types of Variables in C

There are many types of variables in c:

  1. local variable
  2. global variable
  3. static variable
  4. automatic variable
  5. external variable

Local Variable

A variable that is declared inside the function or block is called a local variable.

It must be declared at the start of the block.

void function1(){  

int x=10;//local variable  

}  

You must have to initialize the local variable before it is used.

Global Variable

A variable that is declared outside the function or block is called a global variable. Any function can change the value of the global variable. It is available to all the functions.

It must be declared at the start of the block.

 

1.      int value=20;//global variable  

2.      void function1(){  

3.      int x=10;//local variable  

4.      }  

Static Variable

A variable that is declared with the static keyword is called static variable.

It retains its value between multiple function calls.

 

void function1(){  

int x=10;//local variable  

static int y=10;//static variable  

x=x+1;  

y=y+1;  

printf("%d,%d",x,y);  

}  

If you call this function many times, the local variable will print the same value for each function call, e.g, 11,11,11 and so on. But the static variable will print the incremented value in each function call, e.g. 11, 12, 13 and so on.

Automatic Variable

All variables in C that are declared inside the block, are automatic variables by default. We can explicitly declare an automatic variable using auto keyword.

 

void main(){  

int x=10;//local variable (also automatic)  

auto int y=20;//automatic variable  

}  

External Variable

We can share a variable in multiple C source files by using an external variable. To declare an external variable, you need to use extern keyword.

myfile.h

extern int x=10;//external variable (also global)  

program1.c

 

#include "myfile.h"  

#include <stdio.h>  

void printValue(){  

    printf("Global variable: %d", global_variable);  

}  

Data Types in C

A data type specifies the type of data that a variable can store such as integer, floating, character, etc.

There are the following data types in C language.

Types

Data Types

Basic Data Type

int, char, float, double

Derived Data Type

array, pointer, structure, union

Enumeration Data Type

enum

Void Data Type

void

Basic Data Types

The basic data types are integer-based and floating-point based. C language supports both signed and unsigned literals.

The memory size of the basic data types may change according to 32 or 64-bit operating system.

Let's see the basic data types. Its size is given according to 32-bit architecture.

Data Types

Memory Size

Range

char

1 byte

−128 to 127

signed char

1 byte

−128 to 127

unsigned char

1 byte

0 to 255

short

2 byte

−32,768 to 32,767

signed short

2 byte

−32,768 to 32,767

unsigned short

2 byte

0 to 65,535

int

2 byte

−32,768 to 32,767

signed int

2 byte

−32,768 to 32,767

unsigned int

2 byte

0 to 65,535

short int

2 byte

−32,768 to 32,767

signed short int

2 byte

−32,768 to 32,767

unsigned short int

2 byte

0 to 65,535

long int

4 byte

-2,147,483,648 to 2,147,483,647

signed long int

4 byte

-2,147,483,648 to 2,147,483,647

unsigned long int

4 byte

0 to 4,294,967,295

float

4 byte

double

8 byte

long double

10 byte

Keywords in C

A keyword is a reserved word. You cannot use it as a variable name, constant name, etc. There are only 32 reserved words (keywords) in the C language.

A list of 32 keywords in the c language is given below:

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

volatile

while

We will learn about all the C language keywords later.

C Identifiers

C identifiers represent the name in the C program, for example, variables, functions, arrays, structures, unions, labels, etc. An identifier can be composed of letters such as uppercase, lowercase letters, underscore, digits, but the starting letter should be either an alphabet or an underscore. If the identifier is not used in the external linkage, then it is called as an internal identifier. If the identifier is used in the external linkage, then it is called as an external identifier.

We can say that an identifier is a collection of alphanumeric characters that begins either with an alphabetical character or an underscore, which are used to represent various programming elements such as variables, functions, arrays, structures, unions, labels, etc. There are 52 alphabetical characters (uppercase and lowercase), underscore character, and ten numerical digits (0-9) that represent the identifiers. There is a total of 63 alphanumerical characters that represent the identifiers.

Rules for constructing C identifiers

  • The first character of an identifier should be either an alphabet or an underscore, and then it can be followed by any of the character, digit, or underscore.
  • It should not begin with any numerical digit.
  • In identifiers, both uppercase and lowercase letters are distinct. Therefore, we can say that identifiers are case sensitive.
  • Commas or blank spaces cannot be specified within an identifier.
  • Keywords cannot be represented as an identifier.
  • The length of the identifiers should not be more than 31 characters.
  • Identifiers should be written in such a way that it is meaningful, short, and easy to read.

Example of valid identifiers

1.     total, sum, average, _m _, sum_1, etc.  

Example of invalid identifiers

1.      2sum (starts with a numerical digit)  

2.      int (reserved word)  

3.      char (reserved word)  

4.      m+n (special character, i.e., '+')  

Types of identifiers

  • Internal identifier
  • External identifier

Internal Identifier

If the identifier is not used in the external linkage, then it is known as an internal identifier. The internal identifiers can be local variables.

External Identifier

If the identifier is used in the external linkage, then it is known as an external identifier. The external identifiers can be function names, global variables.

Differences between Keyword and Identifier

Keyword

Identifier

Keyword is a pre-defined word.

The identifier is a user-defined word

It must be written in a lowercase letter.

It can be written in both lowercase and uppercase letters.

Its meaning is pre-defined in the c compiler.

Its meaning is not defined in the c compiler.

It is a combination of alphabetical characters.

It is a combination of alphanumeric characters.

It does not contain the underscore character.

It can contain the underscore character.

Let's understand through an example.

1.     int main()  

2.      {  

3.          int a=10;  

4.          int A=20;  

5.          printf("Value of a is : %d",a);  

6.          printf("\nValue of A is :%d",A);  

7.          return 0;  

8.      }  

Output

Value of a is : 10
Value of A is :20  

The above output shows that the values of both the variables, 'a' and 'A' are different. Therefore, we conclude that the identifiers are case sensitive.

C Operators

An operator is simply a symbol that is used to perform operations. There can be many types of operations like arithmetic, logical, bitwise, etc.

There are following types of operators to perform different types of operations in C language.

  • Arithmetic Operators
  • Relational Operators
  • Shift Operators
  • Logical Operators
  • Bitwise Operators
  • Ternary or Conditional Operators
  • Assignment Operator
  • Misc Operator

Precedence of Operators in C

The precedence of operator species that which operator will be evaluated first and next. The associativity specifies the operator direction to be evaluated; it may be left to right or right to left.

Let's understand the precedence by the example given below:

1.     int value=10+20*10;  

The value variable will contain 210 because * (multiplicative operator) is evaluated before + (additive operator).

The precedence and associativity of C operators is given below:

Category

Operator

Associativity

Postfix

() [] -> . ++ - -

Left to right

Unary

+ - ! ~ ++ - - (type)* & sizeof

Right to left

Multiplicative

* / %

Left to right

Additive

+ -

Left to right

Shift

<< >>

Left to right

Relational

< <= > >=

Left to right

Equality

== !=

Left to right

Bitwise AND

&

Left to right

Bitwise XOR

^

Left to right

Bitwise OR

|

Left to right

Logical AND

&&

Left to right

Logical OR

||

Left to right

Conditional

?:

Right to left

Assignment

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

Right to left

Comma

,

Left to right

Comments in C

Comments in C language are used to provide information about lines of code. It is widely used for documenting code. There are 2 types of comments in the C language.

  1. Single Line Comments
  2. Multi-Line Comments

Single Line Comments

Single line comments are represented by double slash \\. Let's see an example of a single line comment in C.

#include<stdio.h>    

int main(){    

    //printing information    

    printf("Hello C");    

return 0;  

}      

Output:

Hello C

Even you can place the comment after the statement. For example:

printf("Hello C");//printing information  

 

Mult Line Comments

Multi-Line comments are represented by slash asterisk \* ... *\. It can occupy many lines of code, but it can't be nested. Syntax:

/*  

code 

to be commented 

*/  

Let's see an example of a multi-Line comment in C.

#include<stdio.h>    

int main(){    

    /*printing information   

      Multi-Line Comment*/  

    printf("Hello C");    

return 0;  

}       

Output:

Hello C

Comments