0% found this document useful (0 votes)
7 views15 pages

C Programming Basics and Structure

Uploaded by

shaheedp124
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views15 pages

C Programming Basics and Structure

Uploaded by

shaheedp124
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

.

What is C ?
C is a general -purpose, procedural, high-level programming language used in
the development of computer software and applications, system programming
games, and more.
 C language was developed by “Dennis Ritchie” at the Bell Laboratories in
1972.
 It is powerful and flexible language which was first developed for the
programming of the UNIX operating System.
 C is the one of the most widely used programming languages .

Structure of C language
Syntax: #include<stdio.h>
main()
{
// code
}
Note: 1] C is a Case-Sensitive Language
2] .c is an extension of C language file
3] c language support 32 keywords
Ex: #include<stdio.h>
void main()
{
.

printf(“hello world”); //output


}
OR
#include<stdio.h>
int main()
{
printf(“Hello World”);
return 0;
}

Tokens
Tokens are the smallest element of program is know as tokens.
Types of Tokens in c
[Link]
[Link]
[Link]
[Link]
[Link] Symbols
[Link] Literals

[Link]
-Keywords are pre-defined reserved words in a programming.
-they are written in lower case.
-c language support 32 keywords.
EX: auto unsigned enum if
struct continue typedef while
break signed extern int
.

switch default union long


case sizeof float short
register do for
char static void
return double goto
const else volatile
Characteristics
-Case-sensitive.
-Cannot be used as identifiers.
[Link]
Identifiers are names given to various elements in a C program. Such as a
variable,functions,arrays and structure.
Rules of Naming Identifiers
 Must start with a letter(uppercase or lowercase)or an underscore(_).
 Can be followed by letters,digits(0-9) or underscores.
 Case-sensitive.
 Must not be a keyword
EX:myVariable

[Link]
Constants in c are fixed values that do not change during the execution of a
[Link] can be of various types,including integer,floating-
point,character and stirng literals.
[Link] Constatnts
EX:123
[Link]-point
EX:3.14
[Link] Constants
.

EX: ‘A’
[Link] Literals
EX:”Hello World”

[Link]
[Link] Operators: perform arithmetic operations.
EX:+,-,*,/,%
[Link] Operators: compare two values
EX: ==,!=,<,>,<=,>=
[Link] Operators: perform logical operations
Ex: &&(logical AND),|| (logical OR),!(logical NOT).
[Link] operators
EX: &(bitwise AND),|(bitwise OR).
[Link] Operators:Assign values to variables.
EX: =(simple assignment),+=(add and assign).

[Link] Symbols
EX:
 Braces{}: Curly braces,used to define a block of code.
 Parentheses(): [Link] to indicate function calls and function
parameters.
 Brackets[]: Opening and Closing brackets are used as array element
references.
 Semocolon(;): Semicolon,used to terminate statements.
 Comma(,) : Comma,used to separate variables and function arguments.
 Pre-processor#: Hash,used in pre-processor directives.
The preprocessor is macro processor that is used
automatically by the compiler to transform your program before actual
compilation.
.

 Period(.) : It is used to access members of a structure and union.


 Tilde(~) : Bitwise One’s Complement Operator.
 Assignment operator(=) : It is used to assign values and for logical
operation validation.
 Asterisk(*) : It is used to create a pointer variable and for the
multiplication of variables.
[Link] Literals
A sequence of characters enclosed in double quotes.
EX:” HelloWorld !”.

Data Types
-Data types are the types of data which is used in the program.
-Data types are the depends on variable
There are Four Data Types
1. Primitive Data Types (Basic Data Types)
2. Derived Data Types
3. User-Defined Data Types

1. Primitive Data Types


- Primitive data types are the primitive type of value. representing
values int,char,float,char etc.
 Integer Data Type
-The integer data type in c used to store the integer
numbers(any number including positive or negative and zero
without decimal part)
Range- -2147 to +
Size-2 byte
Format Specifier- %d
EX: int main()
{
int a=10;
.

printf(“value of a:%d”,a);
}

 Character Data Type


-Character data type allows its variable to store only a single
character. the size of character is 1 byte.
Range- (-128 to +127)
Size- 1 byte
Format Specifier- %c
EX: int main()
{
char a=’a’;
printf(“value of a : %c”,a);
return 0;
}

Float Data Type


Float data type is used to store floating point values.
Rang- 1.2E-38 to 3.4E+38
Size- 4 byte
Format Specifier-%f
EX:
int main()
{
float a=9.0;
float b=2.5;
printf(“%f”, a);
printf(“%f”, b);
return 0;
}
Double Data Type
.

