The document discusses various topics related to C programming language including data types in C, statements in C, the role of the preprocessor, ASCII character set, calculating volume and area of a sphere, reading and displaying student details, separating integral and fractional parts of a number, converting temperature between Fahrenheit and Celsius scales, operator precedence and associativity, and unary operators.
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
C Data Types and Variables
1. 1. Discuss the basic data types in C.
C language is rich in data types. Data types refer to an
extensive system used for declaring variables or functions of
different types. The type of a variable determines how much
space it occupies in storage and how the bit pattern stored is
interpreted.
There are 4 classes of data types:
1. Primary data types (int, float, char)
2. User defined data types (enumerator, typedef)
3. Derived data types (array, function, pointers, structure,
union)
4. Empty data sets. (void)
2. 2.What is a statement in C? Discuss the different types of
statements in C, giving suitable examples of each.
A statement is a command given to the computer that instructs
the computer to take a specific action, such as display to the
screen, or collect input. A computer program is made up of a
series of statements.
Labeled Statements
A statement can be preceded by a label. Three types of labels
exist in C.
A simple identifier followed by a colon (:) is a label. Usually, this
label is the target of a goto statement.
Within switch statements, case and default labeled
statements exist.
A statement of the form
case constant-expression : statement
Compound Statements
A compound statement is the way C groups multiple
statements into a single statement. It consists of multiple
statements and declarations within braces (i.e. { and }). The
body of a function is also a compound statement by rule.
Expression Statements
An expression statement consists of an optional expression
followed by a semicolon (;). If the expression is present, the
statement may have a value. If no expression is present, the
statement is often called the null statement.
3. The printf function calls are expressions, so statements such as
printf ("Hello World!n"); are expression statements.
Selection Statements
Three types of selection statements exist in C:
if ( expression ) statement
In this type of if-statement, the sub-statement will only be
executed iff the expression is non-zero.
if ( expression ) statement else statement
Iteration Statements
C has three kinds of iteration statements. The first is a while-
statement with the form
while ( expression ) statement
The substatement of a while runs repeatedly as long as the
control expression evaluates to non-zero at the beginning of each
iteration. If the control expression evaluates to zero the first time
through, the substatement may not run at all.
The second is a do-while statement of the form
do statement while ( expression ) ;
This is similar to a while loop, except that the controlling
expression is evaluated at the end of the loop instead of the
beginning and consequently the sub-statement must execute at
least once.
The third type of iteration statement is the for-statement. In ANSI
C 1989, it has the form
4. for ( expressionopt ; expressionopt ; expressionopt ) statement
In more recent versions of the C standard, a declaration can
substitute for the first expression. The opt subscript indicates that
the expression is optional.
Jump Statements
C has four types of jump statements. The first, the goto
statement, is used sparingly and has the form
goto identifier ;
This statement transfers control flow to the statement labeled with
the given identifier. The statement must be within the same
function as the goto.
The second, the break statement, with the form
break ;
is used within iteration statements and switch statements to pass
control flow to the statement following the while, do-while, for, or
switch.
The third, the continue statement, with the form
continue ;
is used within the substatement of iteration statements to transfer
control flow to the place just before the end of the substatement.
In for statements the iteration expression (e3 above) will then be
executed before the controlling expression (e2 above) is
evaluated.
The fourth type of jump statement is the return statement with the
form
5. return expressionopt ;
This statement returns from the function. If the function return
type is void, the function may not return a value; otherwise, the
expression represents the value to be returned.
3. What is the role of the pre-processor in C? Explain the use
of pre-processor directives like #include and #define with
suitable examples.
he C preprocessor is the macro preprocessor for the C, Objective-
C and C++ computer programming languages. The preprocessor
provides the ability for the inclusion of header files, macro
expansions, conditional compilation, and line control.
In many C implementations, it is a separate program invoked by
the compiler as the first part of translation.
The language of preprocessor directives is only weakly related to
the grammar of C, and so is sometimes used to process other
kinds of text files.
1. #define (Macros)
A macro is a code that is replaced by some value of the code of
macro. Any macro is mostly described and defined by its #define
directive.
Syntax:
#define token value
There are two types of macros:
Function – like macros
Object – like macros
6. Function – like macros
The function like-macro works almost like a function call.
For example:
#define MAX(a,b) ((a)>(b) ? (a): (b))
For example:
#define MAX(a,b) ((a)>(b) ? (a): (b))
MAX here is the Macro name
2 #include
There is some other functionality for the include preprocessor
directive. It has its own three variants which replace the code with
the current source files code.
Three variants are as follows:
#include<file>
#include”file”
Include anything else
#include<file>
Searches for a file in the defined list of the system or directories
as specified then searches for a standard list of system libraries.
#include”file”
This type is used for your own customized header files of the
program. A search is made for a file named file first in the current
directory followed by system header files and current directories
of directory current file.
#include anything
7. 4. Describe the ASCII Character Set, and discuss it’s
importance in modern computing systems
All the character sets used in the C language have their
equivalent ASCII value. The ASCII value stands for American
Standard Code for Information Interchange value. It consists of
less than 256 characters, and we can represent these in 8 bits or
even less. But we use a special type for accommodating and
representing the larger sets of characters. These are called the
wide-character type or wchat_t.
However, a majority of the ANSI-compatible compilers in C accept
these ASCII characters for both the character sets- the source
and the execution. Every ASCII character will correspond to a
specific numeric value.
The character sets help in defining the valid characters that we
can use in the source program or can interpret during the running
of the program. For the source text, we have the source character
set, while we have the execution character set that we use during
the execution of any program.
But we have various types of character sets. For instance, one of
the character sets follows the basis of the ASCII character
definitions, while the other set consists of various kanji characters
(Japanese).
The type of character set we use will have no impact on the
compiler- but we must know that every character has different,
unique values. The C language treats every character with
different integer values.
8. 5. Write a C program to calculate and display the volume (V)
and area (A) of a sphere using the formulae: Area A = 4*PI*R2
and Volume = AR/3, where R is the radius.
/*
* C Program to Find Volume and Area of Sphere
*/
#include <stdio.h>
#include <math.h>
#define PI 3.14
int main()
{
float R;
float A, volume;
printf("Enter radius of the sphere : n");
scanf("%f", &R);
A = 4 * PI * R * R;
volume = A * R * 1/3;
printf("area of sphere is: %f", A);
printf("n Volume of sphere is : %f", volume);
return 0;
}
9. 6. Write a program to read your name, roll number and CGPA
and displays them to the screen.
#include <stdio.h>
struct student {
char name[50];
int roll;
float CGPA;
} s;
int main() {
printf("Enter information:n");
printf("Enter name: ");
fgets(s.name, sizeof(s.name), stdin);
printf("Enter roll number: ");
scanf("%d", &s.roll);
printf("Enter CGPA: ");
scanf("%f", &s.CGPA);
printf("Displaying Information:n");
printf("Name: ");
printf("%s", s.name);
printf("Roll number: %dn", s.roll);
10. printf("CGPA : %.1fn", s.CGPA);
return 0;
}
7. Write a C program that accepts a real number and prints
the integral and fractional parts on the screen.
#include <stdio.h>
int main()
{
float num;
printf("enter a number :") ;
scanf("%f",&num);
int num_integer = (int)num;
float num_decimal = num - num_integer;
printf("integer part: %d , fractional part : %fn",num_integer ,
num_decimal);
return 0 ;
}
8. Write a C program to convert a temperature reading in
degrees Fahrenheit to degrees Celsius, using the formula: C
= (5/9)x(F-32).
11. #include<stdio.h>
int main()
{ float F, Celsius;
printf("Enter a temp in F: ");
scanf("%f", &F);
Celsius = ((F-32)*5)/9;
printf("nn Temperature in Celsius is : %f",Celsius);
return (0);
}
9. What is meant by operator precedence and associativity?
Build a table showing the associativity and relative
precedence of various types of operators supported in C.
Operator precedence determines the grouping of terms in an
expression and decides how an expression is evaluated. Certain
operators have higher precedence than others; for example, the
multiplication operator has a higher precedence than the addition
operator.
Operators Associativity is used when two operators of same
precedence appear in an expression. Associativity can be either
Left to Right or Right to Left
12. .
10. What is a unary operator? Discuss the purpose of any
two unary operators supported by the C language. Also
explain with a suitable code sample, the use of the
conditional operator (?:).
13. These are the type of operators that act upon just a single
operand for producing a new value. All the unary operators have
equal precedence, and their associativity is from right to left.
When we combine the unary operator with an operand, we get the
unary expression.
Types of Unary Operator in C
There are following types of unary operators found in the C
language:
unary minus (-)
unary plus (+)
decrement (- -)
increment (++)
NOT (!)
sizeof ()
Address of operator (&)
Unary Minus
This operator changes the sign of any given argument. It means
that a positive number will become negative, and a negative
number will become positive.
But the unary minus is different from that of the subtractor
operator. It is because we require two operands for subtraction.
int p = 20;
int q = -p; // q = -20
14. Unary Plus
This operator changes the sign of any negative argument. It
means that a negative number will become positive, and a
positive number will also become positive.
But the unary minus is different from that of the subtractor
operator. It is because we require two operands for subtraction.
int p = -20;
int q = +p; // q = 20