C - Basics

1. Command-line:
  • It describes what's going on inside a program, so that person looking at the source code does not have a hard time figuring it out.
Types of comments:
(i)Single line comments:
Syntax:
        /*Statements*/
Example:
        /* This is the addition program*/
 (ii)Multiline comments:
  Syntax:
       //Statement
2.Variables:
  • A variable is nothing but it allocates the memory space and holds the values.
Syntax:
Data_type variable_name;
Example:
int x, y;
Rules for naming the variables:
  • The first character must be an alphabet or an underscore(_).
  • No commas or blank spaces are allowed within a variable name.
  • No special symbols, an underscore can be used in a variable name.
User defined variables:
a). Type declaration:
  • It is used to allow the user to define an identifier that would represent an existing data type.
Syntax:
typedef data_type identifier;
Example:
typedef int marks;
marks m1,m2,m3;
b). Enumerated data type:
  • It is used to create user-defined data types i.e. the programmer can create their own data type and define what values the variables of those data types can hold.
Syntax:
enum  enum _name{value 1,value 2, ......,value n};
Example:
enum day{mon,tue,wed,...sun};
Scope of variables:
  • It implies the availability of variables within the program.
  • It is divided into two types.
1). Local variable:
  • The variables which are defined inside the function block called a local variable.
Example:
void main()
{
int a,b;//local variable
......
}
2). Global variable:
  • The variables that are declared before the main() function is called a global variable.
Example:
int a,b;//Global variable
void main()
{
......

}
3.Data types: 
  • It means kinds of data that means numbers or words.
  • The most widely used data types are
1. int (integer) - real numbers
Ex: 1, 2, -5, 1311
2. float - decimal number:
Ex: 1.45, 0.001
3. char(character)- Alphabets
Ex: k,f,v,h
4.double – combinations of int and float numbers
Ex: 1.001,89,1654
4. Input function: 
  • This function is used to value get from users.
Syntax:
scanf(“ format specifiers ”,variable_name);
Example:
scanf("%d",z);
5.Format specifiers:
  •  It is used to indicate unknown value for types to display on the output screen and also get value from user. ( i.e. Holds unknown values)
There are,
6.Output function: 
  • This function is used to show details for users.
Syntax:
printf(“Statements”);
Example:
printf("Welcome to whereisstuff");
7.Escape sequence: 
  • This is used for aligning output on the screen.

