How to create 'C' Program?
1. Open C editor
2. The first line of the program #include <stdio.h> is a preprocessor command, and all required header file.
3. Declare required variables.
4. The next line int main() is the main function where the program execution begins.
5. The next line printf(...) is another function available in C which display the message.
6.The next line scanf(...) statement to accept value of variable.
7. Use calculation if required.
8. Select Save as option and go to the directory where you have saved the file with filename.c.
9. Compile and Execute C Program.
First Program in C.
#include <stdio.h> int main() { printf("Hello, World! \n"); return 0; }
Sum of two integer vlaues.
#include <stdio.h> int main() { int a,b,s=0; printf("Enter two numbers"); scanf("%d %d",&a,&b); s=a+b; printf("Sum=%d",s); return 0; }
Variables in C
Which value is changable is called variable. Each variable in C has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
Variables declaration in C
type variable_list;
int i, j, k; char c, ch; float f, salary; double d;
The line int i, j, k; declares and defines the variables i, j, and k; which instruct the compiler to create variables named i, j and k of type int.
Print Sum of four given integer values.
#include <stdio.h> int main() { float a,b,c,d,s=0,avg=0; printf("Enter four numbers"); scanf("%f %f%f%f",&a,&b,&c,&d); s=a+b+c+d; avg=s/4; printf("Sum=%f Avg=%f",s,avg); return 0; }
Lvalues and Rvalues in C
There are two kinds of expressions in C −
lvalue − Expressions that refer to a memory location are called "lvalue" expressions. An lvalue may appear as either the left-hand or right-hand side of an assignment.
rvalue − The term rvalue refers to a data value that is stored at some address in memory. An rvalue is an expression that cannot have a value assigned to it which means an rvalue may appear on the right-hand side but not on the left-hand side of an assignment.
Variables are lvalues and so they may appear on the left-hand side of an assignment. Numeric literals are rvalues and so they may not be assigned and cannot appear on the left-hand side.
0 Comments