Programming With C-1
Programming With C-1
5 marks questions
Although C has stood the test of time and remains relevant, it is not without its challenges. Being a low-level language, C exposes
developers to potential pitfalls, such as buffer overflows and undefined behavior, which require careful coding practices and testing.
Despite these challenges, C continues to be a vital language in the software industry, and many modern languages and frameworks have
been influenced by its design principles and syntax. Learning C provides a solid foundation for understanding programming concepts and
working closer to the hardware.
3. Explain goto statement.
A goto statement in C programming language provides an unconditional jump from the goto to a labeled statement in the same
function.goto.
Syntax:
goto label.
label: statement.
The label can be any plain text except C keyword and also it can be set anywhere in the C
program above or below to goto statement
Statement 1
Label 1
Statement 2 Go to
Label 2 Lable 3
LAL
Statement 3 L
Lable 3
la
laato
Shows Flowchart of go to statement. ttttto t
Example:
#include <stdio.h>
int main ()
{
int x = 10;
LOOP: do
{
if( x == 15)
{
x = x + 1;
goto LOOP;
}
printf("value of x: %d\n", x);
x++;
}while( x < 20 );
return 0;
}
When the above code is compiled and executed, it produces the following result:
value of x: 10
value of x: 11
value of x: 12
value of x: 13
value of x: 14
value of x: 16
value of x: 17
value of x: 18
value of x: 19
# define P L 3.1412.
# define TRVE 1 Symbolic Constants
# define FALSE φ
Global Declarations
The variables are declared before the main ( ) function as well as user defined functions are called global variables. These global variables
can be accessed by all the user defined functions including main ( ) function.
The main ( ) function
Each and Every C program should contain only one main ( ). The C program execution starts with main ( ) function. No C program is
executed without the main function. The main ( ) function should be written in small (lowercase) letters and it should not be terminated
by semicolon. Main ( ) executes user defined program statements, library functions and user defined functions and all these statements
should be enclosed within left and right braces.
Braces
Every C program should have a pair of curly braces ({, }). The left braces indicates the beginning of the main ( ) function and the right
braces indicates the end of the main ( ) function.
These braces can also be used to indicate the user-defined functions beginning and ending. These two braces can also be used in compound
statements.
Local Declarations
The variable declaration is a part of C program and all the variables are used in main ( ) function
should be declared in the local declaration section is called local variables. Not only variables,
we can also declare arrays, functions, pointers etc. These variables can also be initialized with
basic data types.
197
For Example
Main ( )
{
int sum = 0;
int x;
float y;
}
Here, the variable sum is declared as integer variable and it is initialized to zero. Other variables declared as int and float and these
variables inside any function are called local variables.
Program statements.
These statements are building blocks of a program. They represent instructions to the computer to perform a specific task (operations).
An instruction may contain an input-output statements, arithmetic statements, control statements, simple assignment statements and any
other statements and it also includes comments that are enclosed within /* and */. The comment statements are not compiled and executed,
and each executable statement should be terminated with semicolon.
User defined functions
These are subprograms, generally, a subprogram is a function, and these functions are written by the user are called user, defined functions.
These functions are performed by user specific tasks, and this also contains set of program statements. They may be written before or
after a main () function and called within main () function. This is an option for programmers.
6. Explain arrays in C.
We belong to an era where it is impossible to long for a progressive world without Information Technology. And when
we talk about Information Technology, Computer Programming readily comes into our mind. An Array is one of the
essential facets of programming languages and software development. This is because an array is of great utility in
maintaining extensive datasets, sorting, and identifying variables.In its simplest terms, an array can be defined as a
formation of digits, images, or objects arranged in rows and columns in accordance with their types. Comprehending
the concept of array facilitates your tedious work and allows you to make it concise as arrays illustrate numerous data
elements of similar type using a sole name
In Computer Programming, an array assembles equivalent data elements stored at adjacent memory locations. This
easiest data structure helps to determine the location of each piece of data by simply using an offset to the base value.
An offset is a number that illustrates the distinction between two index numbers for instance, if you want to keep a
record of the marks obtained by a student in 6 subjects, then you don’t need to determine the variables individually.
Rather, you can determine an array that will store each piece of data factors at a contiguous memory location. Arrays
are one of the oldest and most essential data structures used in almost every program. Arrays are also useful in
implementing other data structures such as lists, heaps, hash tables,deques, queues, stacks, and strings.
In C, arrays have a strong relationship to pointers.Consider the following declaration.
int arr[ 10 ] ;
You might think from the above statement that arr is of type int. However, arr, by itself, without any index subscripting,
can be assigned to an integer pointer.Do you know, however, that arr[ i ] is defined using pointer arithmetic?
In particular:
arr[ i ] == * ( arr + i )
Let's take a closer look at arr + i. What does that mean? arr is a pointer to arr[ 0 ]. In fact, it is
defined to be & arr[ 0 ].
A pointer is an address in memory. arr contains an address in memory---the address.
where arr[ 0 ] is located.
What is arr + i? If arr is a pointer to arr[ 0 ] then arr + i is a pointer to arr[ i ].
Perhaps this is easier shown in a diagram.
We assume that each int takes up 4 bytes.
7. Briefly discuss assembly language.
Assembly language is easier to use than machine language. It is a middle level and named as second generation
programming language. An assembler is useful for detecting programming errors. Programmers do not have the
absolute address of data items. Assembly language encourage modular programming. It uses mnemonic codes instead
of bit patterns for instructions. The programs written are machine dependent. Here assemblers are used to translate
assembly language code to machine language.Example for assembly language program:
Move a,b
Add c
Store b
Advantages of Assembly language:
It is easily understood by humans because it uses statements instead of binary digits.
To develop a program takes less time.
Debugging is easy.
Disadvantages of Assembly language:
It is a machine dependent language, hence the program designed for one machine is
not usable for another machine. Say the assembly code for microprocessor 8085 is
different from that of microprocessor 8086.
Programmers should learn the mnemonic codes of each machine.
8. Explain if-else statement.
If-Else statement:
The If-else statement is a two-way branching statement. It consists of two blocks of statements if block and else block enclosed inside the
If-else statement. If the condition inside statement is true, statements inside if block are executed, otherwise statements inside the else
block are executed. Else block is optional, and it may be absent in a program.
Start
Expression ----false---------------
True
stop
10 Marks
This function is defined in the stdio.h header file and is used to show output on the console (standard output).Following is how the printf()
function is defined in the C stdio.h library.
Copy
It writes the C string pointed by the format pointer to the standard output (stdout).
This function is used to print a simple text sentence or value of any variable which can be of int, char, float, or any other datatype.
1. Print a sentence
int main() {
// using printf()
printf("Welcome to Studytonight");
return 0;
Copy
Welcome to Studytonight
To understand the complete code and structure of a basic C language program, check Hello
int age;
printf("Enter your age and then gender(M, F or O): ");
scanf("%d %c", &age, &gender);
printf("You entered: %d and %c", age, gender);
return 0;
}
Output:
Enter your age and then gender(M, F or O): 32 M
You entered: 32 and M
Return Value of printf() & scanf()
The printf() function returns the number of characters printed by it,
and scanf() function returns the number of characters read by it.
int i = printf("studytonight");
printf("Value of i is: %d", i);
studytonightValue of i is: 12
In this program printf("studytonight"); will return 12 as a result, which will be stored in the variable i, because the word studytonight
has 12 characters.
The first printf() statement will print the statement studytonight on the output too.
15 Marks
1. Explain algorithm with example.
2. Discuss principles of graphical representation.
3. Elucidate declaration and initialization of strings.
4. With example explain getchar() and putchar() function.
5. Briefly explain compilation process.
6. Explain relational and logical operators
7. Discuss user-defined function in detail.
8. Describe Calloc() method.
9. Describe structured programming.
10. Explain bitwise operators and arithmetic operators.
11. Discuss user-defined function in detail.
12. Briefly explain malloc() method.