Day 2 Clang
Day 2 Clang
In Hindi #5
In this tutorial we are going to understand the basic syntax of a C program.
We will be using the same program we have been using for previous two
programs, so you may develop a better understanding about the syntax
before proceeding further. So, let’s get started with our tutorial.
A C program is made up of different tokens combined together. These tokens
include:
Keywords
Identifiers
Constants
String Literal
Symbols
int a;
printf("Enter number a\n");
scanf("%d", &a)
return 0;
Copy
I have written a four-line code above so I can explain the tokens in a better
way by using the references from the code above.
Keywords:
Keywords are reserved words that can not be used elsewhere in the program
for naming a variable or function, instead they have a specific function or tasks
and they are solely used for that. In the above given code, the return
statement in the third line is a keyword.
Image: List of all the Keywords of C
Identifiers:
Constant:
Constant are very similar to variable and their values can be of any data type.
The only difference between constant and variable is that a constant’s value
never changes. We will see constants in more detail in the upcoming tutorials.
In the above given code the “0” in the last line is a constant.
String literal:
String literal or string constant is a line of characters enclosed by double
quotes. In the above given code “Enter number a” is a string literal. printf is
being used there to print string literal onto the screen.
Symbol:
Symbols are special characters reserved to perform certain actions. They are
used to notify the compiler so they can perform specific tasks on the given
data. In the above example code & is being used as a symbol.
Let’s talk a little about white space. White space or blank space does not
create any difference while using C. Unlike Python where we have to press
enter to go to new line, in C we use semi-colon (;) to end a line of code. So
until a semi colon arrives, the compiler will treat the code as a single liner so
no matter how many lines we consume the code will run accurately if written
correctly.
There are two code snippets given below. You can notice that they differ a lot
regarding while space but their execution wills how the same output onto the
screen i.e. “Hello World”.
Code1:
#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
Copy
Code2:
#include <stdio.h>
int main()
{
Printf
(
"Hello World\n"
)
;
return 0;
}
Declaration:
We cannot declare a variable without specifying its data type. The data type
of a variable depends on what we want to store in the variable and how much
space we want it to hold. The syntax for declaring a variable is simple:
data_type variable_name;
Copy
or
Copy
the data type can be int, float, char, depending on what kind of value we want
to store.
Naming a Variable:
A variable name can be of anything we want to call out variable. Yet there are
specific rules we must follow while naming a variable:
A variable name can contain alphabets, digits, and underscore (-) only.
The starting letter can not be a digit.
White spaces cannot be used.
The name should not be reserved keyword or special character.
1st way:
int a = 12;
Copy
2nd way:
int a;
a= 12;
Copy
Both of these have exactly the same working.
A variable as it names define can be altered, or its value can be changed, but
same is not true for its type. If a variable is of integer type, then it will only
store an integer value, which means that we cannot assign a character type
value to an integer variable. We can not even store a decimal value into an
integer variable.
Let’s see this with an example:
Example 1:
#include <stdio.h>
int main()
{
int a = 12.2221;
printf("Output = %d" , a);
return 0;
}
Copy
We are sending 12.2221 as a value in a, but since it is an integer type variable,
the output will be only 12.
Output = 12
Copy
Example 2:
#include <stdio.h>
int main()
{
float a = 12.2221;
printf("Output = %f" , a);
return 0;
}
Copy
Here we are using float as a data type. In this case, you can see the output
below is 12.222100
Output = 12.222100
Copy
Note that we used %f instead of %d in case of float.
The reason is that int can store only 2 bytes worth data as its storage capacity
is 2 bytes while float storage capacity is 4 bytes.
float 4
double 8
long double 10
#include <stdio.h>
int main()
{
printf("%lu",sizeof(int));
return 0;
}