Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void



Similar documents
Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Lecture 2 Notes: Flow of Control

6. Control Structures

Informatica e Sistemi in Tempo Reale

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Object Oriented Software Design

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Sources: On the Web: Slides will be available on:

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

Fundamentals of Programming

C PROGRAMMING FOR MATHEMATICAL COMPUTING

3/13/2012. Writing Simple C Programs. ESc101: Decision making using if-else and switch statements. Writing Simple C Programs

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

arrays C Programming Language - Arrays

Two-way selection. Branching and Looping

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Object Oriented Software Design

Selection Statements

C++FA 5.1 PRACTICE MID-TERM EXAM

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

Chapter 3 Operators and Control Flow

Example of a Java program

C Programming Tutorial

Tutorial on C Language Programming

Pemrograman Dasar. Basic Elements Of Java

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

JavaScript: Control Statements I

C++ Programming Language

Conditions & Boolean Expressions

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Chapter 2: Elements of Java

13 Classes & Objects with Constructors/Destructors

Chapter One Introduction to Programming

Stacks. Linear data structures

C++ Language Tutorial

TIP: To access the WinRunner help system at any time, press the F1 key.

Statements and Control Flow

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

C++ INTERVIEW QUESTIONS

J a v a Quiz (Unit 3, Test 0 Practice)

Moving from CS 61A Scheme to CS 61B Java

Computer Programming Tutorial

MISRA-C:2012 Standards Model Summary for C / C++

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Chapter 2 Introduction to Java programming

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

The C Programming Language course syllabus associate level

While Loop. 6. Iteration

Chapter 8 Selection 8-1

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Boolean Data Outline

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

1.00/ Session 2 Fall Basic Java Data Types, Control Structures. Java Data Types. 8 primitive or built-in data types

Objective-C Tutorial

VB.NET Programming Fundamentals

I PUC - Computer Science. Practical s Syllabus. Contents

Introduction to Java

4.8 Thedo while Repetition Statement Thedo while repetition statement

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Why using ATmega16? University of Wollongong Australia. 7.1 Overview of ATmega16. Overview of ATmega16

Keil C51 Cross Compiler

Java Interview Questions and Answers

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Lecture 1 Introduction to Java

5 Arrays and Pointers

The if Statement and Practice Problems

Common Beginner C++ Programming Mistakes

In this Chapter you ll learn:

Java Basics: Data Types, Variables, and Loops

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Writing Control Structures

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

Moving from C++ to VBA

A single register, called the accumulator, stores the. operand before the operation, and stores the result. Add y # add y from memory to the acc

C++ program structure Variable Declarations

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

C++ Essentials. Sharam Hekmat PragSoft Corporation

Conditional Statements Summer 2010 Margaret Reid-Miller

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 6 Program Control

El Dorado Union High School District Educational Services

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Conditionals (with solutions)

Answers to Review Questions Chapter 7

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

AP Computer Science Java Subset

1 Description of The Simpletron

So far we have considered only numeric processing, i.e. processing of numeric data represented

Transcription:

1. Explain C tokens Tokens are basic building blocks of a C program. A token is the smallest element of a C program that is meaningful to the compiler. The C compiler recognizes the following kinds of tokens: keywords : these are reserved identifiers having predefined meanings identifiers : these tokens name functions, variables, constants, and types literals : these tokens that specify values operators : these tokens are used to combine values in expressions punctuation : these tokens separate or terminate complex constructions 2. Explain C Keywords Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : auto double int struct break Else long switch case enum register typedef char extern return union const float short continue default for Goto signed sizeof volatile Do If static while unsigned void All 32 keywords are in lower case letters. These keywords are reserved and cannot be redefined. Following rules must be kept in mind when using keywords. Keywords are case sensitive. The keywords have special meaning in the language and cannot be used for any other purpose such as constant name or variable name. 3. State the use of %d and %f. Write a print statement in C using above mentioned symbols %d and %f are conversion specifiers. They are used with the control string in i n p u t f u n c t i o n s c a n f ( ) a n d o u t p u t f u n c t i o n p r i n t f ( ) Consider the following statement printf ( radius=%d, area= %f, r,a) ; The control-string uses %d conversion specifier. It describes how the value of n will be printed. The following example shows use of %d. In this example %d is used in scanf()

