0% found this document useful (0 votes)
5 views10 pages

C Program to Add Two Integers

The document provides multiple C programming examples, including a program to add two integers, usage of structures to store information about people, and the use of typedef for creating aliases for structures. It also discusses the inclusion of standard libraries like stdio.h and conio.h, explaining their purpose and the reasons to avoid conio.h in modern programming. Additionally, it covers string manipulation within structures and demonstrates how to initialize and access structure members.

Uploaded by

vattag22
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)
5 views10 pages

C Program to Add Two Integers

The document provides multiple C programming examples, including a program to add two integers, usage of structures to store information about people, and the use of typedef for creating aliases for structures. It also discusses the inclusion of standard libraries like stdio.h and conio.h, explaining their purpose and the reasons to avoid conio.h in modern programming. Additionally, it covers string manipulation within structures and demonstrates how to initialize and access structure members.

Uploaded by

vattag22
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

C Program to Add Two Integers

#include <stdio.h> // Include the standard input-output library

int main() {
// Declare integer variables to store the two numbers and
their sum
int num1, num2, sum;

// Prompt the user to enter the first number


printf("Enter the first integer: ");
// Read the first integer from the user and store it in num1
scanf("%d", &num1);

// Prompt the user to enter the second number


printf("Enter the second integer: ");
// Read the second integer from the user and store it in num2
scanf("%d", &num2);

// Calculate the sum of num1 and num2 and store it in the sum
variable
sum = num1 + num2;

// Display the result, showing the two numbers and their sum
printf("The sum of %d and %d is %d\n", num1, num2, sum);

// Indicate successful program execution


return 0;
}

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

int number1, number2, sum;


printf("Enter two integers: ");
scanf("%d %d", &number1,
&number2);

// calculate the sum


sum = number1 + number2;
printf("%d + %d = %d", number1,
number2, sum);
return 0;
}

C program

#include <stdio.h>

This line is a preprocessor directive that instructs the C compiler to include the contents of
the stdio.h (Standard Input/Output) header file into the program. This header file contains
declarations for standard input and output functions, such as printf() which is used later in
the program.
C
#include <conio.h>

This line is another preprocessor directive that includes the conio.h (Console Input/Output)
header file. This header file is specific to some compilers (like Turbo C/C++) and provides
functions for console input/output, such as getch(), clrscr(), etc. While not strictly necessary
for this "Hello World!" program, it is often included in older C programs.
C
int main()

This line declares the main() function. In C, program execution always begins from
the main() function. The int before main indicates that the function will return an integer
value to the operating system upon completion.
C
{

This opening curly brace marks the beginning of the main() function's body, where the
program's instructions are located.
C
printf("Hello World!");

This line calls the printf() function, which is a standard library function used to print formatted
output to the console. In this case, it prints the string literal "Hello World!" to the screen.
C
return 0;

This statement indicates the exit status of the program. Returning 0 from main() conventionally
signifies that the program executed successfully without any errors.
C
}

This closing curly brace marks the end of the main() function's body.

CONIO.H

Use #include <conio.h> for console input/output functions on specific, older systems like MS-
DOS or Turbo C/C++ that are not part of the standard C/C++ libraries. It provides functions
like getch() (read a character without echo) and clrscr() (clear the screen) but is not
recommended for modern, portable development due to its non-standard, platform-specific
nature.
Reasons to use <conio.h>
 Older compilers:
You might be using an older compiler like Turbo C/C++ that supports it, or working on an MS-DOS
environment.
 Specific console functions:
You need functions for direct console manipulation that are not available in standard libraries, such as:
 getch(): Reads a single character from the keyboard without displaying it on the screen.
 getche(): Reads a single character from the keyboard and displays it on the screen.
 clrscr(): Clears the console screen.
 kbhit(): Checks if a keyboard key has been pressed.
Reasons to avoid <conio.h>
 Non-standard and non-portable:
<conio.h> is not part of the C or C++ standard libraries, meaning code using it may not compile or run on
modern systems or compilers like GCC or Visual Studio.
 Platform-specific:
It was primarily used in MS-DOS and targets Microsoft environments, making your code less portable.
 Obsolete:
For most modern applications, standard I/O functions (like printf and scanf) and other standard library
functions provide more portable and maintainable ways to handle console operations.
 Modern alternatives:
To achieve similar functionality, you should use platform-independent libraries or methods instead of relying
on <conio.h>.

#include <stdio.h>

#include <conio.h> // Include conio.h for console functions

int main() {

clrscr(); // Clears the console screen (conio.h function)

printf("Hello from conio.h example!\n");

printf("Press any key to exit...\n");

getch(); // Waits for a key press without displaying it (conio.h

function)

return 0;

Output:
Upon running this program in a compatible environment (like Turbo C/C++), the console screen
will first be cleared, then the message "Hello from conio.h example!" will be displayed, followed
by "Press any key to exit...". The program will then pause, waiting for a key press. Once a key is
pressed, the program will terminate.
Note: If attempting to compile this code with a modern compiler, it will likely result in a
compilation error due to the absence of <conio.h> and its functions in the standard
libraries. For modern C/C++ development, alternative methods for console control
(e.g., system("cls") for clearing the screen, getchar() for input, or platform-specific APIs)
are typically used.

Structure in C
In C programming, a structure is a user-defined data type that groups together
variables of different data types under a single name. This name, often referred to as
the structure tag or structure name, serves as a blueprint for creating variables of that
structure type.
Assigning a Name to a Structure:
You assign a name to a structure when you define it using the struct keyword.
Example:

#include <stdio.h>
#include <string.h> // Required for strcpy()

// Defining a structure named 'Book'


struct Book {
char title[100];
char author[50];
int pages;
};

