C Language Tutorial |
|||
Produced by Ashish Goel
| |||
Saturday, January 31, 2015
C Operators
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 operators direction to be evaluated, it may be left to right or right to left.
Let's understand the precedence by the example given below:
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 |
Keywords in C
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 C language.
A list of 32 keywords in 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.
Data Types in C
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 4 types of 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 basic data types may change according to 32 or 64 bit operating system.
Let's see the basic data types. It size is given according to 32 bit OS.
Data Types | Memory Size | Range |
---|---|---|
char | 1 byte | −127 to 127 |
signed char | 1 byte | −127 to 127 |
unsigned char | 1 byte | 0 to 127 |
short | 2 byte | −32,767 to 32,767 |
signed short | 2 byte | −32,767 to 32,767 |
unsigned short | 2 byte | 0 to 32,767 |
int | 2 byte | −32,767 to 32,767 |
signed int | 2 byte | −32,767 to 32,767 |
unsigned int | 2 byte | 0 to 32,767 |
short int | 2 byte | −32,767 to 32,767 |
signed short int | 2 byte | −32,767 to 32,767 |
unsigned short int | 2 byte | 0 to 32,767 |
long int | 4 byte | |
signed long int | 4 byte | |
unsigned long int | 4 byte | |
float | 4 byte | |
double | 8 byte | |
long double | 10 byte |
Variables in C
Variables in C
A variable is a name of 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:
type variable_list;
The example of declaring variable is given below:
int a;
float b;
char c;
Here, a, b, c are variables and int,float,char are data types.
We can also provide values while declaring the variables as given below:
int a=10,b=20;//declaring 2 variable of integer type
float f=20.8;
char c='A';
There are many types of variables in c:
- local variable
- global variable
- static variable
- automatic variable
- external variable
Local Variable
A variable that is declared inside the function or block is called 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 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.
int value=20;//global variable
void function1(){
int x=10;//local variable
}
Static Variable
A variable that is declared with 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, local variable will print the same value for each function call e.g, 11,11,11 and so on. But 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 is declared inside the block, are automatic variables by default. By we can explicitly declare 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 external variable. To declare a external variable, you need to useextern 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);
}
Printf (Output Statement) OR Scanf (Input Statement ) in C
Printf (Output Statement) OR Scanf (Input Statement ) in C
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
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);
The format string can be %d (integer), %c (character), %s (string), %f (float) etc.
scanf() function
The scanf() function is used for input. It reads the input data from the console.
scanf("format string",argument_list);
Program to print cube of given number
Let's see a simple example of c language that gets input from the user and prints the cube of the given number.
#include<stdio.h>
#include<conio.h>
void main(){
int number;
clrscr();
printf("enter a number:");
scanf("%d",&number);
printf("cube of number is:%d ",number*number*number);
getch();
}
Output
enter a number:5cube of number is:125
The scanf("%d",&number) statement reads integer number from the console and stores the given value in number variable.The printf("cube of number is:%d ",number*number*number) statement prints the cube of number on the console.
Program to print sum of 2 numbers
Let's see a simple example of input and output in C language that prints addition of 2 numbers.#include<stdio.h>#include<conio.h>void main(){int x=0,y=0,result=0;clrscr();printf("enter first number:");scanf("%d",&x);printf("enter second number:");scanf("%d",&y);result=x+y;printf("sum of 2 numbers:%d ",result);getch();}
Output
enter first number:9enter second number:9sum of 2 numbers:18
First C Program
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:
#include <stdio.h>
#include <conio.h>
void main(){
printf("Hello C Language");
getch();
}
#include <stdio.h> includes the standard input output library functions. The printf() function is defined in stdio.h .
#include <conio.h> includes the console input output library functions. The getch() function is defined in conio.h file.
void main() The main() function is the entry point of every program in c language. The void keyword specifies that it returns no value.
printf() The printf() function is used to print data on the console.
getch() The getch() function asks for a single character. Until you press any key, it blocks the screen.
How to compile and run the c program
There are 2 ways to compile and run the c program, by menu and by shortcut.
By menu
Now click on the compile menu then compile sub menu to compile the c program.
Then click on the run menu then run sub menu to run the c program.
By shortcut
Or, press ctrl+f9 keys compile and run the program directly.
You will see the following output on user screen.
You can view the user screen any time by pressing the alt+f5 keys.
Now press Esc to return to the turbo c++ console.
clear screen by clrscr() function
If you run the c program many times, it will append the output in previous output. But, you can call clrscr() function to clear the screen. So it will be better for you to call clrscr() function after the main method as given below:
#include <stdio.h>
#include <conio.h>
void main(){
clrscr();
printf("Hello C Language");
getch();
}
Subscribe to:
Posts (Atom)