Basics of C language

 C Language Basics


What is Programming?
  • Computer programming is a medium for us to communicate with computers, just like we use Hindi or English to communicate with each other. Programming is a way for us to deliver our instructions to the computer.

What is C?

C is a programming language. C is one of the oldest and finest programming languages. C was developed by Dennis Ritchie in 1972.

Uses of C-
  1. Major parts of Windows, Linux, and other operating systems are written in C.
  2. C is used to write driver programs for devices like Tablets, Printers, etc.
  3. C language is used to program embedded systems where programs need to run faster in limited memory.
  4. C is used to develop games, an area where latency is very important, i.e., a computer has to react quickly to user input.
Variables

A variable is a container that stores a ‘value.’ In the kitchen, we have containers storing rice, dal, sugar, etc. Similar to that variable in c stores the value of a constant. Example:

a = 3 a is assigned “3”
b = 4.7 b is assigned “4.7”
c = 'A' c is assigned “A”

Rules for naming variables in c:

1. The first character must be an alphabet or underscore(_).

2. No commas or blanks are allowed.

3. No special symbol other than underscore is allowed

4. Variable names are case-sensitive.

Constants

An entity whose value doesn’t change is called a constant

Types of constant-

Primarily there are 3 types of constant:

1. Integer Constant -1,6,7,9
2. Real Constant -322.1,2.5,7.0
3. Character Constant ‘a’,’$’,’@’(must be enclosed within single inverted commas)


Keywords-

These are reserved words whose meaning is already known to the compiler. There are 32 keywords available in c:

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


Our 1st C program-

#include<stdio.h>

int main() {

printf(“Hello, there”);

return 0;

}

which is stored in a File called first.c


There are some basic rules which are applicable to all the c programs:

  1. Every program's execution starts from the main function.
  2. All the statements are terminated with a semi-colon.
  3. Instructions are case-sensitive.
  4. Instructions are executed in the same order in which they are written.


Comments-

Comments are used to clarify something about the program in plain language. It is a way for us to add notes to our program. There are two types of comments in c:(it helps us to remember the code when you see it after a long time)

  1. Single line comment: //This is a comment.
  2. Multi-line comment : /*This is multi-line comment*/

Compilation and execution-

A compiler is a computer program that converts a c program into machine language so that it can be easily understood by the computer.

A program is written in plain text. This plain text is a combination of instructions in a particular sequence. The compiler performs some basic checks and finally converts the program into an executable.


printf(“This is %d”,i);

// %d for integers

// %f for real values

// %c for characters


Types of variables-
Integer variables int a=3;
Real variables int a=7.7 (wrong as 7.7 is real) ; float a=7.7;
Character variables char a=’B’;


Receiving input from the user

In order to take input from the user and assign it to a variable, we use scanf function.

The syntax for using scanf:

scanf(%d”,&i); // [This & is important]

& is the “address of” operator, and it means that the supplied value should be copied to the address which is indicated by variable i. 

Operator Precedence in C-
  • In the c language, simple mathematical rules like BODMAS no longer apply.
  • We need to use operator precedence & associativity.

            Operator precedence

                The following table list the operator priority in C                    

Priority Operators
1st   * / %
2nd +   -
3rd =

Operators of higher priority are evaluated first in the absence of parenthesis.


                Operator associativity

            When operators of equal priority are present in an expression, the tie is taken care of by associativity

 x  *  y  /  z =>  (x *  y) / z
x  /  y  *  z  =>  (x / y) * z

*, / follows left to right associativity.


  •    If the operators have the same level as (*, /, %) then we have to do the operations from left to right and the same for (+, - ).

Operator Precedence And Associativity In C - Aticleworld 


Comma Operator-


EX - Sum = (a=2,b=3,c=2,a+b+c);

  • When we have a comma (,)operator then the operation will start from left to right.
  • The value evaluated to sum.
#include 
int main()
 double first, second, temp;
 printf("Enter first number: "); 
 scanf("%lf", &first); 
 printf("Enter second number: "); 
 scanf("%lf", &second); 
 temp = first;    // value of first is assigned to temp     
 first = second; // value of second is assigned to first 
 // value of temp (initial value of first) is assigned to second
 second = temp; // %.2lf displays number up to 2 decimal points 
 printf("\nAfter swapping, first number = %.2lf\n", first); 
 printf("After swapping, second number = %.2lf", second); 
 return 0; 



















Comments

Popular posts from this blog

2 nd year Osmania University study material

1st Year Osmania exams study material

3 rd year notes