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

XII-computer notes Chapter 4

Chapter 4 covers fundamental concepts in C programming, including identifiers, keywords, data types, constants, and variables. It explains the rules for naming variables, the declaration and initialization of variables, and the structure and operations of arrays. Additionally, it highlights the case sensitivity of the C language, providing examples to illustrate these concepts.

Uploaded by

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

XII-computer notes Chapter 4

Chapter 4 covers fundamental concepts in C programming, including identifiers, keywords, data types, constants, and variables. It explains the rules for naming variables, the declaration and initialization of variables, and the structure and operations of arrays. Additionally, it highlights the case sensitivity of the C language, providing examples to illustrate these concepts.

Uploaded by

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

Chapter 4 (C FUNDAMENTALS)

Q1. Define Identifiers and keywords?


 Identifiers are names given to various program elements, such as variables, functions and
arrays. Following rules must be observed while constructing identifiers.
 An identifier must start with a letter or and underscore (_).
 An identifier can contain letters, digits or underscore.
 An identifier cannot contain blanks, commas or any other special characters.
 An identifier cannot consist of more than 31 characters.
 An identifier must conform to case sensitivity ( C differentiates between uppercase and
lowercase alphabetical characters.) for example, the identifier MAX is not the same as
the identifier max.
 The valid variable name (x, y1,sum_1, TABLE, table, tax_rate _auto, _temperature)
 Not valid variable name triangle are (blank space), tax-rate (special character -), “x”
(quotation ), est#west (hash), 5th (illegal variable), -temperature ( underscore _)
 Reserved words can not be used(auto, break, char)

Q2. Determine which of the following are valid or invalid identifiers.


a) Array1 b) stax c) name and address d) include e) $twenty
f) 5-number g) y/s h) integer i) f_name j) Getch k) smallest

Q3. Can reserved words in C-language be chosen as variable names?


Ans. In C, we have 32 keywords, which have their predefined meaning and cannot be used as a
variable name. These words are also known as “reserved words”. It is good practice to avoid
using these keywords as variable name.

Q4. Identify valid variables or invalid variables of the following


 number b) you&me c) case
number:- It is valid identifier
you&me:-It is not valid identifier (An identifier cannot contain blanks, commas or any other
special characters.)
case:- It is not valid identifier(Reserved words can not be used)
Q5. What is data type in C language?
Ans. Data type:-Each variable in “C” has an associated data type. It specifies the type of data
that the variable can store like integer, character, floating, double, etc. Each data type requires
different amounts of memory and has some specific operations which can be performed over it.
Integer Data type The integer data type is a set of whole numbers. Every integer value does not
have the decimal value. We use the keyword "int" to represent integer data type in c. The integer
data type is used with different type modifiers like short, long, signed and unsigned. The
following table provides complete details about the integer data type.
Data Type Range Bytes width Format specifier
Short signed int -32768 to +32767 2 %d
Short unsigned int 0 to 65535 2 %u
Long signed int -2147483648 to 2147483647 4 %ld
Long unsigned int 0 o 4294967295 4 %lu
Float Data type:-Float data types are a set of numbers with the decimal value. Every floating-
point value must contain the decimal value. We use the keyword "float" to represent floating-
point data type and "double" to represent double data type in C. Both float and double are
similar but they differ in the number of decimal places. The float value contains 6 decimal places
whereas double value contains 15 or 19 decimal places. The following table provides complete
details about floating-point data types.
Data Type Range Bytes width Format specifier
Float 3.4e -38 to 3.4e + 38 4 %f
Double 1.7e-308 to 1.7e+308 8 %lf
Long double 3.4e-4932 to 11e+4932 10 %Lf
Character data type:-The character data type is a set of characters enclosed in single
quotations. The following table provides complete details about the character data type.
Data Type Range Bytes width Format specifier
Signed char -128 to 127 1 %c
Unsigned char 0 to 255 1 %c

Q6. Define the format specifiers used in any formatted function