int main() {
// Declaring a variable of type 'struct Book'
struct Book myBook;

// Assigning values to the structure members


strcpy([Link], "The Great Gatsby");
strcpy([Link], "F. Scott Fitzgerald");
[Link] = 180;

// Accessing and displaying individual members


printf("Title: %s\n", [Link]);
printf("Author: %s\n", [Link]);
printf("Pages: %d\n", [Link]);

return 0;
}

Explanation:
 struct Book { ... };:
Here, Book is the name (or tag) assigned to the structure. This name allows you to
define variables of this specific structure type later.
 struct Book myBook;:
In main(), myBook is declared as a variable of the struct Book type.
 strcpy([Link], "The Great Gatsby");:
To assign a value to a string member like title, strcpy() is used because direct
assignment with = is not allowed for character arrays after declaration.
 [Link] = 180;:
For integer or other non-array members, values are assigned directly using the
member access operator (.).
 printf("Title: %s\n", [Link]);:
The members of the structure variable
([Link], [Link], [Link]) are accessed using the dot
operator and their values are printed.

Typedef in C

The typedef keyword is used with struct in C to create an alias (a new


name) for the structure type. This offers several benefits, primarily related
to convenience and code readability.
Reasons for using typedef with struct:
 Eliminating the struct keyword: In C, when declaring variables of a structure type, you
typically need to prefix the structure's tag name with the struct keyword (e.g., struct
MyStruct myVariable;). Using typedef allows you to define a new type name that
directly refers to the structure, eliminating the need to write struct every time.
C
// Without typedef
struct Point {
int x;
int y;
};
struct Point p1; // Must use 'struct Point'

// With typedef
typedef struct {
int x;
int y;
} Point;
Point p2; // Can use 'Point' directly
#include <stdio.h>

// Without typedef
struct Point_NoTypedef {
int x;
int y;
};

// With typedef
typedef struct Point_Typedef {
int x;
int y;
} Point; // 'Point' is now an alias for 'struct Point_Typedef'

int main() {
// Declaring a variable without typedef:
struct Point_NoTypedef p1;
p1.x = 10;
p1.y = 20;
printf("Point 1 (no typedef): (%d, %d)\n", p1.x, p1.y);

// Declaring a variable with typedef:


Point p2; // No need to write 'struct Point_Typedef'
p2.x = 30;
p2.y = 40;
printf("Two points x and y are (with typedef): (%d, %d)\n",
p2.x, p2.y);

return 0;
}
Q: i need to store information of more than one person, how can i do using structure in c,
give complete examples.(using loop)
Here is a complete example:

#include <stdio.h>
#include <string.h>

// Define the structure for a person


struct Person {
int id;
char name[50];
int age;
};

int main() {
// Define the number of people to store
int num_people = 3;

// Declare an array of structures to hold data for multiple people


struct Person people[num_people];

// Input details for each person


printf("Enter details for %d people:\n", num_people);
for (int i = 0; i < num_people; i++) {
printf("\nPerson %d:\n", i + 1);

// Get ID
printf("ID: ");
scanf("%d", &people[i].id);

// Get Name (use "%*c" to consume the newline left by scanf)


printf("Name: ");
scanf("%*c%s", people[i].name); // Using %s for names without spaces
for simplicity

// Get Age
printf("Age: ");
scanf("%d", &people[i].age);
}

// Display details for each person


printf("\n--- Details of People ---\n");
for (int i = 0; i < num_people; i++) {
printf("Person %d:\n", i + 1);
printf(" ID: %d\n", people[i].id);
printf(" Name: %s\n", people[i].name);
printf(" Age: %d\n", people[i].age);
}

return 0;
}
Array

To store information for multiple people without using loops, you can declare an array of
structures and initialize each element directly.
Here is a complete example:

#include <stdio.h>
#include <string.h> // Required for strcpy

// Define the structure to hold person's information


struct Person {
char name[50];
int age;
float height; // in meters
};

int main() {
// Declare an array of Person structures to hold information for 3 people
struct Person people[3];

// Initialize the first person's data


strcpy(people[0].name, "Alice");
people[0].age = 30;
people[0].height = 1.65;

// Initialize the second person's data


strcpy(people[1].name, "Bob");
people[1].age = 25;
people[1].height = 1.80;

// Initialize the third person's data


strcpy(people[2].name, "Charlie");
people[2].age = 35;
people[2].height = 1.70;

// Display the information for each person


printf("Information for Person 1:\n");
printf("Name: %s\n", people[0].name);
printf("Age: %d\n", people[0].age);
printf("Height: %.2f m\n\n", people[0].height);

printf("Information for Person 2:\n");


printf("Name: %s\n", people[1].name);
printf("Age: %d\n", people[1].age);
printf("Height: %.2f m\n\n", people[1].height);

printf("Information for Person 3:\n");


printf("Name: %s\n", people[2].name);
printf("Age: %d\n", people[2].age);
printf("Height: %.2f m\n\n", people[2].height);

return 0;
}
For String
#include <stdio.h>
#include <string.h> // Required for strcpy()

struct myStructure {
int myNum;
char myLetter;
char myString[30]; // String declared as a character array
};

int main() {
struct myStructure s1;

// Correct way to assign a string to myString


strcpy([Link], "Some text");

printf("My string: %s\n", [Link]);

return 0;
}

Alternatively, you can initialize the string member at the time of structure variable declaration:
C
#include <stdio.h>

struct myStructure {
int myNum;
char myLetter;
char myString[30];
};

int main() {
// Initializing at declaration
struct myStructure s1 = {13, 'B', "Some text"};

printf("My string: %s\n", [Link]);

return 0;
}

You might also like