What is Program Structure?
Comments in a C program are code that is not executed by the compiler.
Source: Pixabay |
Comment Type:-
There are two types of comments.
(i) Single Line Comment (//) :-
One line comments are used for one line comments.
Exa:- // This is a single line comment
(ii) Multiline Comment (/ * */) :-
Multiline comments are used to write comments of more than one line.
Example:-
/* this
is
A
Multiline
Please comment
,
Basic structure of C program:-
To write a C program, we have to go through the following steps.
Document section
Preprocessor/Link section
Definition Section
Global Declarations Section
Function declaration section
main function
User defined function section
Documentation section (optional)
1. Preprocessor Section (Link Section):-
In this section, we declare all the header files used in the program.
Syntax :- #include <header_file.h>
Example :- #include <stdio.h>
# include <conio.h>
# include <math.h>
#include <string.h>
2- User Defined Function Category:-
In this section, the program is called. Write in multiline comments.
Example:-
,
File Name :- File_Name.C
Author Name: Author_Name
Date:- Date of the programme
Description:- What to program */
3- Definition Section (Optional):-
In this section, all symbolic constants and macros used in the program are defined, where the "define" keyword is used.
Example:-#define PI 3.14
4- World Declaration Section (Optional)
Global Variables Declared in this section, global variables can be used anywhere in the program.
Example:- int result
5- Function Declaration Section:-
This section declares all the functions used in the program.
Syntax :- Return_Type Function_Name();
Example :- int Function_Name();
6- Main Function Section (Important):-
This section contains the complete code of our program,
Whenever our program is run, the main() function is executed first.
Sentence Structure :-
int main()
,
//code
,
7- USER DEFINED FUNCTION SECTION :-
All user-defined functions are declared in this section.
Downloading Dev-C++ :-
Dev, C++ is an application software (computer app) in which you write and run your C program code, you can also use other applications if you want.
To download Dev, C++ click the link below
Link: https://sourceforge.net/projects/orwelldevcpp/
Writing the first C program:-
#include <stdio.h> // Header file
int main() // Main function
,
printf("hello world"); // code
getch(); //code
return 0; // This means return nothing
,
Output :-
oh world
here
#include <stdio.h> :- Here we include the header file named stdio.s, with the help of this header file we can use printf() function.
int main() :- Here int is a return type. int says that this function must return something, so we return 0 at the end of the program.
Written
If we use void instead of int, void tells our program to return nothing i.e. "Null" and we get return 0.
No need to even write.
main() :- Here Main() is the function name, we cannot change this function name, changing it will show program error.
{} :- These are called curly braces. They contain all the code of the main function.
return 0 :- return 0
It says that the program returns nothing.
Execute the program
If you have created the program in Devc++, click Compile & Run or press F11 key to run the program you created, then a black window (console window) will open with the output of your program.
Translator
It converts code written in programming language into machine language (binary language). This is called a translator.
There are following types of translators.
assembler
the interpreter
compiler
1- Assembler:-
A program that translates or converts assembly language (middle-level language) to machine language (binary language). It is called assembler
2- Interpreter (interpreter):-
A program that translates a high-level language into line-by-line machine language (binary language). Interpreter translates high level languages like Perl, Python, Matlab into binary language.
3- Compiler Compiler :-
A program that translates a high-level language like C into a binary language is called a compiler. The compiler does not execute the code line by line.
Rather, it simultaneously translates the entire code into binary.
Input/Output Statement :-
We use output statement to get input statement and output show to take some user input in our program.
To use input/output statements in C, you must include a header file called stdio.h in your program.
The full form of stdio.h is 'Standard Input Output Header File'. It contains all I/O functions.
Input and output functions in C are divided into 2 categories.
1- Formatted input and output functions:
scanf()
printf()
2- Formatted input and output functions:
gether()
putchar()
get()
keep()
Note:- We will go further on gets() and puts().
Format Specifier:-
In the C language, format specifiers are used when inputting variables and outputting variables.
Some common data types and their format specifiers are given in the table below.
printf() :-
printf is used to output.
sentence structure
printf("String");
Or
printf("format-specifier", variable or expression);
Event:-
# Include <stdio.h>
int main()
,
int roll = 125
int age = 18
char gender = 'M'
printf("%d", roll)
printf("\nYour Age: %d", age)
printf("\nYour Roll No: %d, Age: %d and Gender: %c", roll, age, gender)
Return 0
,
Output:-
125
Your age: 18
Your Roll No: 125, Age: 18 and Gender: M
getchar():- getchar function is used to take input of a character.
Syntax :- variable = getchar();
Example:
char Ch;
Ch = getchar();
Putchar:- putchar() function is used to output a character.
Sytax :- puter (variable);
Such as:
char Ch = getchar();
putchar(Ch);
getchar() and putchar() Function Example Program:-
# Include <stdio.h>
int main()
,
four grades;
printf("Enter your grade:");
grade = getchar();
printf("Your grade is");
putchar(grade);
return 0;
,
Output: - Enter your grade: A
A in your grade
Escape Sequence :-
The escape sequence is used to format the output with the printf() funciton.
The following list provides some important escape sequence characters.
\n :- Create new line
\t :- Give a tab
\' :- Print single quotes
\" :- Print double quotes
\\ :- Print back slash
Escape Sequence Example Program :-
> #include <stdio.h>
int main()
,
printf("Ninja \ncoder\n");
printf("Ninja \tcoder\n");
printf("Ninja \'Coder\n");
printf("Ninja \"coder");
return 0;
,
Output:-
Ninja
Coder
Ninja Coder
Ninja' Coder
Ninja "Coder"
Keywords:-
Words like laughing, crying, running already have a meaning in our language Hindi and so we cannot use these words for any other purpose, same way it happens in program.
Keywords are reserved words that already have specific meanings in a C program.
In other words, C programming keywords are used only for predefined functions.
C programming language has 32 keywords and C keywords are always written in lowercase letters. Below is a list of all 32 keywords
Data Types:-
C programs provide different types of data to handle different types of data.
Types of Data Types:- There are 2 types of data types in C programming.
1. Primitives :- int, char, float, Void.
2. Derived/Non-Primitive or (User Defined):- Array, String, Structure, Union, Pointer, Function, Typedef, Enum.
Note:- We will read more about Derived Datatypes.
1. Primitive Data Types :-
Primitive data types are also called fundamental data types or built in or basic data types.
int :- int is for storing integer (integer) values.
float:- It is meant to store floating point values (decimal values).
Char:- value to be stored.
Void:- Void means Null ie it does not store anything.
Variable:-
Variables are created to store data in C, variables are like containers that are used to store data.
The value of a variable can be changed anywhere in the program. Variable declaration syntax
data _type variable_name;
is
Data _Type Variable_Name1, Variable_Name2, … Variable_Namen;
Such as: -
int age;
char gender;
float Height;
Variable Initialization :-
data_type variable_name = value or expression;
is
data_type variable_name;
variable_name = value or expression;
Variable Example Program:-
# Include <stdio.h>
int main()
,
int num1 = 10, num2 = 20;
float height, weight;
height = 6.6;
weight = 10 * 5 + 5.6;
printf("num1: %d, num2: %d, Height: %f Feet and Weight: %f Kg", num1, num2, Height, Weight);
return 0;
,
Output :-
Number 1 : 10, Number 2 : 20, Height : 6.600000 ft and Weight : 55.599998 kg
Constant:-
If we want to fix the value of any variable in our program, we need to make it constant.
To create a constant, the "const" keyword is used before the datatype. While creating a constant, we have to assign its value and the value of that constant cannot be changed later in the entire program.
Sentence Structure :-
Const Data_Type Constant_Name = Value;
Constant Example Program :-#include <stdio.h>
int main()
,
const float PI = 3.14;
printf("PI = %f", PI);
return 0;
,
Output:-
PI = 3.140000
Expression:-
In C programming, the combination of Constant, Variable and Operators is called Expression.
Example :- X = A * B + C - D
Operands:-
A variable or constant used with an operator in an operation is called an operand.
Example:- C = A + B
Here C, A and B are operands,
and = ,+ are operators.
Operator:-
Operators are some special symbols of C programming. whose meaning and function are already determined.
Types of Operators:-
- Arithmetic operators
- Assignment operator
- Relational or comparison operators
- logical operator
- Increment and Decrement Operators
- Conditional or ternary operator
- bitwise operator
1. Arithmetic Operators:-
You are already familiar with arithmetic operators because since childhood we have been doing addition (+), subtraction (-), multiplication (*), division (/) and modulus (%).
Addition(+):- To add operands.
Subtract (-) :- Subtraction operand.
Multiplication (*):- To multiply operands.
Division (/) :- Division into operands.
Modulus (%) :- Divide the operand to get the remainder.
2. Assignment Operator:-
Assignment operators are used to evaluate (solve) mathematical expressions with arithmetic operators and assign the result to a variable.
The assignment operator (=) assigns (copies) the resulting value to the right-side or li value, variable, constant, or expression to the left-side variable.
Simple Assignment (=):- Assigning value to left-hand side variable by solving right-hand side value or expression.
Additive Assignment (+=) :- Assignment to the left side variable by adding the left and right side values (variables).
Subtraction Assignment (-=) :- Subtracting left and right side values (variables) and assigning to the left side variable.
Multiplication Assignment (*=) :- Assigns left and right side values (variables) to the left side variable by multiplication.
Division Assignment (/=) :- Assignment of left and right side values (variables) to the left side variable by division.
Module Assignment (%=) :- Dividing left and right side value (variable) and assigning remainder to left side variable.
Relational or Comparison Operators:-
Relational operators are used to compare the relation of 2 values. These operators return 0 if the relation is False and return a non-zero integer if True.
Equal to (==) :- Returns true if both operand values are equal
Not Equal to (!=) :- will return true when the values of both operands are different
Less than (<) :- Returns true when the value of the left operand is greater than the value of the right operand.
Greater than (>) :- will return true when the value of the left operand is greater than the value of the right operand.
Less than or equal to (<=) :- Returns true when the value of the left operand is less than or equal to the value of the right operand.
Greater than or equal to (>=) :- Returns true when the value of the left operand is greater than or equal to the value of the right operand.
Logical Operators:-
Sometimes in our programs we need to define multiple conditions that are related to each other and for that we use logical operators.
Logical operators return false i.e. 0 and true i.e. non-zero integer after checking the result value (true, false) of all conditions.
AND (&&) :- will return true when all conditions are true
OR (||) :- Returns true if any one of the conditions is true.
NO (!):- This condition stores results like true to false and false to true.
Increment and Decrement Operators:-
Increment ( ++ ) and Decrement ( — ) operators are unary operators, that is, they are used only with a single variable.
With the increment operator, the value of the variable is increased by 1 number and with the help of decrement operator, the value of the variable is decreased by 1 number.
Such as: -
You can also write X = X + 1 as ++X
You can also write X = X – 1 as –X
Conditional or Ternary Operator:-
If we use conditional operators to make decisions like if-else statements.
You can also say that it is an abbreviation of If-Else.
Two symbols ( ? ) and ( : ) are used in the conditional operator (Ternary Operator).
Condition is followed by True Expression (Statement) before question mark ( ? ) and False expression is followed by Colon Mark ( : ).
Sentence structure:-
(condition)? Satya-Prakash : False-Prakash;
Such as: -
(6%2 == 0)?printf("True"):printf("False");
Bitwise operator:-
Bitwise operators are used to perform operations between operands at the bit level. Bitwise operators first convert the operands to bits and then perform the operation.
& :- bitwise AND operator
, :- bitwise OR operator
^ :- Bitwise exclusive or operator
~ :- one's complement operator (unary operator)
<< :- left shift operator
>> :- Right shift operator
Data Type Costing:-
In C programming, changing the value of one data type to the value of another data type is called type casting.
Type casting type
- Implicit type casting (automatic conversion)
- Explicit Type Casting (Narrow Conversion)
1. Implicit Type Casting :-
Such type casting is done automatically.
char -> Short -> int -> Long float -> Double -> Long Double
The data type order from smallest to largest is given above. And implicit type casting works during this command.
Implicit Type Casting Example Program :-
2. Explicit Type Casting :-
When we change the value of the data type to take more memory then the value of the data type takes less memory. Hence it is called Explicit Type Casting.
long double -> double -> float -> long -> int -> short-char
Syntax :- variable = (data-type) variable or value or expression
Note:- In C programming, data can be lost in explicit type casting, like when you cast float data type to int the decimal part will be lost.
Explicit Type Casting Example Program :-
# Include <stdio.h>
int main ()
,
int N;
float X = 20.75;
// Explicit conversion
N = (int)X;
// print statement
printf("Value of X : %f\n", X);
printf("Value of N : %d", N);
return 0;
,
Output:-
Value of X: 20.750000
Value of N: 20
Storage Classes:- The storage classes are as follows.
automatic (automatic)
registration
Fixed
external
1. Automatic:-
The variables we declare inside the function (Block). They are called local variables.
This is the default meaning you don't need to enter the auto keyword to make the auto variable.
Sentence Structure :-
data_type variable_name;
\\ or
auto data_type variable_name;
2. Registration:-
Variables defined in the RegisterStories class are stored in a register (which is usually equal to one character) instead of RAM, which means that the storage size of a variable is equal to the maximum value of the register. The register storage class is defined for variables whose values are to be repeated over and over again. Like the counter.
Example:
int age register;
The point to note here is that when a variable is defined as a register, it does not mean that the variable will be stored in the register or not, it depends on the hardware of that computer and its operating system.
3. Static:-
A static class instructs the compiler to keep this variable active throughout the duration of the program and not to create and initialize the variable every time it is called and destroy it as soon as the call ends (goes out of scope). Say, the difference between local variables and static variables is that each time a call is made, local variables are created while static variables save their state and start from there next time. Actually when allocating memory when we use local variables, memory. That function is also assigned with that variable but when we define static variable then the value of that static class variable is defined in a shared space. which can be accessed by all functions.
4. Externalities:-
By using extern keyword we can define a global variable which we can use in any other file as well. When we define a global variable, it is not defined but it is defined in a separate file and only reference is returned to that file, which we can later access from any other file.