0% found this document useful (0 votes)
7 views

Chapter

This document discusses variables, data types, input/output, and basic C program structure in 3 paragraphs. It defines variables and rules for naming variables. It describes how to write variables of different data types like int, char, and float. It also discusses constants, keywords, comments, format specifiers, and provides two examples of simple C programs to calculate the area of a square and circle.

Uploaded by

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

Chapter

This document discusses variables, data types, input/output, and basic C program structure in 3 paragraphs. It defines variables and rules for naming variables. It describes how to write variables of different data types like int, char, and float. It also discusses constants, keywords, comments, format specifiers, and provides two examples of simple C programs to calculate the area of a square and circle.

Uploaded by

KESHAV SINGH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Variables, Data Types + Input/Output

'C' Program structure -

# include<stdio.h>

int main() {
printf("Hello World");
return 0;
}

Variables - Variable is the name of a memory location which stores some data.

Rules for naming of Variable-

1. Variables are case sensitive.


for ex.- int a = 30;
int A = 40; both are different

2. 1st character is alphabet or "_" (underscore).


for ex.- int number = 40;
int final_price = 100;

3. no comma/blank space .
for ex.- int number = 40; - Right
int final score. =100; - wrong, as no blankspace or full stop.

4. No other symbol other than "_" .


for ex.- int new_price = 80; - Right
int new#price = 80; - wrong as symbols other than "_" are not allowed.

How to write variables:

# include<stdio.h>

int main() {
int number = 25; - for data types like numbers.
char star = "*"; - for special character.
int age = 22;
age = 25; - updated age in the same section.
float pi = 3.14; - for decimal numbers.

int a = 30;
int A = 25; both are different variables.

return 0;
}

* Variables name should always be meaningful. so that we can understand the


variable in single attempt.

Constants - Values that don't change.

1. Integer Constants - 1,2,3,0,-1,-2.


2. Real Constants - 1.0,2.0,3.14,-2.4 .
3. Character Constants - 'a', 'B' , 'b' , '#' .

Keywords - Reserved words that have special meaning to the compiler.


There are 32 reserved keywords in 'C'.
We can't use these names for any variables.

32 reserved keywords are as below :-

auto , double , int , struct


break , else , long , switch
case , enum , register , typedef
char , extern , return , union
continue , for , signed , void
do , if , static , while
default , goto , sizeof , volatile
const , float , short , unsigned

Comments - Lines that are not part of the program.


Comments are written so that another programmer reading the program can understand
it that what is written here. Basically it is written to help someone who don't
know what is written in it.

Single Line Comment - //...............


Multiple Line Comment - /*
This is the format for multiple line
code.
*/

To print output in next line :-

int main() {
printf("Hello World");
printf("Hello Java\n");

/n is compulsary for print in next line

* line under double quote is known as string.

To print no. in the string according to variable

int main() {
int age = 22;
printf("age is age");
return 0;
}

Cases to print output :-

1. Integers
printf("age is %d\n",variable);
for ex.- printf("age is %d\n",age);

2. Real Numbers
printf("value of pi is %f\n",variable);
for ex.- printf("value of pi is %f \n",pi);
3. Characters
printf("star looks like this %c \n",variable);
for ex.- printf("star looks likes this %c \n",star);

* In C programming these 3 are known as format specifiers.

Input - scanf("%d",&variable); here &variable is the address

for ex.- int main () {


int age;
printf("enter age");
scanf("%d", &age);
printf("age is : %d", age);
return 0;
}

Compilation - A computer program that translates C code into machine code.

Hello.c --------> C Compiler -------> a.exe (Windows)

Q.1 - Write a program to calculate area of a square ?

# include<stdio.h>
//area of square
int main() {
int side;
printf("enter side");
scanf("%d", &side);

printf("area is : %d", side*side);


return 0;
}

Q.2 - Write a program to calculate area of circle ?

# include<stdio.h>
//area of circle
int main() {
float radius;
printf("enter radius");
scanf("%f", &radius);

printf("area is : %f", 3.14*radius*radius);


return 0;
}

You might also like