Escape sequence
Description
Example
Output
\n
Newline or next line
printf(“Hai! \n I am Sabrina.”)
Hai! I am Sabrina.
\t
Horizontal tab(one tab space)
printf(“Hai\t Hello”)
Hai                   hello
\’
Display single-quotes
printf(“I don\”t know”)
I don’t know
\”
Display double-quotes
printf(“/“Hey/”’)
“Hey”
\\
Print slash
printf(“a//b”)
a/b


















Example program:

/* To print "Welcome to whereisstuff " and multiply two numbers  */
#include<stdio.h>
#include<conio.h>//header files
void main()//main function
{
int x,y,z;//variable-declaration
clrscr();
printf("Welcome to whereisstuff\t\n Multiply two numbers\n");//print fun
printf("Enter x and y value:\n");
scanf("%d%d",&x,&y);//get value from user and store
z=x*y;
printf("Multiply value is: %d",z);//result disply
getch();
}

Output: 

Share:

C – Tokens

C – Tokens:
C – Tokens are small and each of every individual unit or parts in a program called tokens.
It has six kinds of tokens. There are
  • Keywords (eg: int, while),
  • Identifiers (eg: a, b),
  • Constants (eg: 5, 10),
  • Strings (eg: “Hello”, “Welcome”),
  • Special symbols (eg: (), {}),
  • Operators (eg: +, /,-,*)
1. Keywords:
  • It is pre-defined words in C. (i.e., Fixed words for creating programs in C)
  • List of keywords in C language: 
auto
double
int
struct
break
else
long
switch
case
enum
register
typedef
char
extern
return
union
const
short
float
unsigned
continue
for
signed
Void
default
goto
sizeof
Volatile
do
if
static
While
2.Identifiers:
  • Identifiers are user-defined words in c program such as variable name, function name, array name, and etc.
3.Constants:
  • It is a fixed value in C.
  • But it can be changed during it’s before execution.
  • Users can also create constant throw syntax.
Syntax:
Const data_type variable_name = value;
For example:
The pi(π) value is standard value i.e. 22/7 = 3.14.
4.String:

  • Strings are collections of characters or alphabets i.e. words
  • It is used to display for users.
5.Special symbols:
It has a special built-in meaning in the C language. 


Expression
Symbols
Asterisk
*
Assignment
=
Ampersand
&
Brackets
[]
Braces
{}
Comma
,
Double-quotes
Front lash
/
Parentheses
()
Pre-processor
#
Semi-colon
;

1.   6.Operators:
Operators are symbols and used to perform some mathematics operations.
Six kinds of operators,
1.      Arithmetic operators(+,-,*,/,%,++,--)
2.     Relational Operators (==,!=,<=,>=,<,>)
3.     Logical Operators (&&,||,!)
4.     Assignment Operators(=,+=,-=,*=,/=,%=)
5.     Conditional Operators (?:)
6.    Bitwise Operators(&,|,^,<<,>>)
Example:

#include<stdio.h>
#include<conio.h>
void main()
{
    const int a=5;
    const int b=10;
    int c;
    clrscr();
    c=a+b;
    printf("Addition of a and b value is: %d",c);
    getch();
}

Output:

Explanations:
  • Keywords - main,int,printf,clrscr,getch
  • Identifiers - a,b,c
  • Constants - a=5,b=10
  • String- printf(“Addition of a and b value is: %d”,c);
  • Special symbols-(),{,},;,”
  • Operators -=,+


Share:

C - Language Introduction

Introduction:
  1. C – Language proposed by Dennis Richie in 1969 at AT&T Bell Labs.
  2. It is an upgraded version of two earlier languages, called BCPL and B, which were also developed by Bell laboratories. 
  3. It is called a structured oriented programming language.
  4. C is the basic language in all programming languages.
  5. It is used to create system applications like games, drivers, and so on.
Why C programming language is required?
  • Before using c program, the program was written by assembly language i.e. Middle-level language.
  • Assembly languages were fully instruction based language so if any error comes, we cannot be found easily.
  • Because If one word has spelled a mistake in the program then it returns full program lines have error so we cannot find the exact error(i.e. error number is not displaying).
  • In C program using function concept for overcoming the above problem.
  • That means, it is used to find an exact error in the program and which block has an error.
  • Because the function is used to divide the program into smaller subprograms.
        For example,
  • The main() function have add(),sub() and mul() functions and one error in sub() block means C program easily identified and it checks only that blocks.
  • So using the C program save the compile-time and also we can be reusable the function code, so it reduces the number of lines.
  • This is the reasons for calling the C program as Function oriented programming language or Procedure oriented programming language.
Note:
The basic structure of C:
Syntax:

 //Command line
<Header files>
 Main function()
 {
      Variable declaration
      I/O operation
 }

Example:

 #include<stdio.h>
 #include <conio.h>
 void main() 
 { 
     int a,b,c;
     clrscr();
     printf("Enter a and b value : ");
     scanf("%d %d",&a,&b);
     c=a+b; 
     printf("\nAddition of a and b value is: %d",c); 
     getch(); 
}

Output:
Program explanation:
  • # - it’s called pre-processor and used to replace the values in variables. 
  • include - it's used to add header files and the user programs.
  • stdio.h - standard input and output header file(it holds printf and scanf of library files).
  • conio.h - stands for console input and output header file(used in old MS-DOS compilers to create text user interfaces and it holds clrscr() and getch() libraries).
  • void main() - main part
  • int – data type
  • a,b,c – variables
  • clrscr() – refresh output screen
  • printf – used to display the user entered details
  • scanf – used to get the value from the user and put into the memory location
  • %d - format specifiers
  • c=a+b – addition process and stored result in c variable
  • getch() – used to hold the output screen and wait until the user gives any type of input(i.e until the user press any key)
Output process in the backend:
Output process explanation in the backend:
Step 1: Write source code that is your program (refer above example program)
Step 2: Pre-processor
For example, a and b value is 5 and 4 then pre-processor replace the values
scanf(“%d %d”,5,4);
Step 3: Complier (convert the English language to machine level language)
To check the error and convert machine-level code (i.e. 0’s and 1’s)
Step 4: Assembler
Used to convert machine level language to human-readable language. (0’s and 1’s to normal text)
Step 5: loader and linker
Adding header files and pre-processor program
Step 6: Final output

Share:

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me