A double data type in c is used to store decimal numbers(numbers with


floating-point values)with double precision.
Range-2.3E -308 to +1.7E +308
Size-8 byte
Format Specifiers-%d

2] Derived Data Types


The data types that are derived from the primitive or build-in data types are
referred to as derived data types.
EX: Array,pointer,function.
3] User-Defined Data Types
The user defined data types are defined by the user himself is known as
derived data types.
EX: structure, union.
Program Using Different Data Types
#include<stdio.h>
int main()
{
int a=100;
float b=331.79;
double d=8.44e+11;
char ch=’A’;
printf(“Integer : %d\n”,a) ;
printf(“Float : %f\n”,b) ;
printf(“double: %d\n”,d) ;
printf(“Character : %c\n”,ch) ;
return 0;
.

Variables
Variables it is a name given to block of memory to stored values is known as
variable.

Rules for Naming Variables in C


1. A variable name must only contain alphabets,digits,and underscore.
2. A variable name must start with an alphabet or an underscore [Link]
cannot start with a digit.
3. No white space is allowed within the variable name.
4. A variable name must not be any reserved word or keyword.
Syntax: datatype variablename=value;
1] Declaring Variable
 Data type- specifies the type of data the variable will hold.
EX: int,float,char
Syntax: datatype variablename;
 Variable name- the name of variable,which must follow the rules for
identifiers.
EX: int age;
Float salary;
Char grade;

2] Initializing variable- The initial value assigned to the variable


EX: int age=20;
Syntax : datatype variablename=value;
There are Three Types of variable
1] Local Variable
2] Global Variable
3] Static Variable
4] Extern Variable
.

1] Local Variable
- Local Variable declared inside the Function or block.
EX: #include<stdio.h>
void function()
{
int a=10; // Local Variable
printf(“%d \n”,a);
}
2] Global Variable
- Global Variable are declared outside of the functions or block.
- We can accept global variable from anywhere.
EX: main()
{
int a=20; // Global Variable
void myFunction()
{
printf(“%d \n”,a);
}
}

3] Static Variable
- A variable that is declared with the static keyword is called Static Variable.
EX: void main()
{
fun(); // function call
.

}
void fun()
{
int a=10;
static int b=20;
a++;
b++;
printf(“%d %d “,a,b);
}

5] Extern Variable- We can store variable in multiple sources files by using


external variable
EX: main()
{
extern a=10;
}
int a=10;
void fun()
{

}
void sys()
{

Example of Program Using Different Types of Variables


.

#include<stdio.h>
int globalVar = 10; //global variable
void myFunction()
{
static int staticVar = 5 ; //static local variable
int localVar =2 ;
staticVar++;
localVar++;
printf(“Global Variable : %d\n”,globalVar);
printf(“Static Variable : %d\n”,staticVar);
printf(“Local Variable : %d\n”,localVar);
}
int main()
{
myFunction();
myFunction();
myFunction();
return 0;
}

Q1] Write a program addition of two numbers


#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,add;
.

printf(“Enter two numbers”);


scanf(“ %d %d” , &a, &b);
add=a+b;
printf(“addition of two numbers : %d” , add);
return 0;
}

Q2] Write a program swapping of two number


#include<stdio.h>
int main()
{
int a,b,temp;
printf(“Enter two numbers”);
scanf(“ %d %d “, &a ,&b);
temp=a;
a=b;
b=temp;
printf(“ %d %d “, a,b);
return 0;
}
.

Q3] Write a program to check even or odd number in c program.


#include<stdio.h>
void main()
{
int no;
printf(“Enter the number”);
scanf(“ %d “, % no);
if(no %2==0)
{
printf(“ %d is even no”);
}
else
{
printf(“ %d is odd no”);
}
}

Q3] Write a program to find square and cube given no in c.


#include<stdio.h>
main()
{
int a,b,sqr,cube;
printf(“Enter the number”);
scanf(“%d”, &a);
printf(“Enter the number”);
scanf(“%d”, &b);
.

sqr = a*a;
cube=b*b*b;
printf(“The square is : %d”, sqr);
printf(“The cube is : %d”, cube);
}
Q4] Write a program to generate Fibbonaccies series foe given number.
->Fibbonaccies series is the sequence where each number is the sum of
previous two numbers of the sequence is called as fibbonaccies series.
EX: 0,1,1,2,3,5,8,13,21,34 etc.
Program
#include<stdio.h>
int main()
{
int n;
int a=0, b=1, c, i;
printf(“Enter number);
scanf(“%d”, &n);
for(i=0; i<=n; i++)
{
printf(“%d”, a);
c=a+b;
a=b;
b=c;
}
return 0;
}
.

You might also like