Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

1 Unit

Download as pdf or txt
Download as pdf or txt
You are on page 1of 117

WHY C?

named as “C”
• The C programming language is named after the letter "C" because it
was developed as a successor to the B programming language. Dennis
Ritchie created C at Bell Labs between 1972 and 1973 to develop
utilities for Unix

• Structured high level machine independent language


• C has evolved from BPCL, B AND ALGOL by Dennis Richie in 1972 at the Bell
laboratories
• Added data types
• Made along with UNIX OS system it was also developed in Bell laboratories
• language became powerful after the publishing of the book The C
programming language by Brian Kernighan and Dennis Ritchie in 1978
• To remain standard in 1983 American National Standards Institute(ANSI)
appointed a technical committee
• 1989 committee approved a version of C which is now known as ANSI C (C89)
• Although C++ and Java have evolved from C, the standardizing committee
thought of adding few features of C++ and Java to C.
• After standardizing C in 1999. The version is now said C99.
• Importance of C:
• Robust language ; language has rich set of built-in functions and operators
• C Compiler combines capabilities of assembler with feature of high level languages therefore it is suited for system
software and business package
• Efficient and fast due to variety of data types and powerful operators
• 32 keywords
• Portable
• C can extend itself ; we can continuously add our own functions in library
Features
• Memory management : The memory allocation can be done either before or at the time of program
implementation. There are two techniques for memory allocation: static memory allocation and dynamic memory
allocation.
• Portability : C language is lavishly portable as programs that are written in C language can run and compile on any system
with either no or small changes.
• Recursion: In C language any function can call other function. A function can call itself . When a function calls itself that
type of function is called as recursive function.
• Platform independent: .As we know that C language is a portable language there it is also platform independent.
APPLICATIONS MADE USING C
APACHE
LINUX OS
UBUNTU
RED HAT
MYSQL DATABASE
RUBY
PERL
JAVA
MOST PARTS OF WINDOWS
NUMPY SKYPY LIBRARIES OF PYTHON
INSTALLING IDE & COMPILER
• An IDE (Integrated Development Environment) is software that combines commonly used
developer tools into a compact GUI (graphical user interface) application. It is a combination of tools
like a code editor, code compiler, and code debugger with an integrated terminal.
• IDE is a software suite that provides basic tools to developers to write and test softwares
• IDE has other features through which we can store code online ,format the code and optimize the
code online.
Compilers are used to convert the source code into machine-readable code so that the
computer can understand it. For every programming language, we have to set up a
compiler.

C programming in VS code
Then c++ - GCc for windows or Mingw for windows
Run – next- finish
My computer- C program files- mingw64- open the folder- mingw32- bin- copy path- then
my computer properties- advance system setting- Environmental variables- path-edit-
new- paste the copied path- ok- ok- close
Open vs code IDE
Add new folder
Under that folder add new file main.c
Add extensions
C C++ extension for windows intellisense
Intellisense- used for suggestion code improvement
PROGRAM 1:
#include<stdio.h>
main()
{ Output: I see, I remember
/*printing begins….*/
Printf( “I see , I remember”);
/*printing ends……*/
}
Main() – It is a special function used by the C program to tell the computer where the program starts.
Every program must have exactly one main function. If we use more than one main() function , then the
compiler cannot understand which one marks the start of the program
( ) parentheses- The empty parentheses tells us that the main function has no arguments
{ , }- tells us the start and end of the function and also the end of the program. All the statements in
between the brackets forms the body of the function.
• Printf - Its is a predefined function .predefined means that it is the
function which is already defined and compiled and linked with our
program
• ; - Every statements in C should end with semicolon;

%d - for printing integers


FORMAT SPECIFIER: %f - for printing floating-point numbers
The format specifier in C is used to tell %c - for printing characters
the compiler about the type of data %s - for printing strings
to be printed or scanned in input and %p - for printing memory addresses
output operations. They always start %x - for printing hexadecimal values
with a % symbol and are used in the
formatted string in functions like Hexadecimal numbers are number with base 16
printf(), scanf. (hexa stands 6 and deci stands 10)
C Prepocessor

Program.c

Program.i

Compile

Program.obj

Program.exe
C library functions
• C Standard library functions or simply C Library functions are inbuilt functions in C
programming.
• The prototype and data definitions of these functions are present in their respective header
files. To use these functions we need to include the header file in our program

<string.h> String handling functions


