SlideShare a Scribd company logo
4
Most read
6
Most read
14
Most read
Introduction
The C is Procedure-oriented programming language that is developed for creating
system applications that directly interact with the hardware devices such as drivers,
kernels, etc.
C programming is considered as the base for other programming languages, that is why
it is known as mother language.
It provides the core concepts like the array, strings, functions, file handling, etc. that are
being used in many languages like C++, Java, C#, etc.
C language is considered as the mother language of all the modern programming
languages because most of the compilers, JVMs, Kernels, etc. are written in C
language.
A procedure is known as a function, method, routine, subroutine, etc. A procedural
language specifies a series of steps for the program to solve the problem.
A procedural language breaks the program into functions, data structures, etc.
C is a procedural language. In C, variables and function prototypes must be declared
before being used.
C is considered as a middle-level language because it supports the feature of both
low-level and high-level languages. C language program is converted into assembly
code, it supports pointer arithmetic (low-level), but it is machine independent (a feature
of high-level).
A Low-level language is specific to one machine, i.e., machine dependent. It is
machine dependent, fast to run. But it is not easy to understand.
A High-Level language is not specific to one machine, i.e., machine independent. It is
easy to understand.
C Program
In this tutorial, all C programs are given with C compiler so that you can quickly change
the C program code.
File Name : main.c
#include <stdio.h>
1. int main(){
2. printf("Hello C Language");
3. return 0;
4. }
C programming language was developed in 1972 by Dennis Ritchie at bell laboratories
of AT&T (American Telephone & Telegraph), located in the U.S.A.
Dennis Ritchie is known as the founder of the c language.
It was developed to overcome the problems of previous languages such as B, BCPL, etc.
Initially, C language was developed to be used in UNIX operating system. It inherits
many features of previous languages such as B and BCPL.
Let's see the programming languages that were developed before C language.
Language Year Developed By
Algol 1960 International Group
BCPL 1967 Martin Richard
B 1970 Ken Thompson
Traditional C 1972 Dennis Ritchie
K & R C 1978 Kernighan & Dennis Ritchie
ANSI C 1989 ANSI Committee
ANSI/ISO C 1990 ISO Committee
C99 1999 Standardization Committee
Features of C Language
C is the widely used language. It provides many features that are given below.
1. Simple
2. Machine Independent or Portable
3. Mid-level programming language
4. Rich Library
5. Fast Speed
6. Recursion
Simple
C is a simple 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.
Machine Independent or Portable
Unlike assembly language, c programs can be executed on different machines with
some machine specific changes.
Mid-level programming language
Although,C is intended to do low-level programming. It is used to develop system
applications such as kernel, driver, etc. It also supports the features of a high-level
language. That is why it is known as mid-level language.
Rich Library
C provides a lot of inbuilt functions that make the development fast.
Speed
The compilation and execution time of C language is fast since there are lesser inbuilt
functions and hence the lesser overhead.
Recursion
In C, we can call the function within the function. It provides code reusability for
every function. Recursion enables us to use the approach of backtracking.
Pointer
C provides the feature of pointers. We can directly interact with the memory by using
the pointers. We can use pointers for memory, structures, functions, array, etc.
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; }
#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.
printf() The printf() function is used to print data on the console.
return 0 The return 0 statement, returns execution status to the OS. The 0 value is
used for successful execution and 1 for unsuccessful execution.
How to compile and run the c program
There are 2 ways to compile and run the c program, by menu and by shortcut.
Flow of C Program
The C program follows many steps in execution. To understand the flow of C program
well, let us see a simple program first.
File Name : simple.c
#include <stdio.h>
5. int main(){
6. printf("Hello C Language");
7. return 0;
8. }
Flow of Execution
Let's try to understand the flow of above program by the figure given below.
1)
C program (source code) is sent to preprocessor first. The
preprocessor is responsible to convert preprocessor
directives into their respective values. The preprocessor
generates an expanded source code.
2)
Expanded source code is sent to compiler which compiles
the code and converts it into assembly code.
3)
The assembly code is sent to assembler which assembles
the code and converts it into object code. Now a simple.obj
file is generated.
4)
The object code is sent to linker which links it to the library
such as header files. Then it is converted into executable
code. A simple.exe file is generated.
5)
The executable code is sent to loader which loads it into
memory and then it is executed. After execution, output is
sent to console.
printf() and scanf() 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);
Variables in C
A 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:
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';
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
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.
1. #include<stdio.h>
2. int main(){
3. int number;
4. printf("enter a number:");
5. scanf("%d",&number);
6. printf("cube of number is:%d ",number*number*number);
7. return 0;
8. }
Output
enter a number:5
cube 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>
1. int main(){
2. int x=0,y=0,result=0;
3.
4. printf("enter first number:");
5. scanf("%d",&x);
6. printf("enter second number:");
7. scanf("%d",&y);
8.
result=x+y;
printf("sum of 2 numbers:%d ",result);
return 0;
}
Output
enter first number:9
enter second number:9
sum of 2 numbers:18
Rules for defining variables
o A variable name must be start with the alphabet, and underscore only. It can't
start with a digit.
o No whitespace is allowed within the variable name.
o A variable name must not be any reserved word or keyword, e.g. int, float, etc.
Valid variable names Invalid variable names
1.
2. int a;
3. int _ab;
4. int a30;
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.
1. void function1(){
2. int x=10;//local variable
3. }
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.
1. int value=20;//global variable
2. void function1(){
3. int x=10;//local variable
4. }
It must be declared at the start of the block.
Static Variable
A variable that is declared with the static
keyword is called static variable.
1. void function1(){
2. int x=10;//local variable
3. static int y=10;//static variable
4. x=x+1;
5. y=y+1;
6. printf("%d,%d",x,y);
7. }
It retains its value between multiple function calls.
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.
1. void main(){
2. int x=10;//local variable (also automatic)
3. auto int y=20;//automatic variable
4. }
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.
File name : myfile.h
1. extern int x=10;
2. //external variable (also global)
File name : program1.c
1. #include "myfile.h"
2. #include <stdio.h>
3. void printValue(){
4. printf("Global variable: %d", global_variable);
5. }
Basic Data Types
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
short 2 byte −32,768 to 32,767
int 2 byte −32,768 to 32,767
short
int
2 byte −32,768 to 32,767
long
int
4 byte -2,147,483,648 to 2,147,483,647
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:
do if char enum break signed const register
while else int extern continue unsigned struct sizeof
for goto float void default union return static
switch case double volatile auto typedef short long
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.
o Arithmetic Operators
o Relational Operators
o Shift Operators
o Logical Operators
o Bitwise Operators
o Ternary or Conditional Operators
o Assignment Operator
o Misc Operator
Precedence of Operators in C
The precedence of operator specie’s 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).
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 Multi Line Comments
1. #include<stdio.h>
2. int main(){
3. //printing information
4. printf("Hello C");
5. return 0;
6. }
1. #include<stdio.h>
2. int main(){
3. /*printing information
4. Multi-Line Comment*/
5. printf("Hello C");
6. return 0;
7. }
7. Single line comments are represented by
double slash //. Let's see an example of a
single line comment in C.
8. Multi-Line comments are represented by
slash asterisk * ... *. It can occupy many
lines of code, but it can't be nested
Escape Sequence in C
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.
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.
2 ways to define constant in C
There are two ways to define constant in C programming.
1. const keyword
2. #define pre-processor
const keyword #define pre-processor
1. #include<stdio.h>
2. int main(){
3. const float PI=3.14;
4. printf("The value of PI is: %f",PI);
5. return 0;
6. }
1. #include <stdio.h>
2. #define PI 3.14
3. main() {
4. printf("%f",PI);
5. }
We can’t modify a const object
The const keyword is used to define constant in C programming.
The #define pre-processor directive is used to define constant or micro substitution. It
can use any basic data type.
Syntax:
1. #define token value
Let's see an example of #define to create a macro.
1. #include <stdio.h>
2. #define MIN(a,b) ((a)<(b)?(a):(b))
3. void main() {
4. printf("Minimum between 10 and 20 is: %dn", MIN(10,20));
5. }
Output:
Minimum between 10
and 20 is: 10