statement to accept an integer value. Again %d is used in the printf() statement to print the integer value. int a; printf ( Please enter an integer ); scanf( %d,&a) printf ( you entered the value %d,a); The next example shows use of %f. In this example %f is used in scanf() statement to accept a float value. Again %f is used in the printf() statement to print the float value. int b; printf ( Please enter a float number ); scanf( %f,&b) printf ( you entered the value %f,b); 4. Define expressions Expressions: Expression is a single value or a combination of values and variables which are connected by operators. Following are examples of expression: a + b - c ; x = a ; x > 6 ; The first expression uses arithmetic operators + and, the second expression uses the assignment operator =, and the third expression uses relational operator >. Expression can also be a logical condition that is or false. In C the and false condition are represented by integer value 1 and 0 respectively. 5. What are operators? Operators: Operators are tokens used to combine values in an expression. The following are examples of operators used in an expression: a + b - c ;

x = a ; x > 6 ; The first expression uses arithmetic operators + and, the second expression uses the assignment operator =, and the third expression uses relational operator >. Some operators perform operation on two operands, while others perform operation on only one operand. Explain the various operators used in C Explain the bit wise operators in C Explain the logical operators used in C What is an operator? Explain unary and binary operators. Explain the increment and decrement operators. State four arithmetic operators and four logical operators of C The operators used in C are divided into following types: Arithmetic Operators Relation Operator Logical Operator Arithmetic Operators: The operators used for basic arithmetic are =, +, -, *, and /. Operator Meaning subtraction + addition * multiplication / division % modulus The operator % is used in integer arithmetic. It is called modulus operator. It gives the remainder when the integer to its left is divided by the integer to its right. Example: 13 % 5 is read as "13 modulo 5" and it has the value 3 The modulus operation gives remainder of an integer division. Hence % cannot be used in type float or double. Unary Operators: These operators perform an operation on a single variable and give a new value. The unary operators are: Operator Meaning Unary negative + Unary positive ++ Unary increment Unary decrement sizeof size of variable in

A B Unary Minus: This operator is used to negate a numerical constant, variable or a expression. Examples: 35 2.5 (a * b) a + 35 Increment operator: The operator ++ will increase the value of its operand by 1.. Decrement operator: The operator will decrease the value of its operand by 1. Sizeof operator: This operator will return the size of the operand. The operand can be an expression. Example: int p=10; float q = 2.5 ; printf ( the size of p in byte is %d, sizeof(p)); printf ( the size of q in byte is %d, sizeof(q)); The output of this will be the size of p in byte is 2 the size of q in byte is 4 Bitwise Boolean Operators: The six bit wise operators used in C are given below. The bit wise operators work bit by bit on operands. The operands must be of integral type. & AND OR ^ XOR (exclusive OR) ~ NOT ( changes 1 to 0 and 0 to << 1) Shift left >> Shift right Logical Operators: The logical operators used in C are given below. && logical if any one operand is false, the result is false logical OR if any one operand is, the result is! NOT if the operand is, the result is false and vice versa

false false false false The operators && and are binary operators i.e. the logical operators require two operands. The! operator is a unary operator. false Consider operands A and B. The truth table for && and operators are given below A B A && B false false false false false false false These logical operators are used to combine or negate expression containing relational operators. Here's an example of a logical expression. r = ((a&&b) (c>d)); In the example: r is set equal to 1 if a and b are nonzero, or if c is greater than d. In all other cases, r is set to 0. Define/Explain Data Types Every variables used in a C program is defined with a specific type. In Standard C there are four basic data types. They are int, char, float, and double. char - characters int - integers (whole numbers) float - real numbers double - higher precision real numbers In addition, C provides structured types: array - groups of variables of identical types, accessed using integer indices structs - groups of variables of mixed types, accessed by using named field selectors unions - variables that can contain values of different types, depending on a field selector C also allows enumerated types - variables that can take on a small number of different named values Explain the break statement The break statement is used in loop statements (for, while, and do-while). It is possible to force an immediate exit from a loop by using the break statement. A break statement is formed with the keyword break followed by a semicolon.

Example : #include <stdio.h> int x; for(x=1;x<=100;x++) if(x==10) printf( %d, x); printf ( loop terminated ); When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop. Break is also used in switch statements to terminate execution of statement. When the break statement is executed, execution proceeds to the statement that follows switch statement. Example : int n; printf( \nenter a number between 1 to 3 ); scanf( %d,%n); switch (n) case 1 : printf( you entered 1 ); case 2 : printf( you entered 2 ); case 3 : printf( you entered 3 ); default : printf( wrong choice ); Explain the continue statement It is possible to bypass the loops normal control structure and force an early iteration of a loop and. This is accomplished by using continue. The continue statement is used in loop statements (for, while, and do-while) to terminate iteration of the loop. The next iteration of loop to takes place, skipping code between itself and conditional expression that controls