Ans. Format specifiers :-The format specifiers are used in C for input and output purposes.
Using this concept the compiler can understand that what type of data is in a variable during
taking input using the scanf() function and printing using printf() function. Here is a list of
format specifiers with printf() or accepting input with scanf().
The Most Commonly Used Format Specifiers in C
SPECIFIER USED FOR
%c a single character
%s a string
%hi short (signed)
%hu short (unsigned)
%Lf long double
SPECIFIER USED FOR
%n prints nothing
%d a decimal integer (assumes base 10)
%i a decimal integer (detects the base automatically)
%o an octal (base 8) integer
%x a hexadecimal (base 16) integer
%p an address (or pointer)
%f a floating point number for floats
%u int unsigned decimal
%e a floating point number in scientific notation
%E a floating point number in scientific notation
%% the % symbol
%d (Decimal Integer) Format Specifier
#include <stdio.h>
int main()
{
int a=50;
printf("The integer value of a is %d \n",a);
return 0;
}
Output:

%c (Character) Format Specifier


#include <stdio.h>
int main()
{
char s;
printf("Enter the character \n");
scanf("%c",&s);
printf("The character is: %c",s);
return 0;
}
Output:
%f (Floating Point) Format Specifier
#include <stdio.h>
int main()
{
float a=3;
printf("The floating point of a is %f \n",a);
return 0;
}
Output:

%e (Floating Pointer Number) Format Specifier


#include <stdio.h>
int main()
{
float a=12.5;
printf("The floating-point of a is %e \n",a);
return 0;
}
Output:

%s (String) Format Specifier


#include <stdio.h>
int main()
{
char s[15]="simplilearn";
printf("The string value of s is %s \n",s);
return 0;
}
Output:

%lf (Double) Format Specifier


#include <stdio.h>
int main()
{
double d=12.5;
printf("The double value of d is %lf \n",d);
return 0;
}
Output:

%o (octal integer) Format Specifier


#include <stdio.h>
int main()
{
int oct=11;
printf("The octal integer value of oct is %o \n",oct);
return 0;
}
Output:

%x (Hexadecimal Integer) Format Specifier


#include <stdio.h>
int main()
{
int h=14;
printf("The hexadecimal value of h is %x \n",h);
return 0;
}
Output:

%p (Prints Memory Address) Format Specifier


To find the memory address that holds values of a variable, we use the %p format specifier, and
it prints in hexadecimal form.
#include <stdio.h>
int main()
{
int sum=0;
printf("The memory address of sum is %p \n",&sum);
return 0;
}
output:

Q7. What is constant and variable? Also define its types.


Ans. CONSTANT:-Constants are the variables whose values cannot be changed throughout the
execution of the program once they are initialized at the beginning of the program. A constant is
an identifier whose value remains unchanged through out the program. A constant is a value that
cannot be altered by the program during execution, i.e., the value is constant. When associated
with an identifier, a constant is said to be "named.
TYPES OF CONSTANT:
Constants are used in two ways. They are:
1. Literal Constants
2. Defined Constants
LITERAL CONSTANT: Literal constants are data used for representing fixed values. 'They can
be used directly in the codes.
Example:
1, 2, 5, "c", "good" etc.
DEFINED OR SYMBOLIC CONSTANT:I n C, we can create symbolic constant whose value
remains unchanged but used as a variable. A symbolic constant can be created using the #define
preprocessor directive or const keyword.
Example:
const int LIGHT_SPEED = 299792458;
#define LIGHT SPEED 29972458
VARIABLE:A variable is nothing but a name given to a storage area that our programs can
manipulate. Its value can change during program execution. Each variable C has a specified data
type, which determine the size and layout of the variable's memory.

Q8. Define rules of naming variable?