More Related Content

PPTX
Local variables Instance variables Class/static variables
PPTX
computer Graphics
PPTX
Day 3 Compose Camp Material Design.pptx
PPTX
Introduction to Java
PPTX
Boxing & unboxing
PPTX
Pointers and Array, pointer and String.pptx
PDF
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
PDF
C Recursion, Pointers, Dynamic memory management
Local variables Instance variables Class/static variables
computer Graphics
Day 3 Compose Camp Material Design.pptx
Introduction to Java
Boxing & unboxing
Pointers and Array, pointer and String.pptx
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
C Recursion, Pointers, Dynamic memory management

Similar to Learn c language Important topics ( Easy & Logical, & smart way of learning) (20)

PDF
Basic Information About C language PDF
PPT
The smartpath information systems c pro
PDF
C programming language tutorial for beginers.pdf
PPTX
Programming in C.pptx
PPTX
c-introduction.pptx
PPTX
Best Computer Institute in Pitampura, Delhi, Learn from Industry Experts.
PPTX
unit 1 programming in c ztgdawte efhgfhj ewnfbshyufh fsfyshu
PDF
Introduction to C programming
DOCX
Complete c programming presentation
PPTX
C Language Introduction - Presentation pptx
DOCX
C programming language Reference Note
PDF
ICT1002-W8-LEC-Introduction-to-C.pdf
PDF
67404923-C-Programming-Tutorials-Doc.pdf
DOCX
The basics of c programming
PPTX
C Language ppt create by Anand & Sager.pptx
PPTX
Unit-1_c.pptx you from the heart of the day revision
PPTX
Lec 02 Introduction to C Programming.pptx
PPT
What is turbo c and how it works
PPTX
Introduction to C programming
PPT
Lecture 01 2017
Basic Information About C language PDF
The smartpath information systems c pro
C programming language tutorial for beginers.pdf
Programming in C.pptx
c-introduction.pptx
Best Computer Institute in Pitampura, Delhi, Learn from Industry Experts.
unit 1 programming in c ztgdawte efhgfhj ewnfbshyufh fsfyshu
Introduction to C programming
Complete c programming presentation
C Language Introduction - Presentation pptx
C programming language Reference Note
ICT1002-W8-LEC-Introduction-to-C.pdf
67404923-C-Programming-Tutorials-Doc.pdf
The basics of c programming
C Language ppt create by Anand & Sager.pptx
Unit-1_c.pptx you from the heart of the day revision
Lec 02 Introduction to C Programming.pptx
What is turbo c and how it works
Introduction to C programming
Lecture 01 2017
Ad