the loop. A continue statement is formed with the keyword continue followed by a semicolon. Example: The following program prints even numbers between 0 and 100 #include <stdio.h> int x; for(x=0;x<=100;x++) if(x%2!=0) continue; printf( %d,x); After a continue statement is executed, execution proceeds to increment clause and continuation test of for loop. Hence only even numbers are printed because an odd one will cause the loop to iterate early. The continue statement can be used in while and do while loops. It will cause control to go directly to the conditional expression and then continue the looping process. The continue statement is useful when data in body of loop is bad, out of bounds or unexpected. Instead of acting on bad data, control can go back to top of loop and get another data value. Give the selection process of the switch statement Explain switch case statement with syntax The switch statement is used to select and execute a particular group of statement from several available groups. The selection is based on the value of an expression or variable The syntax of the switch statement is: switch (expression or variable) case constant1 : statement-block1; case constant2 : statement-block2;... case constantn : statement-blockn; case default : statement-block3; where expression is of simple type such as int, char, or enum. It cannot have float or double type.

When switch statement is executed, the expression is evaluated. The resulting value is compared to values of constants 1 through N in order until a matching value is found. If a match is found in constant i then statements-block i through N and the default statements will be executed. Normally, last statement in each statement-block is a break statement so that only one statement-block is executed. The default clause is optional. If it is present then the default-statements are executed whenever value of expression does not match the constant values. Example: The program defines variable v of char type. The program takes input and stores it in variable v. The program will check the value of v and match it with the several cases. If the case matches the corresponding statement is executed. char v; printf( \nenter a vowel : ); scanf( %d,%c); switch (v) case a : printf( you entered a ); case e : printf( you entered e ); case i : printf( you entered i ); case o : printf( you entered o ); case u : printf( you entered u ); default : printf( you did not enter a vowel ) Example : In this example, the switch statement will allows execution of different statements depending on the value of n entered by the user. int n; printf( \nenter a number between 1 to 3 ); scanf( %d,%n); switch (n) case 1 : printf( you entered 1 );

case 2 : printf( you entered 2 ); case 3 : printf( you entered 3 ); default : printf( wrong choice ) Explain the for-statement. Give the syntax of for-statement The for statement is a looping construction. It has the following form: for (initialization; condition; increment) statements where initialization is a statement that is executed once at beginning of for loop. condition is an expression that can be (nonzero) or false (zero). This expression is tested prior to each iteration of the loop. The loop terminates when it is false. increment is a statement that is executed after statements. statements is a sequence of statements. If there is only one statement then the braces may be omitted. Example: The following program will print the numbers 1 to 10 in reverse order int i ; for (i=10 ; i>=0 ; i--) printf( %d\n,i); Example: The following program will print the sum of numbers from 1 to 10 int i,sum = 0 ; for (i=1 ; i<=10 ; i++) sum=sum + i ; printf( sum = %d,sum); Example: The following program will print the sum of all even numbers from 1 to 100

int i,sum = 0 ; for (i=2 ; i<=100 ; i=i+2) sum=sum + i ; printf( sum of even numbers = %d,sum); Example: The following program will print the first 10 odd numbers int i ; for (i=1 ; i<=20 ; i=i+2) printf( %d\n,i); Explain the syntax of while statement The while statement is a looping construction which has the following form: while (loop condition) statement block; where: loop condition is an expression that can be (nonzero) or false (zero). This expression is tested prior to each iteration of the loop. The loop terminates when it is false. When the condition is false, control will go to the first statement after the curly bracket statements is a sequence of statements. If there is only one statement then the braces can be omitted. Example: The following program will print the first 10 natural numbers. The loop is executed 10 times. int i=1 ; while (i<=10) printf( %d\n,i); i++; Explain : Do-While Loops

The do-while statement is a looping construction. It has following form:

do statement block ; while (loop condition); where statements is a sequence of statements. If there is only one statement then the braces may be omitted. condition is an expression that can be (nonzero) or false (zero). This expression is tested after each iteration of the loop. The loop terminates when it is false. Example: The following program will print the first 10 natural numbers. The loop is executed 10 times. int i=1 ; do printf( %d\n,i); i++; while (i<=10) Give the syntax of the simple if- statement The simplest form of if statement is as follows: if (condition) statement; The if keyword must be followed by a set of parentheses containing the expression to be tested. The parentheses is followed by a single statement which is executed only if the test expression evaluates to. Example: The following program uses the simple if statement: int n; printf( Enter a positive number less than 10 ); scanf( %d,&n); if (n<=10) printf("thank you ); if (n>10) printf("sorry. The number is greater than 10 );