<time.h> Date time functions
<math.h> Mathematics functions

<stdio.h> Standard Input/Output functions


• Include Directive:
• #include is a way of including a standard or user-defined file in the program
and is mostly written at the beginning of any C program. The #include
preprocessor directive is read by the preprocessor and instructs it to insert
the contents of a user-defined or system header file in our C program. These
files are mainly imported from outside header files.

• For example: #include<stdio.h>


# include<math.h>
# include<string.h>
• C does make a distinction between uppercase and lowercase
• Printf and PRINTF is not same .
• C has everything written in lowercase
• String “I SEE , I REMEMBER” can be in uppercase.
The main function
Main is a part of every program.
There can be different types to write main:
main() – empty pair of parentheses mean that the function has no
argument. Or it can also be written as void inside the parameters
Int main() – defines that that the function returns a zero value to the
operating system. When int is specifies the last line of the program should
be zero.
void main() – defines there is no parameter inside main
main(void)
void main (void)
• For Example:
Output- I see, I remember
printf(“I see,\n”);
printf(“ I remember”);
Output- I see
I remember
Printf(“I see,\n I remember”);
Output- I see
I remember
Program : adding two nos.
/*program ADDITION /* comment line
/*written by ABC /* comment line
main()
{
int number;
float amount;
number=100;
amount=30.75+75.35;
printf(“%d\n ”,number);
printf(“%5.2f”, amount);
}
#define
#define is a preprocessor directive it is not a statement.
It should not end with a semicolon;
Symbolic constants are generally written in uppercase
Defined directives are usually placed before main() function
Symbolic constants are not declared in declaration sections
• We cannot use the ‘=’ operator to assign value to the macros (eg. #define PI
3.14).
• We do not use the semicolon ‘;’ at the end of the statement in #define.
#include <stdio.h>

#define PI 3.14159265359
int main()
{

int radius = 21;


int area;

// Using macros to calculate area of circle


area = PI * radius * radius;

printf("Area of Circle of radius %d: %d", radius, area);

return 0;
}
Basic structure of C program
Documentation Section
Line 1- comments line
Link Section( #include) Line2- it provides instructions to
Definition Section the compiler to link functions from
Global Declaration the system library
main() Function Section Line3- defines all symbolic
{
constants
Declaration part;
Execution part;
Line4-some variables which are
} used in more than 1 function;
global variables are declared
outside the main function.
Data types
• 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. The data type is a collection of data with values having fixed values, meaning

as well as its characteristics.


Continued…………..

Types Description

Primitive data types are the most basic data types that are used for
Primitive Data Types
representing simple values such as integers, float, characters, etc.

User Defined Data Types The user-defined data types are defined by the user himself.

The data types that are derived from the primitive or built-in datatypes
Derived Types
are referred to as Derived Data Types.
1. Typedef
The typedef is a keyword that is used to provide existing data types with a new name. The C
typedef keyword is used to redefine the name of already existing data types.
C typedef Syntax:
typedef existing_name alias_name;
#include <stdio.h>

// defining an alias using typedef typedef long long ll;

int main()
{
// using typedef name to declare variable
ll var = 20;
printf("%ld", var);

return 0;
}
1. Float and double

• Float and double are two primitive data types in C programming that are used to store decimal
values. They both store floating point numbers but they differ in the level of precision to which
they can store the values.
• float is used to store single-precision floating point numbers. It can store decimal values with
precision up to 6-7 decimal places.
• The size of the float is 4 bytes
• It can store values up to 7 decimal points without loss of precision.
• The format specifier for float is %f.
#include <stdio.h>
int main()
{
// Syntax of declaring and initializing the float variable
float myVariable = 789.123456f;
// printing floating point number
printf("Float value is %f", myVariable);
return 0;

• Output :Float value is 789.123474


The precision of decimal numbers is lost after the 7th digit due to the limited bits in float.
In these cases, a double datatype is recommended.
• Double is used to store double precision floating point values. It is the greater version of
float which can store real numbers with precision up to 15 decimal places.
• The size of the double is 8 bytes.
• It can store values up to 15 decimal points without loss of precision.
• The format specifier for double is %lf
Data Type with size
Format
Data Type Size (bytes)
Size Format Specifier
Data Type
(bytes) Specifier
short int 2 %hd
unsigned long
8 %llu
long int unsigned short int 2 %hu

signed char 1 %c unsigned int 4 %u

unsigned char 1 %c int 2 %d

float 4 %f long int 4 %ld

double 8 %lf unsigned long int 4 %lu

long double 16 %Lf long long int 8 %lld


Find The Error
keyword
Keywords are predefined or reserved words that have special meanings to the compiler. These are
part of the syntax and cannot be used as identifiers in the program. A list of keywords in C or
reserved words in the C programming language are mentioned below:

auto break case char const continue default do

double else enum extern float for goto if

int long register return short signed sizeof static

struct switch typedef union unsigned void volatile while


• No keywords can be used to make a identifier or a constant variable
• Identifier- words that can be used to call ;used to name a variable or a
function. No punctuation marks are allowed in identifier.
Constants
• The constants in C are the read-only variables whose values cannot be modified once they
are declared in the C program. The type of constant can be an integer constant, a floating
pointer constant, a string constant, or a character constant. In C language,
the const keyword is used to define the constants.
Types of Constants
A integer constant refers to sequence of digits. There are 3 different types of integers namely,
decimal ,octal , hexadecimal
• Decimal Integers- consist of set of digits, o through 9 preceded by optional sign of – or +. Valid Eg- 123 -
321 0 654321 +78
not valid – 15 750 20,000 $1700
Embedded spaces commas non digit characters are not allowed
Octal integers- consist of combinational of digits from 0 to 7 with leading 0 .Eg - 037 0 0435 0551
Variable
• It is the name given to a memory location
• Declaration- datatype variable name;
Eg-int a;
int a,b;
int a, b, c,d;
• Initialization- datatype variable name=value;
Eg- int a= 4;
int a;
a=4;
Rule to write a variable:

• A variable name can contain alphabets, digits, and underscore (-) only.
• The starting letter can not be a digit.
• White spaces cannot be used.
• The name should not be reserved keyword or special character.
• We can declare and assign value to a variable in two ways.
• Valid variable- int harry ; float harry 123; float _harry123;
• Invalid variable- harry , int 77harry , $harry , char long; char 123
Declaration of variables
1) It tells the compiler what the variable name is
2) It tell the compiler what type of data the variable will hold
Declaration should be done at the beginning of statement block before they are used in the program
Declaration statement should always end with a semicolon ;
Int number;
Int amount;
Char abc, cdf;
Storage classes
Variable- characterstics
data type
Scope
Lifetime
Storage class
Memory

Main()
{
Int a;
{
Int b;
{
Int c;
}
}
}
• A storage class defines scope , default initial value and lifetime of a variable.
Auto storage class This is the default storage class for all the
#include<stdio.h>
variables declared inside a function or a block.
void fun()
Hence, the keyword auto is rarely used while
{
writing programs in C language.
auto int a=10;
++a;
printf("%d",a);
} Auto variables can be only accessed within the
void main() block/function they have been declared and not
{ outside them (which defines their scope). Of
fun(); course, these can be accessed within nested
fun(); blocks within the parent block/function in which
fun(); the auto variable was declared.

} syntax- auto int variable_name=value;


Static class

• This storage class is used to declare static variables which are popularly
used while writing programs in C language. Static variables have the
property of preserving their value even after they are out of their scope!
Hence, static variables preserve the value of their last use in their scope. So
we can say that they are initialized only once and exist till the termination
of the program. Thus, no new memory is allocated because they are not re-
declared.
• Their scope is local to the function to which they were defined. Global
static variables can be accessed anywhere in the program. By default, they
are assigned the value 0 by the compiler.

Syntax- static int a=10;


#include<stdio.h>
Example of static void fun()
storage class {
static int a=10;
++a;

printf("%d\n",a);
}
void main()
{
fun();
fun();
fun();

}
External storage class
• They are same as global variable
• Scope- global to the program they are defined in
• Default initial value- 0
• Lifetime- Throughout the lifetime of the program
• A global variable can be changed by any function in the program
• Extern keyword- is used to inform our compiler that the given variable
is declared somewhere else
Register Storage

• scope – local to the function they are declared in


• Default initial value- garbage value
• Lifetime- till the end of the function block In which they are declared
• Register variable request the compiler to store the variable in the CPU
register instead of storing in the memory to have faster access.
Example of garbage value

#include<stdio.h>
#include<stdlib.h>
int main(){
int x;
printf("The value of uninitialized variable is %d\n",x);
int *u;
printf("The value of uninitialized Pointer is %d\n",u);

return 0;
}
• In this example, the variable x is declared but not initialized. When it
is used in the printf() statement, the value of x is unknown and might
be any value that is currently held in that memory address. This
number could be very tiny, very big, or even negative.
• You must initialize variables before using them in C to prevent
garbage values. You can accomplish this by giving the variable a
specific value when declared or using an assignment statement later
in the program.
.

Local Variable
The variables declared inside a block are
automatic or local variables. The local variables
exist only inside the block in which it is declared.

#include <stdio.h>
int main(void)
{ for (int i = 0; i < 5; ++i)
{ printf("C programming"); }
// Error: i is not declared at this point
printf("%d", i);
return 0; }
Global variable
#include <stdio.h>

int x = 5; // global variable


int main() {

int y = 10; // local variable


return 0;
}

The Declaration of a global variable is very similar to that of a local variable.


The only difference is that the global variable is declared outside any function
Assigning values to variables
Value = amount+ inrate*amount;
While(year<=PERIOD)
{
Year = year+1
}

Assignment of a variable :
variable_name= constant/ value;
Initial value=9; year=year+1
Final value=3; the new value of year is equal to old
value of year +1

Bcf=d;
But c permits multiple assignments in one line
Initial value=9; final value=3; abc=6;
What is SCANF Function??

• Scanf(“control string",& variable 1 , &varaible2);


• Ampersand&- variables name’s address
• Eg- scanf(“%d”,&number);
Example of scanf function
main()
{
Int number;
Printf(“enter the number\n”);
Scanf(“%d , &number);
If (number<100)
Printf( your number is less than 100);
Else
Printf(“your number is greater than 100\n”);
}
Constants
Valid examples –
#define STRENGTH 100
#define PASS_MARK 50
#define MAX 200
#define PI 3.14

INVALID-
#define X=2.5
# define MAX 10
#define N 25;
#define N 5, m 10
#Define ARRAY 11
• #define PRICES& 100
Operator

An operator is a symbol that operates on a value or a variable. For example: + is an operator to perform
addition.
C has a wide range of operators to perform various operations.

C Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc on
numerical values (constants and variables).
Operator Meaning of Operator

+ addition or unary plus


- subtraction or unary minus

* multiplication
/ division
remainder after division
%
(modulo division
C Increment and Decrement Operators
C programming has two operators increment ++ and decrement -- to change the value of an operand (constant or variable) by 1.
Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1.
These two operators are unary operators, meaning they only operate on a single operand.
Example 2: Increment and Decrement Operators
Post-increment m++
Pre increment ++m

Post increment
#include<stdio.h>
int main()
{

int a=5;
printf("%d",a++);
return 0;

}
Pre increment
#include<stdio.h>
int main()
{

int a=5;
printf("%d",++a);
return 0;

}
#include<stdio.h>
Int main()
{
Int m=10;
Int n, n1;
n=++m;
n1=m++;
n--;
--n1;
n-=n1;
printf(“%d”, n);
Return 0;
}
What is the difference between the following 2
#include <stdio.h> codes?
int main() #include <stdio.h> //Program 1
{
int main()
int a = 1, b = 1, c;
c = a++ + b;
{
printf("%d, %d", a, b); int d, a = 1, b = 2;
} d = a++ + ++b;
printf("%d %d %d", d, a, b);
a) a = 1, b = 1 }
b) a = 2, b = 1 #include <stdio.h> //Program 2
c) a = 1, b = 2 int main()
d) a = 2, b = 2 {
int d, a = 1, b = 2;
d = a++ +++b;
printf("%d %d %d", d, a, b);
}
#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
int i = 0; int i = 2;
int j = i++ + i; int j = ++i + i;
printf("%d\n", j); printf("%d\n", j);
} }

a) 6
a) 0 b) 5
b) 1 c) 4
c) 2 d) Compile time error
d) Compile time error
Different kinds of operators
main()
{
int a,b,c,d;
a=15;
b=10;
c= ++a-b;
printf(“a =%d b=%d c=%d\n”,a,b,c);
d=b++ +a;
Printf(“a=% b=%d, d=%d\n”, a,b,d);
printf(“a*=b= %d\n”,a*=b);
}
C Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common assignment operator is =

Operator Example Same as

= a=b a=b

+= a += b a = a+b

-= a -= b a = a-b

*= a *= b a = a*b

/= a /= b a = a/b

%= a %= b a = a%b
Example of assignment operator

#include<stdio.h>
#include<math.h>
#define N 100
#define A 2
int main()
{
int a;
a=A;
while(a<N)
{
printf("%d\n",a);
a*=a;

}
return 0;

}
Meaning of
Operator Example
C Relational Operators Operator

A relational operator checks the == Equal to


5 == 3 is
evaluated to 0
relationship between two
5 > 3 is
operands. If the relation is true, > Greater than
evaluated to 1
it returns 1; if the relation is 5 < 3 is
< Less than
false, it returns value 0. evaluated to 0

Relational operators are used != Not equal to


5 != 3 is
evaluated to 1
in decision making and loops.
Greater than or 5 >= 3 is
>=
equal to evaluated to 1

Less than or 5 <= 3 is


<=
equal to evaluated to 0
• C Logical Operators
• An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or
false. Logical operators are commonly used in decision making in C programming.

Operator Meaning Example

Logical AND. True only if all operands If c = 5 and d = 2 then, expression


&&
are true ((c==5) && (d>5)) equals to 0.

Logical OR. True only if either one If c = 5 and d = 2 then, expression


||
operand is true ((c==5) || (d>5)) equals to 1.

Logical NOT. True only if the operand If c = 5 then, expression !(c==5)


!
is 0 equals to 0.
The logical operators && and || are used when we want to test more than one
condition and make decisions
a>b&&x==10

The logical expression given above is true only if a>b is true and x==10 is true.
If either or both of them are false then the expression becomes false.
Same examples of the usage of logical expression are-
If(age>55 &&salary<1000)
If (number<0 ||number>100)
Precedence of relational and logical operators

Highest !
>>= <<=
== !=
&&
Lowest ||
Ternary operator

• A ternary operator pair “?:” is available in C to construct expression of the


form
exp1 ? exp 2: exp3
Where exp1 , exp 2, exp 3 are expressions
The operator ?: work as follows: exp1 is calculated if the expression is true then
the exp2 is evaluated and becomes the value of the expression. If the exp1 is
false then exp3 is evaluated and its value becomes the value of the expression
#inlude<stdio.h>
Int main()
{
Int x ,y , max;
Printf(“ enter the number x and y”);
Scanf(“%d %d”, &x, &y);
Max=x>y? x: y
Printf(“the larger of %d and %d is %d”, a,b,c);
Return 0;
}
Special operators
Comma operators like comma, sizeof pointer(&*)
Comma operator:
Can be used to link related expressions together. A comma operator is
evaluated from left to right.
Value=(x=10, y=9, x+y);
Size of Operator
The sizeof compile time operator and when used with an operand returns the
number of bytes the operand occupies . The operand may be a variable, a
constant or a data type qualifier.
Eg- m= sizeof(sum);
n= sizeof(long int);
K= sizeof(235L);
• The sizeof operator is used to determine the length of array and structures
when their sizes are not known to the programmer. It is also used to
allocate memory space dynamically to variables during execution of
program.
main()
{
float a,b,c,x,y,z ;
a=9;
b=12;
c=3;
x=a-b /3+c *2 -1 ;
y=a-b /(3+c)* (2-1) ;
z=a-(b/(3+c) *2)-1 ;
}
Type Conversion
• Type conversion in C is the process of converting one data type to another. The type conversion is
only performed to those data types where conversion is possible. Type conversion is performed by a
compiler. In type conversion, the destination data type can’t be smaller than the source data type.
Type conversion is done at compile time and it is also called widening conversion because the
destination data type can’t be smaller than the source data type.

#include<stdio.h>
int main()
{
char a= '6';
int b=7;
int result =a+b;
printf("the sum is : %d",result);
return 0;
}
There are two types of Conversion:
1.Implicit Type Conversion
Also known as ‘automatic type conversion’.
A. Done by the compiler on its own, without any external trigger from the user.
B. Generally takes place when in an expression more than one data type is present. In such conditions
type conversion (type promotion) takes place to avoid loss of data.
C. All the data types of the variables are upgraded to the data type of the variable with the largest data
type.
bool -> char -> int -> long -> float -> double -> long double
/ An example of implicit conversion
#include <stdio.h>
int main() x = 107, z = 108.000000
{
int x = 10; // integer x
char y = 'a'; // character c

// y implicitly converted to int. ASCII


// value of 'a' is 97
x = x + y;

// x is implicitly converted to float


float z = x + 1.0;

printf("x = %d, z = %f", x, z);


return 0;
}
Explicit conversion
• This process is also called type casting and it is user-defined. Here the user can typecast the result to make it
of a particular data type. The syntax in C Programming:
// C program to demonstrate explicit type casting
#include<stdio.h>

int main()
{
double x = 1.2;

// Explicit conversion from double to int


int sum = (int)x + 1;

printf("sum = %d", sum);

return 0;
}
#include <stdio.h>

int main() {
float a = 1.5;
int b = (int)a; Output a = 1.500000 b = 1

printf("a = %f\n", a);


printf("b = %d\n", b);

return 0;
}
Advantages of Type Conversion

• Type safety: Type conversions can be used to ensure that data is being stored and
processed in the correct data type, avoiding potential type mismatches and type
errors.
• Improved code readability: By explicitly converting data between different types,
you can make the intent of your code clearer and easier to understand.
• Improved performance: In some cases, type conversions can be used to optimize
the performance of your code by converting data to a more efficient data type for
processing.
• Improved compatibility: Type conversions can be used to convert data between
different types that are not compatible, allowing you to write code that is
compatible with a wider range of APIs and libraries.
• Improved data manipulation: Type conversions can be used to manipulate data in
various ways, such as converting an integer to a string, converting a string to an
integer, or converting a floating-point number to an integer.
• Improved data storage: Type conversions can be used to store data in a more
compact form, such as converting a large integer value to a smaller integer type, or
converting a large floating-point value to a smaller floating-point type.
ASCII
• ASCII stands for the "American Standard Code for Information Interchange“
• t was designed in the early 60's, as a standard character set for computers and
electronic devices.
• ASCII is a 7-bit character set containing 128 characters.
• It contains the numbers from 0-9, the upper and lower case English letters from A
to Z, and some special characters.
A 65 a 97
B 66 b 98
C 67 c 99
D 68 d 100
E 69 e 101
F 70 f 102
G 71 g 103

H 72 h 104

I 73 i 105
j 106
J 74
k 107
K 75
l 108
L 76
m 109
M 77
n 110
N 78
o 111
O 79
p 112
P 80 q 113
Q 81 r 114
R 82 s 115
S 83 t 116
T 84 u 117
U 85 v 118
V 86 w 119
W 87 x 120
X 88 y 121
Y 89 z 122
Int.c sum.c div.c

 Interview question
ENUM variable

• Enum keyword is used


• User defined data type
• Enum
week{Sunday,monday,tuesday,wednesday,thursday,friday,saturday};
• Week –identifier
• Values will print of Sunday=0
• Used to assign names to integral constant because names are easier to
handle
#include<stdio.h>
Enum week{Sunday , m,t,w,th,f,sat};

Int main()
{
Enum week today;
Today=Wednesday;
Printf(“day %d”,today+1);
}
Why do we nedd enum when we can use #define to assign names to integral
constants

• Enum can be declared in the local scope


• Enum names are automatically initialized by the compiler.
Exercise:
#include <stdio.h>
enum day {sunday = 1, tuesday, wednesday, thursday, friday, saturday};

int main()
{
enum day d = thursday;
printf("The day number stored in d is %d", d);
return 0;
}
#include <stdio.h>
enum State {WORKING = 0, FAILED, FREEZED};
enum State currState = 2;

enum State FindState() {


return currState;
}

int main() {
(FindState() == WORKING)? printf("WORKING"): printf("NOT
WORKING");
return 0;
}
enum state {working, failed};
enum result {failed, passed};

int main() { return 0; }

Output:
Compile Error: 'failed' has a previous declaration
as 'state failed'
Write a program to check is a number is divisible by 2 or not
Tell whether the no. is even or odd
1)What is a variable and what is meant by the “value” of a variable?
2)How do variables and symbolic names differ?
3)State the difference between the declaration of a variable and the definition of
symbolic name
4)What are enumeration variables? How are they declared? what is advantage of
using them in a program?
5) Write a C program to input an integer and print its table
6)Write a c program to print the square of a number.
Interview questions

• Is it possible to declare an identifier that starts with an underscore.


• Is it possible to declare an identifier that ends with an underscore.
• What is the return type of print function?
• What is the difference between declaring and defining a variable?
• What is return type of scanf function?

You might also like