0% found this document useful (0 votes)
45 views7 pages

Assignment 2 C Language

The document discusses various topics related to variables, data types, and operators in C programming language. It defines tokens and keywords as the basic building blocks of a programming language. It explains the different data types in C including primary, derived, user-defined, and empty data types. It also describes the difference between signed and unsigned modifiers. The document then discusses variables, constants, and operators while providing examples of declaring and using them in C code. It concludes by explaining common functions like getchar(), putchar() etc and comparing operators, operands, and different types of operators.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
45 views7 pages

Assignment 2 C Language

The document discusses various topics related to variables, data types, and operators in C programming language. It defines tokens and keywords as the basic building blocks of a programming language. It explains the different data types in C including primary, derived, user-defined, and empty data types. It also describes the difference between signed and unsigned modifiers. The document then discusses variables, constants, and operators while providing examples of declaring and using them in C code. It concludes by explaining common functions like getchar(), putchar() etc and comparing operators, operands, and different types of operators.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 7

Chap - 2 - Variable ,Datatype & Operators

1.    What is token and Keyword?


 Tokens are the smallest building blocks of any programming language.
 Variables,data types,constants,operator functions are example of tokens.
 Keywords are predefined identifier that are reserved and have special meaning for
language.
 Keywords used for their intended purpose and cant be used as name for variable
 There are total 32 keywords such as int,char,float,break,default etc.

2.    What is  Data type and explain their types?


 Data types are used to define the type of data that is to be stored.
 There are four data types-
1. Primary data types(int,char,float)
2. Derived data types(array,pointer)
3. User define data types(structure,unions)
4. Empty data type(void)

3.    What  is the difference between unsigned and signed modifier?


 Unsigned modifier is having only positive number whereas signed modifier having
both positive and negative numbers.

4.   What is variable and explain rules how to declare variable? 


 Variable reserved loction in memory that has name,,has associated type,hold the data
which is to be stored.
Rules
 First character must be either alphabet or underscore.
 Not start with digit.
 No commas & blank space are allowed.
 No special symbol other than “_”.

5.    What is Constant and how to declare Constant in C?


 A constant is an entity whose value does not change throughout the program.
 We have more than one constant in a program.
 Constant use keyword const.

6.    what is use of sizeof() operator in C?


 Used to compute the size of oprand.
 Returns the size of a variable.
 It can be applied to any data types.
 When sizeof() used with data types it simply return the amount of memory allocated
to that data type.
7.  Explain Following functions -1.GetChar  2.  putChar   3.  Getch  4. Putch   5. Getche .
1.GetChar -
 This function is used to Accept one character , from keyboard.
 It takes Only One Character as an input.
 Syntax-character_var = getchar();
2.  putChar –
 This function is used to print one character on the screen, from keyboard.
 Syntax-
character_var = getchar(); //accept character
putchar(character_var); // display the accepted character
3.  Getch  –
 This function is used to Accept one character , from keyboard ,without echo to the
screen
 Without echo means when we type non screen it is not visible.
 This function provides functionality of not getting echo to the screen
 This Function takes One Character as an input
 Many programmer use this Function at the end of the program to stop screen until a
Character is given as an input.(but its work is not to stop the screen,it Wait's for the
character )
 Syntax-character_var = getch();

4. Putch  -
 This function is used to print one character ,on the screen
 This Function also print One Character as an input
 Syntax-putch(variable_name);
5.  Getche  –
 This function is used to Accept one character , from keyboard ,echo to the screen
 This Function also takes One Character as an input
 Syntax-character_var = getche();

8. what is the difference  between operator and operands?


 An operator is the ‘function’ that performs the operation, whereas the operand is the
input to that function.
 In the expression 3 + 4 = 7, the operator is ‘+’ since it’s telling us how to perform the
operation and the operands are 3 and 4 the inputs upon which the operation is acting.

9. What is the difference between unary and binary and ternary operator?
 Unary- works on one operand.
 Binary- works on two operands.
 Ternary- works on three operands.