Describe the if- statement

The if statement has one of two forms: if (condition) -statements or if (condition) -statements false-statements where condition is an expression that can be (nonzero) or false (zero). -statements and false-statements are sequences of statements. If there is only one statement in a sequence then the surrounding braces may be omitted. The second form includes the clause For both forms, -statements are executed only if condition is. For second form the false-statements are executed if condition is false. Example : The following statement is used to test whether a number entered by the user is positive or negative #include <stdio.h> void main () int n ; printf ("Enter a non-zero number") ; scanf ("%d", &n); if (n < 0) printf ("number is negative") ; printf ("The number is positive") ; Example: Finding the greater of two numbers #include <stdio.h> void main () int a,b ;

printf ("Enter a number") ; scanf ("%d", &a); printf ("Enter another number") ; scanf ("%d", &b); if (a>b) printf ("%d is greater than %d", a,b) ; printf ("%d is greater than %d", a,b) ; State two types of if statement The simplest form of if statement is as follows: If one statement at this point, there is no need to use curly braces. if (condition) statement; If m o re than one statement, they may be grouped using curly braces. Such statement group is called compound statement. if (condition) Statement1; Statement2; The general form of the if statement includes the clause is as follows. if (condition) statement; statement; if (condition) statement; statement; Statement; statement; Example: The following example uses if statement to find the greatest of three numbers entered by the user. #include <stdio.h> main () int a, b, c, big ; printf ("Enter three numbers"); scanf ("%d %d %d", &a, &b, &c);

if (a > b) && (a > c) big = a ; if (b > c) big = b ; big = c ; printf ("largest number is %d", big) Explain the nesting of if-statement The if statement can itself contain another if statement. This is known as nesting of if statement. The nested if may appear in a program as : if(condition 1) if(condition 2) statement 1; statement 2; if(condition 3) statement 3; statement 4; The main thing to remember about nested if is that the statement always refers to the nearest if statement within the same block. Example : This program uses the nested if to find greatest of three numbers #include <stdio.h> main () int a, b, c, big ; printf ("Enter three numbers"); scanf ("%d %d %d", &a, &b, &c); if (a > b) if (a > c) big = a ;

big = c; if (b > c) big = b ; big = c ; printf ("largest of %d, %d & %d = %d", a, b, c, big) ; Explain the -if ladder with example The construct of -if ladder is shown below : if(condition1) statement1 ; if(condition1) statement1 ; if(condition2) statement2 ;... if(conditionn) statementn ; statement ; The conditional expressions are evaluated from top downward. When a condition is found, the associated statement is executed, and rest of ladder is bypassed. If none of conditions is, then final statement is executed. The final acts as a default condition i.e. if all other conditions tests fail, then the last statement is performed. If there is no final and all other conditions are false then no action will take place. Example : Following example illustrates the of if--if ladder #include <stdio.h> main () int n; printf("enter an integer between 1 and 5"); scanf( %d,n);

if(n==1) printf("number is one\n"); if(n==2) printf("number is two\n"); if(n==3) printf("number is three\n"); if(n==4) printf("number is four\n"); if(n==5) printf("number is five\n"); printf("you didn't follow the rules"); Explain the? : operator (Conditional Operator) C language uses a combination of? and : for making two way decisions. This operator is called the conditional operator and it takes three operands. The general form of the conditional operator is : conditional expression? expression1 : expression2 The conditional expression is first evaluated. If result is non-zero, then expression1 is evaluated and its value is returned. If result is zero, then expression2 is evaluated and its value is returned. Example : Consider the following code : if (x>3) a = 5; a = 1; The same can be written as : a = (x>3)? 5 : 1 ; The conditional operator can be nested for more complex assignments. For example consider the following Salary = 4R+20 for R < 20 = 150 for R = 20 = 5R+90 for R > 20 This can be written as: salary = (R=20)? 150:((R<20)? (4*R+20):(5*R+90)) The code is concise, but more difficult to read. Hence it is better to use if statements when nesting of conditional operator is required. if (R=20) salary = 150 ; if R < 20 salary = 4*R+20 ; salary = 5*R+90 ;