RULES FOR NAMING VARIABLE: The general rules for constructing names for variables
(unique identifiers) are:
1. A variable name contains alphabets (letters), number, and underscores.
2. A variable name must start with a letter or an underscore (_).
3. Variable names are case sensitive. (myVar and myvar or Sum end sum are different variables)
4. Variable names cannot contain whitespaces or special characters like !, #, % etc.
5. Reserved words (Like C++ keyword, such as int cannot be used as names.
6. A variable name cannot be longer than 32 characters in C++ by default.

Q9: Define Declaring (Creating) and initializing Variables.


Ans. DECLARATION (CREATING) VARIABLE: Variable declaration is a process in which
we create storage space for variable in memory. A variable declaration consists of data type and
name of the variable written as follow:
data_typevariable_name,
int sum
INITIALIZATION: Assign initial value to a variable is known as variable initialization. It can
be initialized during declaration or separately. The equal sign is used to assign value written as
follows:
data_typevariable_name value;
int sum = 3;

Q.10: Define array data structure and its operations.


Ans. ARRAY: The array is the type of linear data structure that stores homogeneous (same type)
elements at memory locations which are consecutive. The same types of objects are stored
sequentially in an array. The main idea of an array is that multiple data of the same type can be
stored together. Before storing the data in an array, the size of the array has to be defined. The
location of elements stored in an array has a numerical value called index to identify the element.
Some operations of array are:

Traverse: Go through the elements and print them.


Search: Search for an element in the array by its value or its index.
Update: Update the value of an existing element at a given index.
Insert: Insert an element in array.
Deletion: Delete an element from array.
Sorting: Rearrange a given array or list of elements.

Declaring Arrays To declare an array in C, a programmer specifies the type of the elements and
the number of elements required by an array as follows −
type arrayName [ arraySize ];
This is called a single-dimensional array. The arraySize must be an integer constant greater than
zero and type can be any valid C data type. For example, to declare a 10-element array
called balance of type double, use this statement −
double balance[10];
Here balance is a variable array which is sufficient to hold up to 10 double numbers.
Initializing Arrays :-We can initialize an array in C either one by one or using a single statement
as follows −
double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};
The number of values between braces { } cannot be larger than the number of elements that we
declare for the array between square brackets [ ].
Accessing Array Elements:-An element is accessed by indexing the array name. This is done by
placing the index of the element within square brackets after the name of the array. For example

double salary = balance[9];
The above statement will take the 10th element from the array and assign the value to salary
variable. The following example Shows how to use all the three above mentioned concepts viz.
declaration, assignment, and accessing arrays −
Live Demo
#include <stdio.h>
int main () {
int n[ 10 ]; /* n is an array of 10 integers */
int i,j;
/* initialize elements of array n to 0 */
for ( i = 0; i< 10; i++ ) {
n[i ] = i + 100; /* set element at location i to i + 100 */
}
/* output each array element's value */
for (j = 0; j < 10; j++ ) {
printf("Element[%d] = %d\n", j, n[j] );
}
return 0;
}
When the above code is compiled and executed, it produces the following result −
Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109

Q11. Why is C is called case sensitive language? Give any example


Ans. C is a case-sensitive programming language. The uppercase letters and lowercase letters in
the C language are considered to be different. C is case-sensitive for the identifiers. This means
that if we define a variable in lowercase, we cannot refer to it in uppercase. C language treats
them as different variables. C is also case-sensitive for the function names. This means that if we
define a function in lowercase, we cannot refer to or invoke the function in uppercase. We can
provide two different implementations for the lowercase and uppercase functions
Code:
#include<stdio.h>
int main()
{
Printf("Hello World"); \\Here, the p in printf is in uppercase
return 0;
}
Output:
main.c: In function ‘main’:
main.c:5:5: warning: implicit declaration of function ‘Printf’; did you mean ‘printf’? [-
Wimplicit-function-declaration]
5 | Printf("Hello World");
| ^~~~~~
| printf
/usr/bin/ld: /tmp/ccVL48i9.o: in function `main':
main.c:(.text+0x15): undefined reference to `Printf'
collect2: error: ld returned 1 exit status

#include
/* Function Prototypes */
void foo();
void FOO();
int main()
{
// declare variables
int a = 7;
int A = 9;
/* C is case-sensitive programming
Language. It considers lowercase letters
and uppercase letters to be different.
*/
printf("Lower case a = %d \n",a);
printf("UPPER case A = %d \n",A);
foo(); // invoke foo()
FOO(); // invoke FOO()
return 0;
} // end main
// dummy function foo (lower case)
void foo(){
printf("Lower case function foo()\n");
}
// dummy function FOO (UPPER case)
void FOO(){
printf("UPPER case function FOO()\n");
}

Notice that we have provided two different implementations to the functions foo() and FOO(). C
language treats both functions to be different.

You might also like