Fundamentals of C

Programming languages or concepts
0

What is C Language .

C Language Introduction:-

           'C' is a general purpose language that is notably popular, Simple and Flexible.
It is platform independent,Structured programming language which is widely in various applications.
       Once can say C is a base of all programming languages.if you know C you can easily grasp the concept or knowledge of the other programming languages that uses the concept of C programming.


History of C

History of C programming language

 C Language was developed in 1972 by "Dennis Ritchie" at bell laboratories of American Telephone and Telegraph (AT&T) located in USA.
    Dennis Ritchie is a founder of the C language.
    C Language developed for overcome the problems of previous languages such as B,ACPL,ALGOL,etc.

 


Features of C

eatures of C Programming Language

C is a procedural programming language. C is the widely used language. It was  developed by Dennis Ritchie in the year 1972. It was initially developed as a system programming language to write an operating system. 


Features of C Programming Language: 

C is a Simple.

C is a Procedural Language.

C is a Fast and Efficient.

C is a Rich Library/ Libraries with rich Functions.

C is a Modularity.

C is a Pointers.

C is a Statically Type.

C is a Memory Management.

C is a General-Purpose Language.

C is a Rich set of built-in Operators.

C is a Middle-Level Language.

C is a structured programming language.

C is a Portability.

C is a Easy to Extend.

C is a Recursion.

Simple:-

C is a simple programming language in the sense that it provides a structured approach (to break the problem into parts), the rich set of library functions, data types, etc.

procedural language:-

In a procedural language like a C step by step predefined instructions are carried out. C program may contain more than one functions to perform a particular task. 


Fast and Efficient:-

The compilation and execution time of C language is fast at first there are lesser inbuilt functions and hence the lesser overhead.  programming language as the been middle-level language provides programmers access to direct manipulation with the computer hardware  It’s fast because statically typed languages are faster than dynamically typed languages. 

 Rich Library:-

C provides a lots of inbuilt functions that make the development faster. Robust libraries and functions in C help even a beginner coder to code with ease with C. 


modularity:-

The concept of storing C programming language codes / programs  in the form of libraries for further future uses is known as modularity. C language has its own library to solve common problems like in this we can use a particular function by using a header file stored in its library. 


Pointer:-

C provides the feature of pointers. We can directly interact with the memory by using the pointers Variables. We can use pointers for memory, structures, functions, array, etc.

     pointers point to a specific location in the memory and interact directly with it. Using the C pointers, you can operate with memory, arrays, functions, and structures.

Statically Type:-

    C programming language is a statically typed language. Meaning the type of variable is checked at the time of compilation but not at run time. This means each time a programmer type a program they have to mention the type of variables used. 

 Memory Management:-

It supports the feature of dynamic memory allocation. In C language, we can free the allocated memory at any time by calling the free() function.


First C Program

 First C Program


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

we will learn to create the first C program and then will understand its structure. First of all, let's have a look at how to write a simple and basic Hello World program in C language. Let's get started.

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

#include <stdio.h>    

int main(){    

printf("Hello C Language");    

return 0;   

}  

printf scanf

printf() and scanf() in C

In this tutorial, you will learn to use scanf() function to take input from the user, and printf() function to display output to the user.

The printf() and scanf() functions are used for input and output in C language. Both functions are inbuilt library functions, defined in stdio.h (header file).

printf() function

In C programming, printf() is one of the main output function. The function sends formatted output to the screen.

The printf() function is used for output. It prints the given statement to the console.


The syntax of printf() function is given below:

printf("format string",argument_list);  



scanf() function

In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function reads formatted input from the standard input such as keyboards.

The scanf() function is used to read input data from the console. The scanf() function is builtin function available in the C library. scanf() function can read character, string, numeric & other data from keyboard in C language.

Syntax:-

scanf("format string",argument_list);  


Variables in C

Variables in C:

A variable is a name for a piece of memory in the Random Access Memory (RAM) which can used to store and access the data. The value stored in the allocated memory may change as the program execution progresses so called variable.
 Variable is used to store the value.
A variable in simple terms is a storage place which has some memory allocated to it.  As name indicates it value can be changed or also it can be reused many times.

Syntax:-
            Data type  Variable_name;
 eg. 
         int a;


Types of Variable in c:-

1) Local variable
2) Global variable
3) Static variable
4) External variable
5) Automatic variable


1) Local variable:-

         A variable which is declared inside the function is known as local variable.
       It is used only inside the program. A variable that is declared and used inside the function or block is called local variable. 
It’s scope is limited to function or block. It cannot be used outside the block.Local variables need to be initialized before use. 

2) Global variable:-

              It is declared at the starting of program. 
      A variable is declared outside the program is known as global variable.
 Any function can change the value of the global variable. It is available to all the functions.
    Global variable is used all over the program.

Data Types in c

Data Types in C