10. What is operator in C and what are the difference types of operator in C?
 Operator is symbol which operates on a value or a variable.
 Operator allow to perform diff kinds of operations on operands.
 Different types of operator is as follows-
1. Arithmetic operator
2. Increment decrement operator
3. Assignment operator
4. Relational operator
5. Logical operator
6. Conditional operator
7. Bitwise operator
8. Special operator

11. What is the difference between single equal  "=" and double equal "=="operators in
C?
 Single equal “=” is assignment operator used to assign value to operator
 Double equal “==” is relational operator used to compare two variable or
constants

12. What is the difference between pre decrement operator and  post decrement operator?
 Pre decrement operator is used to decrement variable value by 1 before assigning
the value to the variable. Post decrement operator is used to decrement variable
value by 1 after assigning the value to the variable.

13. How much bytes of space does integer  , float data type take  in memory?
 integer - 4 bytes 
 character - 1 byte
 float - 4 bytes
 double - 8 bytes
 long double - 16 bytes
Lab: 2
1. Write a C program to convert specified days into years, weeks and days. Note: Ignore leap
year. Test Data :     Number of days : 1329    Expected Output :    Years: 3   Weeks: 33  Days:
3
#include <stdio.h>
int main()
{
int days,weeks,years;
printf("Number of days: ");
scanf("%d",&days);
years = days/365;
weeks = (days % 365)/7;
days = days- ((years*365) + (weeks*7));
printf("Years: %d\n", years);
printf("Weeks: %d\n", weeks);
printf("Days: %d \n", days);
return 0;
}

2. W.A.P using Arithmetic operators e.g x*=y, x/=y,x+=y,x-=y,x%=y;


#include <stdio.h>
int main()
{
int x = 20;
int y = 10;
x*=y;
printf("multiplication is %d\n", x );
x+=y;
printf("addition is %d\n", x );
x-=y;
printf("substraction is %d\n", x );
x%=y;
printf("modulus is %d\n", x );
x/=y;
printf("dividation is %d\n", x );
}

3. W.A.P  to check entered year is leap year or not.


#include<stdio.h>
int main()
{
int yr;
printf ("Enter a year: ");
scanf ("%d", &yr);
if (yr%4 == 0 && yr%100 == 0 && yr%400 == 0)
{
printf("It is LEAP YEAR.");
}
else if (yr%4==0 && yr%100!=0)
{
printf("It is LEAP YEAR.");
}
else
{
printf ("It is NOT LEAP YEAR.");
}
return 0;
}

4. Declare a 5 variables name it as maths,biology,physics,chemistry,history take the marks


from user and calculate total and average of marks.
#include <stdio.h>
int main()
{
int maths, biology, physics, chemistry, history;
float Total, Average, Percentage;
printf("Please Enter the marks of five subjects:maths biology physics chemistry history\
n");
scanf("%d%d%d%d%d", &maths, &biology, &physics, &chemistry, &history);
Total = maths + biology + physics + chemistry + history;
Average = Total / 5;
Percentage = (Total / 500) * 100;

printf("Total Marks = %.2f\n", Total);


printf("Average Marks = %.2f\n", Average);
printf("Marks Percentage = %.2f", Percentage);
return 0;
}

5. Accept principal amount, rate of interest, and duration from the user. Display Interest
Amount and Total Amount (Principal + Interest).

#include <stdio.h>
int main()
{
float principle, rate, interest,Total_Amount;
int time;

printf("Enter Principle Amount, Rate %% per Annum and Time\n");


scanf ("%f %f %d", &principle, &rate, &time);

interest = (principle * rate * time)/ 100.0;


Total_Amount = (principle + interest);
printf ("Principle Amount = %5.2f\n", principle);
printf ("Rate %% per Annum = %5.2f%\n", rate);
printf ("Time = %d years\n", time);
printf ("Simple Interest = %5.2f\n", interest);
printf ("Total amountt = %5.2f\n", Total_Amount);
return 0;
}

You might also like