UNIT - 2 C' Programming Basics Part - A (2mark Questions)
This document provides an overview of basic C programming concepts. It defines key terms like variables, data types, and control structures. It also includes examples of common C programming tasks like checking if a number is even or odd, using the sizeof operator, and finding the biggest of two numbers. The document is intended to teach basic C syntax and operations to new programmers.
UNIT - 2 C' Programming Basics Part - A (2mark Questions)
This document provides an overview of basic C programming concepts. It defines key terms like variables, data types, and control structures. It also includes examples of common C programming tasks like checking if a number is even or odd, using the sizeof operator, and finding the biggest of two numbers. The document is intended to teach basic C syntax and operations to new programmers.
1. What are the advantages and disadvantages of A variable is an identifier for memory C language? location Advantages:- Data can be stored in it and taken when Suitable for creating application software and needed. system software 9. What is user-defined datatype? What are its types? Easy, simple and fast. User can define a new kind of datatype to declare It can be used for general purpose variables. It is called as user-defined datatype Powerful language Types: Typedef, Enumerated Flexible and portable 10. What is the difference between local variable and Disadvantages:- global variable? LOCAL VARIABLE GLOBAL VARIABLE It is a weakly typed language Defined at starting of a Declared before the main It needs extra memory space block function It is not object oriented language It can be used only within It can be used anywhere in 2. What are the characteristics of C language? that block the program It has many in-built functions ex:- ex:- Pointers can be used int a=10; void main() It is a structured language void main() { Small and easy to understand { int i=10; Efficient programming language printf(“%d”,i); printf(“%d”,i); It can be used in many types of computers } } 3. Give the structure of a C program o/p:- 10 o/p:- 10 Documentation section Preprocessor section 11. What ate the data types available in C? Definition section In-built data type (int, char, float, …) Global declaration section User defined data type( typedef, enum,struct) Main function() Derived data type (array, pointer, function) { No data type (void) Declaration part; 12. What are the basic (or) fundamental datatypes? Executable statements; Int – to store numbers without decimal point } Float – to store numbers with decimal point User defined function() Char – to store a single character { Double – To store a big decimal point number Executable statements; 13. Write any 6 escape sequences in C. } \n – move to next line 4. What are the steps to execute a C program? \t – move one space horizontally Create a C program \v- move in space vertically Compile the program (Alt+F9) \f – move to next page Link that program to library \r – move to starting of that line Execute the program(Ctrl+F9) \b – backspace 5. What is a token? What are the types of Tokens? 14. What is an operator? What are its types? The smallest individual unit of a program An operator is a symbol that tells computer to is called as Token perform certain task. Identifiers, keywords, constants, strings, Types:- operators Arithmetic operators (+, -, *, /, % ) 6. Mention some keywords in C language Relational operators ( <, >, <=, >=, ==, != ) If, else, while, do, for, continue, break, include, Logical operator ( &&, ||, !) main, int, float, char, void Assignment operator ( +=, -=, *=, /=, %=, = ) 7. What is a constant? Increment/decrement operator (i++, ++i, i--, --i) A constant is a fixed value that cannot be changed Conditional operator ( ? : ) in the C program execution. Bitwise operator ( &, |, ^, <<, >>, ~) Special operator ( sizeof() )
“Love what you Do, Do what you Love” – Steve jobs
15. Write differences between operator and operands. 20. What is a ternary operator? OPERAND OPERATOR It is also called as conditional operator It contains Data values It is a symbol for It is also called as question-colon operator operation It checks the condition. Operation is performed on Operation is performed by If condition is true, first part will be executed operands operators If condition is false, second part is executed Ex:- Ex:- Ex:- C = a + b; C = a + b; C = (a < b) ? a : b Here, a, b, c = operands Here, +, = are operators If (a < b) is true, then c = a 16. What are formatted and unformatted I/O if (a > b) is false, then c = b functions? 21. Write a program to check a number is odd/even Formatted I/O functions #include<stdio.h> They follow certain format #include<conio.h> O/p:- % symbol is used in input and output statements void main() Enter a number Ex:- { 311 Input – scanf(“%d”,&n); It is odd number clrscr(); Output – printf(“%d”, n); int n; Unformatted I/O functions printf(“enter a number”); They do not have any format scanf(“%d”,&n); % symbol is not used. if ( n % 2 = = 0 ) Ex:- printf(“it is even number”); Input – getc(), gets(), getch(), getchar() else Output – putc(), puts(), putch(), putchar() printf(“it is odd number”); 17. What are the types of control structures in C? getch(); If statement } If…else statement 22. Write a program using sizeof() operator If..else ladder #include<stdio.h> Switch statement O/p:- #include<conio.h> 2 Goto statement void main() 1 18. What is the difference between break and continue { 4 statement? clrscr(); 8 BREAK CONTINUE To stop the current To stop the current printf(“%d”, sizeof(int)); iteration and get out of iteration and continue for printf(“%d”, sizeof(char)); that block next iteration printf(“%d”, sizeof(float)); Break; keyword is used Continue; keyword used printf(“%d”, sizeof(double)); It is used in switch case, It is used in for, while getch(); for, while, do } 23. Write a program to find biggest among two number Statements after the Statements after #include<stdio.h> BREAK statement will not CONTINUE statement will O/p:- #include<conio.h> be executed not be executed in current void main() iteration { A is big 19. What is the difference between while and do- clrscr(); while? int a=10, b=20; WHILE DO..WHILE if(a>b) printf(“a is big”); Entry check loop Exit check loop else printf(“b is big”); Condition is checked at Condition is checked at getch(); entry point exit point } Loop will not run if Loop will run ONE TIME if 24. Write a Program to print the numbers from 10 to 1 using for loop #include<stdio.h> O/p:- condition is false the condition is false #include<conio.h> 10 Ex:- Ex:- void main() 9 8 While (condition) Do { 7 { { int i; 6 for(i=10; i>0; i--) 5 Body of the loop Body of the loop 4 printf(“%d”,i); } } while ( condition); 3 getch(); 2 } 1
“Love what you Do, Do what you Love” – Steve jobs
PART – B ( 8 and 16 mark questions) 2. Explain different types of data types in C. 1. Explain the structure of a C program In-built data type (int, char, float, …) Documentation section User defined data type( typedef, enum, struct) Preprocessor section Derived data type (array, pointer, function) Definition section No data type (void) Global declaration section In-built data type:- Main function() They are the created already within C compiler { If we want, we can use them directly Declaration part; Keyword int, float, char, double is used. Executable statements; Int – integer ( to store numbers) } Float – to store decimal point number User defined function() Char – character ( to store a single character) { Double – to store a big decimal point number Executable statements; User-defined data type:- } User is going to create a new data type Documentation section:- Cannot be used directly like in-built data types We can add any comments, description Typedef – to redefine the name of data type It will not be executed Typedef int a; Ex:- Enum – enumerated data type /* program to add two numbers*/ Enum days{1=”sun”, 2=”mon”, 3=”tue”, 4=”wed”, Preprocessor section:- 5=”thu”, 6=”fri”, 7=”sat”} Pre-processor statements are used Struct – structure to hold different members To link the program to library struct mech Ex:- { #include<stdio.h> int roll; Definition section:- char name[20]; We can define all constants here. float marks; These values will not be changed in the program } Ex:- Derived data type:- #define a 10 These are the data types that are derived from the Global declaration section:- already existing data type. We can declare global variables that can be used Ex:- Arrays, pointer, functions anywhere in the program Int Array[10]; to store 10 integers one-by-one It is outside of all the other functions Pointer int * a; to store address of other Ex:- variable Int a=10; Functions() to do a part of work Void main() No data type:- { Void ….. Void is a data type that is not actually a data type ….. It does not contain any values } It is also called as empty data type Main function() It is must for all the programs 3. Explain arithmetic and relational operators in C. A program cannot run without main() Arithmetic operators:- Main – keyword is used Operators that do arithmetic operations such as Program starts to run from main() only addition, subtraction, multiplication and division are Declaration part:- called as arithmetic operators We can declare values to the variables Operator Name Operation Ex: a=10; * Asterisk Multiplication Executable section:- / Slash Division Execute the statements in this section + Plus Addition C=a+b; printf(“%d”, c); - Minus subtraction User defined function:- % Percentage modulus User can write the functions if needed.
“Love what you Do, Do what you Love” – Steve jobs
Program for arithmetic operator:- 4. Explain logical and assignment operator in C #include<stdio.h> Logical operator:- #include<conio.h> It is used to do logical operations such as logical void main() AND, OR and NOT { It is also binary operators because it takes two clrscr(); operands. int a,b; It returns 1 if TRUE, 0 if FALSE printf(“enter values for a and b”); Operator Name Return value scanf(“%d%d”,&a,&b); o/p:- && Logical AND 1 or 0 printf(“answer is”); Enter values || Logical OR 1 or 0 printf(“%d”, a+b); for a and b ! Logical NOT 1 or 0 printf(“%d”, a-b); 4 2 o/p:- printf(“%d”, a*b); Program for logical operator:- Enter values for a,b,c Answer is printf(“%d”, a/b); 6 #include<stdio.h> 2 printf(“%d”, a%b); 2 #include<conio.h> 4 getch(); 8 6 void main() 2 Answer is } { 1 0 clrscr(); 1 Relational operators:- int a,b; 13 Operator Name Return value printf(“enter values for a, b and c”); < Less than 1 or 0 scanf(“%d%d%d”,&a,&b,&c); > Greater than 1 or 0 printf(“answer is”); <= Less than or equal to 1 or 0 printf(“%d”,( a < b ) && ( a < c ) ); >= Greater than or equal to 1 or 0 printf(“%d”,( a < b ) || ( a < c ) ); == Equality 1 or 0 printf(“%d”,(!a)); != Not equal to 1 or 0 getch(); } These operators are binary operators because they want two operands to operate. Assignment operators:- They return 1 if TRUE, 0 if FALSE They are used to assign values to the variables Program for relational operator:- Values can be assigned after the expression #include<stdio.h> Shorthand assignment operators is also there. #include<conio.h> Operator Name Meaning assign value void main() += Short-hand a+=b Add a + b and { add a=a+b store in a clrscr(); -= Short-hand a - =b sub a - b and int a,b; sub a=a–b store in a printf(“enter values for a and b”); *= Short-hand a*=b multiply a * b scanf(“%d%d”,&a,&b); o/p:- multiply a=a*b and store in a printf(“answer is”); Enter values for a /= Short-hand a/=b divide a / b and printf(“%d”, a<b); and b divide a=a/b store in a 4 printf(“%d”, a>b); == Equality 1 or 0 2 printf(“%d”, a<=b); Answer is != Not equal 1 or 0 printf(“%d”, a>=b); 0 to printf(“%d”, a==b); 1 printf(“%d”, a!=b); 0 1 getch(); 0 } 1
“Love what you Do, Do what you Love” – Steve jobs
5. Explain the increment and decrement operators 6. Explain the formatted and unformatted I/O statements and conditional operators in C. I/O Functions Increment operator:- Pre-increment operator (+ + i ) Post-increment operator ( i + +) Unformatted statements Decrement operator:- Formatted statements INPUT OUTPUT Pre-decrement operator (- - i ) INPUT OUTPUT getch() putch() Post-decrement operator ( i - -) scanf() printf() getchar() putchar() Pre-increment operator: increment first, and assign value gets() puts() Post-increment operator: assign value first and then increment Pre-decrement operator: decrement first, then assign value Formatted statements:- Post-decrement operator: assign value first, then decrement. They can read all types of values Example:- But the format should be specified #include<stdio.h> Printf() to print any value to the o/p #include<conio.h> Scanf() to get any value from the i/p void main() O/p:- Format specifiers for printf() and scanf():- { printf() scanf() clrscr(); 10 %c print a character %c Read a character int a=10; 12 %d print a decimal integer %d Read a decimal integer printf(“%d”,a++); %e print floating point %e Read floating point printf(“%d”,++a); 12 exponent exponent printf(“%d”,a - -); %f print a floating point %f Read a floating point printf(“%d”,- - a); 10 %h print a short int %h Read a short int getch(); %I print a %I Read a } decimal/hexadecimal/octal decimal/hexadecimal/octal In the above example, value of a = 10 %o print an octal integer %o Read an octal integer printf(“%d”,a++); in this line, the value of a is printed %s print a string %s Read a string first, and then it is incremented. (a = 10 + 1 = 11) %u print an unsigned decimal %u Read an unsigned integer decimal integer printf(“%d”,++a); in this line, the value of a is incremented first, and then it is printed. (a = 11 + 1 = 12) Example Program:- printf(“%d”,a - -); in this line, the value of a is printed #include<stdio.h> first, and then it is decremented. (a = 12 - 1 = 11) #include<conio.h> O/p:- printf(“%d”,- - a ); in this line, the value of a is void main() Enter value for x and y 10 20 Printf = 25 decremented first, and then it is printed. (a = 11 - 1 = 10) { Scanf = 2 Conditional operator:- clrscr(); It is also called as question-colon operator int a,b,x,y; It is also called as ternary operator a=printf("Enter value for x and y \n"); It contains question mark and colon. b=scanf("%d%d",&x,&y); It contains 1 condition and two executable statements printf("printf = %d",a); If condition is TRUE, statement1 will be executed printf("scanf = %d",b); If condition is FALSE, statement2 will be executed getch(); Syntax:- } Condition ? statement 1 : statement 2 ; Program:- Unformatted statement:- #include<stdio.h> No format is followed here #include<conio.h> Any kind of data will be processed here void main() getch() To get one character or number or anything { getchar() To get one variable clrscr(); gets() To get a string int a=10,b=20; getc() To get one character a>b ? printf(“a is bigger”) : printf(“b is bigger”); getch(); putch() To print one character or number or anything } putchar() To print one variable o/p:- puts() To print a string b is bigger putc() To print one character
“Love what you Do, Do what you Love” – Steve jobs
Program:- O/p:- 8. Explain switch-case statement and perform #include<stdio.h> Enter a string arithmetic operations using it. #include<conio.h> MECH It is used to take decision from a number of choices void main() Answer = MECH One switch statement should be there, many cases { can be there clrscr(); Inside every case, break statement should be there char a[10]; All cases should be unique (not same) puts(“Enter a character”); Syntax:- gets(a); o Switch ( variable) puts(“Answer=”); { puts(a); Case 1: statement 1; break; getch(); Case 2: statement 2; break; } Case n: statement n; break; Program:- } #include<stdio.h> O/p:- Example program:- #include<conio.h> Enter a character B #include<stdio.h> void main() B Answer = #include<conio.h> { void main() B clrscr(); { char a; clrscr(); printf(“Enter a character”); int a, b, choice; a = getchar(); a=10, b=20; printf(“Answer=”); printf(“enter 1 for addition”); putchar(c); printf(“enter 2 for subtraction”); getch(); printf(“enter 3 for multiplication”); } printf(“enter 4 for division”); printf(“enter your choice”); 7. Write a C program to check a number is Armstrong scanf(“%d”,&choice); or not. switch(choice) { #include<stdio.h> case 1: printf(“%d”,a + b); break; #include<conio.h> case 2: printf(“%d”,a - b); break; void main() case 3: printf(“%d”,a * b); break; { case 4: printf(“%d”,a / b); break; clrscr(); default: printf(“invalid choice”); int n,r,arm,sum; } printf(“enter the number”); getch(); scanf(“%d”,&n); } O/p:- O/p:- arm=n; enter 1 for addition sum=0; Enter the number enter 2 for subtraction while(n>0) 153 enter 3 for multiplication { enter 4 for division r=n%10; Armstrong number Enter your choice sum=sum+r*r*r; 1 n=n/10; 30 } Enter your choice If(arm==sum) printf(“Armstrong number”); 2 Else printf(“Not an Armstrong number”); -10 getch(); Enter your choice } 3 200 Enter your choice 4 0
“Love what you Do, Do what you Love” – Steve jobs
9. Write a C program to find the sum of the series 1+2+3+…….+N #include<stdio.h> #include<conio.h> O/p:- void main() { Enter the number clrscr(); int n,i,sum; 10 printf(“enter the number”); Answer = 55 scanf(“%d”,&n); for(i=0; i<=n; i++) { sum = sum + i; } printf(“answer=%d”,sum); getch(); } 10. Write a C program to print the reverse of a number #include<stdio.h> #include<conio.h> void main() { clrscr(); int n,r,rev; printf(“enter the number”); scanf(“%d”,&n); rev=0; O/p:- while(n>0) { Enter the number r=n%10; 123 rev=rev*10+r; n=n/10; Reverse = 321 } printf(“reverse=%d”,rev); getch(); } 11. Write a c program to check whether a number is palindrome or not #include<stdio.h> #include<conio.h> void main() { clrscr(); int n,r,rev,pal; printf(“enter the number”); scanf(“%d”,&n); rev=0; O/p:- while(n>0) { Enter the number r=n%10; 121 rev=rev*10+r; n=n/10; palindrome } If(pal==rev) printf(“palindrome”); Else printf(“Not a palindrome”); getch(); }