in this tutorial we are learn about Data types present in C programming Language.
A data type specifies the type of data that a variable can store such as integer, floating, character, etc.

In C programming, data types are declarations for variables. This determines the type and size of data associated with variables. For example,

int firstVar;

Here, firstVar is a variable of int (integer) type. The size of int is 4 bytes.


The types in C can be classified as follows
Basic Types :

They are arithmetic types and are further classified into: (a) integer types and (b) floating-point types.
Ex: int, char, float, double

Enumerated types :

They are again arithmetic types and they are used to define variables that can only assign certain discrete integer values throughout the program.
Ex: enum.

The type void :

The type specifier void indicates that no value is available.

Ex:  void.

Derived types :

They include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union types and (e) Function types.

Keywords in c

Keywords in C Programming Language:-

  •     Keyword is pre define words in C compiler.
  •      Each keywords is meant to performs a specific function in C programming.
  •     Keywords are not a user defined,we can't used a variable as keyword.
  •  C Language can support a 32 keywords they are listed below.
  • Keywords are predefined, reserved words in C language and each of which is associated with specific features. These words help us to use the functionality of C language.
  • Keywords are words that have special meaning to the C compiler.
  • 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.

Keywords in C Programming

auto

double

int

break

else

long

case

enum

register

char

extern

return

const

float

short

continue

for

signed

default

goto

sizeof

do

if

Static

struct

switch

typedef

union

unsigned

void

volatile

while

 


C Identifiers

What are Identifiers?

Identifiers are words or texts used to identify anything in the C language.

An identifier can be combination of letters such as uppercase, lowercase letters, underscore, digits, but the starting letter should be either an alphabet or an underscore.

Before starting any language, you must at least know how you name an identifier.

Just like you have a name, using which everyone calls you, it can be John, Ron, Scarlett, Monica, etc., similarly in the C language, when we define a variable or a function, or a structure, etc.


Rules for naming identifiers

  • The rules that must be followed while naming the identifiers are as follows −
  • 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.
  • An identifier can only have alphanumeric characters (a-z , A-Z , 0-9) (i.e. letters & digits) and underscore( _ ) symbol.
  • It should not begin with any numerical digit.
  • Identifier names must be unique
  • In identifiers, both uppercase and lowercase letters are distinct. Therefore, we can say that identifiers are case sensitive.
  •  i.e.Identifiers are case-sensitive in the C language. For example, name and Name will be treated as two different identifiers.
  • You cannot use a keyword as identifiers.

C Operators

Operators in C programming language:-

   We will learn about different operations in C programming with the help of example, C language support a wealthy set of built-in operators. A operator is a symbol that tells the compiler to perform a certain Logical and mathematical operations/ manipulation.


Operators:-

     An operator is a symbol that operates on a value or a variable.
    Operators is used to perform specific mathematical and logical computation on operands.
  Ex. Consider the below statement:
                 C= A+B;
 Here, + is the addition operator and A & B are operands. The addition operator tells  the compiler to add both of the operands A & B.
 And result stored in C operand.

C Format Specifier


    The format specifiers are used in C for input and output purposes. Using this concept the compiler can understand that what type of data is in a variable during taking input using the scanf() function and printing using printf() function. Here is a list of format specifiers.
    The Format specifier is a string used in the formatted input and output functions. The format string determines the format of the input and output. The format string always starts with a '%' character.

The commonly used format specifiers in printf() function are:

 

Format specifier

 

Description

%u

                It is used to print the unsigned integer value where the unsigned integer means that the variable can hold only positive value.

%d or %i

It is used to print the signed integer value where signed integer means that the variable can hold both positive and negative values.

%x

                a hexadecimal (base 16) integer

%o

It is used to print the octal unsigned integer where octal integer value always starts with a 0 value.

%X

It is used to print the hexadecimal unsigned integer, but %X prints the alphabetical characters in uppercase such as A, B, C, etc.

%hi

short (signed)

%hu

short (unsigned)

%Lf

long double

%n

prints nothing

%i

a decimal integer (detects the base automatically)

%p

an address (or pointer)

%e

a floating point number in scientific notation

C Escape Sequence


An escape sequence in C language is a sequence of characters that doesn't represent itself when used inside string literal or character.

It is composed of two or more characters starting with backslash \. For example: \n represents new line.An escape sequence is a sequence of characters that does not represent itself when used inside a character or string literal, but is translated into another character or a sequence of characters that may be difficult or impossible to represent directly.


List of Escape Sequences in C


Escape Sequence

Meaning

\a

Alarm or Beep

\b

Backspace

\f

Form Feed

\xhh

hexadecimal number

\nnn

octal number

\?

Question Mark

\"

Double Quote

\'

Single Quote

\n

New Line

\r

Carriage Return

\v

Vertical Tab

\t

Tab (Horizontal)  

ASCII value in C

