Variables
- A variable is a named location present inside the memory, which holds a value that the program can modify.
- Variables must be declared before it’s used.
Syntax:
type variable_list;
- Here, the type must be a valid data type and any modifiers, and variable_list may consist of one or a list of identifiers separated by commas. Here are some declarations:
int i, j, k;
short int a;
unsigned int b;
double profit, loss;
Local Variables:
- Variables that are used inside a function or a method are called local variables.
- Local variables can be used only by statements present inside the block in which the variables are declared.
- A local variable is created upon entry into its block of code, and it is destroyed upon the exit of the block. Furthermore, a variable declared within one code block has no relationship with another variable with the same name declared within a different code block.
Example:
#include<stdio.h>
int main()
{
int x;
x=10;
if(x==10)
{
int x; /*This x hides the outer x*/
x=99;
printf("Inner x: %d\n",x);
}
printf("Outer x: %d\n", x);
return 0;
}
Output:
Inner x: 99
Outer x: 10
Global Variables:
- Global variables are known throughout the program and can be used anywhere in the program.
- They will hold their value throughout the execution of the program.
- Global variables can be created by declaring them outside of any function or a method.
Example:
#include<stdio.h>
/* global variable declaration */
int g = 20;
int main()
{
/* local variable declaration */
int t = 10;
printf("value of g is %d\n", g);
return 0;
}
Output:
value of g is 20
Data Types
- It is a set (range) of values that has similar characteristics.
- It is a collection of data with a value that has a meaning and its characteristics.
- It determines the type of data that is held by a variable.
- The C language has predefined data types to handle the various kinds of data used in the program, where each variable has been associated with specific data types.
Example:
int x;
Where “int” is an integer data type and “x” is a variable.
The above example shows that the variable “x” can store only the integer type value.
Data Types | Memory Size | Ranges |
Char | 1 byte | -128 to 127 |
unsigned char | 1 byte | 0 to 255 |
Short | 2 bytes | -32,768 to 32, 767 |
unsigned short | 2 bytes | 0 to 65, 535 |
Int | 4 bytes | -32,768 to 32, 767 |
unsigned int | 4 bytes | 0 to 65, 535 |
long int | 4 bytes | -2, 147, 483, 648 to 2, 147,483, 647 |
unsigned long int | 4 bytes | 0 to 65, 535 |
Float | 4 bytes | |
Double | 8 bytes | |
long double | 10 bytes |
Int:
The integer represents the whole number (zero, positive, negative, and non-decimal numbers).
Example: -2, 0, 2
(int a = -2) or (int b= 0) or (int c=2)
int x;
Where “x”, can be any integer value.
int x,y,z;
We can also initialize the ‘n’ number of variables.
The following is the example:
int c = -2;
int d = 2;
Example Program:
#include<stdio.h>
int main()
{
int age=10;
printf("Tommy is %d years old", age);
return 0;
}
Output:
Tommy is 10 years old
char:
Example:
#include<stdio.h>
int main()
{
char ch='A';
printf("%d", ch);
return 0;
}
Output:
65
double:
Example:
#include<stdio.h>
int main()
{
double d= 28.888;
printf("Double data type: %lf\n",d);
return 0;
}
Output:
Double data type: 28.888000
float:
Example:
#include<stdio.h>
int main()
{
float marks=10.5;
printf("marks = %f\n", marks);
return 0;
}
Output:
marks = 10.500000
Enumeration
- An enumeration is a set of user-defined integer constants.
- It is a user-defined data type in C.
- The enumeration in C is represented using the keyword enum.
- Enumerations are very common in everyday life.
- For example, an enumeration of a week is Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday.
- Enumerations are defined much similar to structures.
- Two enum names in a program can have the same value.
Syntax:
enum enum_name { enumeration list };
Here, enum_name is the meaningful name for the enumeration list.
Program:
#include<stdio.h>
enum week {Monday=1, Tuesday=3, Wednesday=3, Thursday=4, Friday=5, Saturday=6, Sunday=7};
int main()
{
enum week day;
day = Monday;
printf("Monday is the %d th day of the week", day);
return 0;
}
Output:
Monday is the 1 th day of the week
- If we do not explicitly provide values to enum names, the compiler automatically assigns values to enum names starting from 0.
Example:
#include<stdio.h>
enum week {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
int main()
{
enum week day;
day = Friday;
printf("Day = %d", day);
return 0;
}
Output:
Day = 5
ASCII Value
- ASCII full-form: American Standard Code for information interchange.
- In C programming, each character is denoted with some ASCII code, and each ASCII code holds 7 bits in memory.
- A character variable does not hold a character value itself, and it has the ASCII value of the character.
- The ASCII value is the numerical representation of a character.
- Each character variable in the C language is assigned with some number whose range is from 0 to 127.
Example:
ASCII value of ‘A’ is 65.
Here, 65 will be stored in the character variable rather than ‘A’. i.e., we assign ‘A’ to the character variable whose ASCII value is 65.
Program:
#include <stdio.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf("%c",&ch);
printf("\n The ASCII value of the entered variable is : %d", ch);
return 0;
}
Output:
Enter a character: B
The ASCII value of the entered variable is : 66
Comments
- In C, Comments are used to explain the operation of the code whenever they are needed.
- The C compiler ignores any text between the beginning and ending comment symbols.
Types of comments:
Single-line comment:
- Single-line comments start with // and end at the end of the line.
- It is useful, especially when there is a need for short or line-by-line descriptions.
Example:
// This is a single-line comment
Multi-line comment:
- Multi-line comment begins with the character pair /* and ends with */.
- In this style of comment, the text of the comment may extend over two or more lines.
Example:
/* this is a multi-
-line comment */
Program:
#include<stdio.h>
int main()
{
printf("Hello! ");// printing Hello!
printf("Let's learn C language with ScriptWithMe.");
/* printing
information
*/
return 0;
}
Output:
Hello! Let's learn C language with ScriptWithMe.
Who is the course for?
- This course is designed for the students, employees, software programmers who want to learn C programming from scratch to end.
- After successfully completing this course you will be able to understand how the programming works and use it in the real time environment.
Prerequisites before starting this course:
- Before entering into this course you should have the basic understanding of how computer technology works.
- If you don’t have any experience in C or any other programming language then this course is for you.
- If your intermediate level of knowledge in C programming and you want to enhance your C skills to advance level then this course is for you.
- All you need is just any of the one device mobile, laptop, or PC and internet connection.
Questions 🙵 Answers.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum
Video 48 Min + 2 Min read to complete
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum
Video 48 Min + 2 Min read to complete
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum
Video 48 Min + 2 Min read to complete
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum
Video 48 Min + 2 Min read to complete
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum
Video 48 Min + 2 Min read to complete