More from Rohit Singh (15)

PDF
Rohit RajSingh_ApplicationForm.pdf
DOCX
date2.docx
PDF
Data entry exclusive+
PDF
Assignment 1
PDF
Custom pagination
PDF
Sql exception and class notfoundexception
PDF
Angular Part 3 (Basic knowledge)
PPT
Project ppt, Learn Project Java
PPTX
5g networking technology
PDF
First program of C ( Complete Explanation )
PPTX
C language
PPTX
CCNA Course Training Presentation
PPTX
Html Basic
PPTX
Software testing basic
PDF
Core java interview questions
Rohit RajSingh_ApplicationForm.pdf
date2.docx
Data entry exclusive+
Assignment 1
Custom pagination
Sql exception and class notfoundexception
Angular Part 3 (Basic knowledge)
Project ppt, Learn Project Java
5g networking technology
First program of C ( Complete Explanation )
C language
CCNA Course Training Presentation
Html Basic
Software testing basic
Core java interview questions
Ad

Recently uploaded (20)

PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
OOP with Java - Java Introduction (Basics)
PPT
Mechanical Engineering MATERIALS Selection
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Structs to JSON How Go Powers REST APIs.pdf
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Construction Project Organization Group 2.pptx
PPTX
web development for engineering and engineering
DOCX
573137875-Attendance-Management-System-original
PPTX
Welding lecture in detail for understanding
PDF
Digital Logic Computer Design lecture notes
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPT
Project quality management in manufacturing
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
OOP with Java - Java Introduction (Basics)
Mechanical Engineering MATERIALS Selection
CH1 Production IntroductoryConcepts.pptx
Structs to JSON How Go Powers REST APIs.pdf
Embodied AI: Ushering in the Next Era of Intelligent Systems
Construction Project Organization Group 2.pptx
web development for engineering and engineering
573137875-Attendance-Management-System-original
Welding lecture in detail for understanding
Digital Logic Computer Design lecture notes
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Project quality management in manufacturing
Strings in CPP - Strings in C++ are sequences of characters used to store and...
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
bas. eng. economics group 4 presentation 1.pptx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Foundation to blockchain - A guide to Blockchain Tech