ASCII is abbreviated as the “American Standard Code for Information Interchange”. As we are humans we have our language to understand the same way machine also have the same thing to understand characters, digits, special characters that is ASCII representation of the character. It is a character encoding schema that is used for electronic communication.ASCII contains numbers, each character has its own number to represent. We have 256 character to represent in C (0 to 255) like character (a-z, A-Z), digits (0-9) and special character like !, @, # etc. This each ASCII code occupied with 7 bits in the memory. Let suppose ASCII value of character ‘C’ is 67. When we give input as ‘B’ machine treat it as 67 internally and stores its address. When we get back our original number compiler gives you 67 and other internal software converts these values into its equivalent characters.


What is ASCII code?

    The full form of ASCII is the American Standard Code for information interchange. It is a character encoding scheme used for electronics communication. Each character or a special character is represented by some ASCII code, and each ascii code occupies 7 bits in memory.


In C programming language, a character variable does not contain a character value itself rather the ascii value of the character variable. The ascii value represents the character variable in numbers, and each character variable is assigned with some number range from 0 to 127. For example, the ascii value of 'A' is 65.


In the above example, we assign 'A' to the character variable whose ascii value is 65, so 65 will be stored in the character variable rather than 'A'.

Constants in C

Constants in C

A constant is a value or variable that can't be changed in the program, for example: 10, 20, 'a', 3.4, "c programming" etc.

Constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals.

Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are enumeration constants as well.

Constants are treated just like regular variables except that their values cannot be modified after their definition.


There are two types of constants

Primary constants :− Integer, float, and character are called as Primary constants.

Secondary constants :− Array, structures, pointers, Enum, etc., called as secondary constants.

Syntax

const datatype variable;


Defining Constants: 

In C/C++ program we can define constants in two ways as shown below: 

 

  1. Using #define preprocessor directive
  2. Using a const keyword

Tokens in C

Tokens and Keywords in C

 In C tokens, identifiers and keywords are the basics in a C language. All are listed in one by one as followed.
C-Tokens:-
   C Tokens are the basics building blocks in c Language which are constructed together for build a program.
  In C each and every smallest individual part of C Programming in known as C - Tokens.

Thery are six types of C - Tokens in C

1) Keywords.              (eg.while,int,etc)
2) identifiers.              (eg.main, total,etc)
3) Contents.                 (eg.10,99)
4) String.                      (eg."Hello","Saurabh",etc)
5) Special symbols.    (eg.(),@,etc)
6) Operators                (eg.+,*,/,-,etc)


Keywords in C Programming Language:-

  •     Keyword is pre define words in C compiler.
  •      Each keywords is meant to performs a specific function in C programming.
  •     Keywords are not a user defined,we can't used a variable as keyword.
  •  C Language can support a 32 keywords they are listed below.
  • Keywords are predefined, reserved words in C language and each of which is associated with specific features. These words help us to use the functionality of C language.
  • Keywords are words that have special meaning to the C compiler.
  • 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.

Conditional Operator in C

Conditional operators:

 Conditional operators return one value if condition is true and returns another value is condition is false.

The conditional operator is also known as a ternary operator. The conditional statements are the decision-making statements which depends upon the output of the expression. It is represented by two symbols, i.e., '?' and ':'.

As conditional operator works on three operands, so it is also known as the ternary operator.

What is a Conditional Operator in C ?

The behavior of the conditional operator is similar to the 'if-else' statement as 'if-else' statement is also a decision-making statement.

The operands may be an expression, constants, or variables. It starts with a condition, hence it is called a conditional operator. Conditional operators return one value if the condition is true and returns another value if the condition is false.

This operator is also called as ternary operator.

Syntax     :        (Condition? true_value: false_value);

OR

The conditional operator is of the form  

variable = Expression1 ? Expression2 : Expression3

Example :         (x > 20  ?  0  :  1);

Bitwise Operator in C

The bitwise operators are the operators used to perform the operations on the data at the bit-level. When we perform the bitwise operations, then it is also known as bit-level programming. It consists of two digits, either 0 or 1. It is mainly used in numerical computations to make the calculations faster.


What are Bitwise Operators?

    Bitwise Operators are used for manipulating data at the bit level, also called bit level programming. Bitwise operates on one or more bit patterns or binary numerals at the level of their individual bits. They are used in numerical computations to make the calculation process faster.

The bitwise logical operators work on the data bit by bit, starting from the least significant bit

Always remember one thing that bitwise operators are mostly used with the integer data type because of its compatibility.

Following is the list of bitwise operators provided by ‘C’ programming language:


Operator

Meaning of operator

&

Bitwise AND operator

|

Bitwise OR operator

^

Bitwise exclusive OR operator

~

One's complement operator (unary operator)

<< 

Left shift operator

>> 

Right shift operator


Post a Comment

0Comments

Post a Comment (0)
close