Learn c language Important topics ( Easy & Logical, & smart way of learning)

  • 1. Introduction The C is Procedure-oriented programming language that is developed for creating system applications that directly interact with the hardware devices such as drivers, kernels, etc. C programming is considered as the base for other programming languages, that is why it is known as mother language. It provides the core concepts like the array, strings, functions, file handling, etc. that are being used in many languages like C++, Java, C#, etc. C language is considered as the mother language of all the modern programming languages because most of the compilers, JVMs, Kernels, etc. are written in C language. A procedure is known as a function, method, routine, subroutine, etc. A procedural language specifies a series of steps for the program to solve the problem. A procedural language breaks the program into functions, data structures, etc. C is a procedural language. In C, variables and function prototypes must be declared before being used. C is considered as a middle-level language because it supports the feature of both low-level and high-level languages. C language program is converted into assembly code, it supports pointer arithmetic (low-level), but it is machine independent (a feature of high-level). A Low-level language is specific to one machine, i.e., machine dependent. It is machine dependent, fast to run. But it is not easy to understand. A High-Level language is not specific to one machine, i.e., machine independent. It is easy to understand. C Program In this tutorial, all C programs are given with C compiler so that you can quickly change the C program code. File Name : main.c #include <stdio.h> 1. int main(){ 2. printf("Hello C Language"); 3. return 0; 4. }
  • 2. C programming language was developed in 1972 by Dennis Ritchie at bell laboratories of AT&T (American Telephone & Telegraph), located in the U.S.A. Dennis Ritchie is known as the founder of the c language. It was developed to overcome the problems of previous languages such as B, BCPL, etc. Initially, C language was developed to be used in UNIX operating system. It inherits many features of previous languages such as B and BCPL. Let's see the programming languages that were developed before C language. Language Year Developed By Algol 1960 International Group BCPL 1967 Martin Richard B 1970 Ken Thompson Traditional C 1972 Dennis Ritchie K & R C 1978 Kernighan & Dennis Ritchie ANSI C 1989 ANSI Committee ANSI/ISO C 1990 ISO Committee C99 1999 Standardization Committee Features of C Language C is the widely used language. It provides many features that are given below. 1. Simple 2. Machine Independent or Portable 3. Mid-level programming language 4. Rich Library 5. Fast Speed 6. Recursion
  • 3. Simple C is a simple 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. Machine Independent or Portable Unlike assembly language, c programs can be executed on different machines with some machine specific changes. Mid-level programming language Although,C is intended to do low-level programming. It is used to develop system applications such as kernel, driver, etc. It also supports the features of a high-level language. That is why it is known as mid-level language. Rich Library C provides a lot of inbuilt functions that make the development fast. Speed The compilation and execution time of C language is fast since there are lesser inbuilt functions and hence the lesser overhead. Recursion In C, we can call the function within the function. It provides code reusability for every function. Recursion enables us to use the approach of backtracking. Pointer C provides the feature of pointers. We can directly interact with the memory by using the pointers. We can use pointers for memory, structures, functions, array, etc. 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; }
  • 4. #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. printf() The printf() function is used to print data on the console. return 0 The return 0 statement, returns execution status to the OS. The 0 value is used for successful execution and 1 for unsuccessful execution. How to compile and run the c program There are 2 ways to compile and run the c program, by menu and by shortcut. Flow of C Program The C program follows many steps in execution. To understand the flow of C program well, let us see a simple program first. File Name : simple.c #include <stdio.h> 5. int main(){ 6. printf("Hello C Language"); 7. return 0; 8. } Flow of Execution Let's try to understand the flow of above program by the figure given below.
  • 5. 1) C program (source code) is sent to preprocessor first. The preprocessor is responsible to convert preprocessor directives into their respective values. The preprocessor generates an expanded source code. 2) Expanded source code is sent to compiler which compiles the code and converts it into assembly code. 3) The assembly code is sent to assembler which assembles the code and converts it into object code. Now a simple.obj file is generated. 4) The object code is sent to linker which links it to the library such as header files. Then it is converted into executable code. A simple.exe file is generated. 5) The executable code is sent to loader which loads it into memory and then it is executed. After execution, output is sent to console. printf() and scanf() 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).
  • 6. 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); Variables in C A 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: 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';
  • 7. 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 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. 1. #include<stdio.h> 2. int main(){ 3. int number; 4. printf("enter a number:"); 5. scanf("%d",&number); 6. printf("cube of number is:%d ",number*number*number); 7. return 0; 8. } Output enter a number:5 cube of number is:125
  • 8. 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> 1. int main(){ 2. int x=0,y=0,result=0; 3. 4. printf("enter first number:"); 5. scanf("%d",&x); 6. printf("enter second number:"); 7. scanf("%d",&y); 8. result=x+y; printf("sum of 2 numbers:%d ",result); return 0; } Output enter first number:9 enter second number:9 sum of 2 numbers:18 Rules for defining variables o A variable name must be start with the alphabet, and underscore only. It can't start with a digit. o No whitespace is allowed within the variable name. o A variable name must not be any reserved word or keyword, e.g. int, float, etc. Valid variable names Invalid variable names 1. 2. int a; 3. int _ab; 4. int a30; 1. int 2; 2. int a b; 3. int long;
  • 9. 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. 1. void function1(){ 2. int x=10;//local variable 3. } 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. 1. int value=20;//global variable 2. void function1(){ 3. int x=10;//local variable 4. } It must be declared at the start of the block. Static Variable A variable that is declared with the static keyword is called static variable. 1. void function1(){ 2. int x=10;//local variable 3. static int y=10;//static variable 4. x=x+1; 5. y=y+1; 6. printf("%d,%d",x,y); 7. } It retains its value between multiple function calls. 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.
  • 10. 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. 1. void main(){ 2. int x=10;//local variable (also automatic) 3. auto int y=20;//automatic variable 4. } 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. File name : myfile.h 1. extern int x=10; 2. //external variable (also global) File name : program1.c 1. #include "myfile.h" 2. #include <stdio.h> 3. void printValue(){ 4. printf("Global variable: %d", global_variable); 5. } Basic Data Types 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 short 2 byte −32,768 to 32,767 int 2 byte −32,768 to 32,767 short int 2 byte −32,768 to 32,767 long int 4 byte -2,147,483,648 to 2,147,483,647 float 4 byte
  • 11. 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: do if char enum break signed const register while else int extern continue unsigned struct sizeof for goto float void default union return static switch case double volatile auto typedef short long 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. o Arithmetic Operators o Relational Operators o Shift Operators o Logical Operators o Bitwise Operators o Ternary or Conditional Operators o Assignment Operator o Misc Operator Precedence of Operators in C
  • 12. The precedence of operator specie’s 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). 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
  • 13. 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 Multi Line Comments 1. #include<stdio.h> 2. int main(){ 3. //printing information 4. printf("Hello C"); 5. return 0; 6. } 1. #include<stdio.h> 2. int main(){ 3. /*printing information 4. Multi-Line Comment*/ 5. printf("Hello C"); 6. return 0; 7. } 7. Single line comments are represented by double slash //. Let's see an example of a single line comment in C. 8. Multi-Line comments are represented by slash asterisk * ... *. It can occupy many lines of code, but it can't be nested Escape Sequence in C 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. 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. 2 ways to define constant in C There are two ways to define constant in C programming. 1. const keyword 2. #define pre-processor
  • 14. const keyword #define pre-processor 1. #include<stdio.h> 2. int main(){ 3. const float PI=3.14; 4. printf("The value of PI is: %f",PI); 5. return 0; 6. } 1. #include <stdio.h> 2. #define PI 3.14 3. main() { 4. printf("%f",PI); 5. } We can’t modify a const object The const keyword is used to define constant in C programming. The #define pre-processor directive is used to define constant or micro substitution. It can use any basic data type. Syntax: 1. #define token value Let's see an example of #define to create a macro. 1. #include <stdio.h> 2. #define MIN(a,b) ((a)<(b)?(a):(b)) 3. void main() { 4. printf("Minimum between 10 and 20 is: %dn", MIN(10,20)); 5. } Output: Minimum between 10 and 20 is: 10