0% found this document useful (0 votes)
7 views4 pages

C Programming Basics

The document provides an overview of basic C programming concepts and includes example programs for tasks such as printing text, adding numbers, calculating the area of a circle, and converting temperatures. It also covers format specifiers, data types, input methods, and operators. This resource is particularly useful for MSc Physics students learning C programming.

Uploaded by

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

C Programming Basics

The document provides an overview of basic C programming concepts and includes example programs for tasks such as printing text, adding numbers, calculating the area of a circle, and converting temperatures. It also covers format specifiers, data types, input methods, and operators. This resource is particularly useful for MSc Physics students learning C programming.

Uploaded by

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

C Programming Basics and Concepts

1. Basic C Program to Print "Hello World!":

#include <stdio.h>

int main() {

printf("Hello World!\n");

printf("Welcome to C programming.\n");

return 0;

2. More Simple C Programs for MSc Physics:

a) Add Two Numbers:

#include <stdio.h>

int main() {

int a, b, sum;

printf("Enter two numbers: ");

scanf("%d %d", &a, &b);

sum = a + b;

printf("Sum: %d", sum);

return 0;

b) Area of Circle:

#include <stdio.h>

int main() {

float radius, area;

printf("Enter radius: ");

scanf("%f", &radius);

area = 3.14 * radius * radius;

printf("Area: %.2f", area);

Page 1
C Programming Basics and Concepts

return 0;

c) Temperature Conversion (Celsius to Fahrenheit):

#include <stdio.h>

int main() {

float celsius, fahrenheit;

printf("Enter Celsius: ");

scanf("%f", &celsius);

fahrenheit = (celsius * 9/5) + 32;

printf("Fahrenheit: %.2f", fahrenheit);

return 0;

3. Format Specifiers:

%d - Integer

%c - Character

%f - Float

%lf - Double

%.2lf - Double with 2 decimal points

%.10lf - Double with 10 decimal points

4. Variables and Data Types:

int - Integer (e.g., 10)

char - Character (e.g., 'A')

float - Floating point (e.g., 3.14)

double - Double precision (e.g., 3.14159265)

5. scanf - Taking Input:

scanf("format specifier", &variable);

Page 2
C Programming Basics and Concepts

Example:

int num;

printf("Enter number: ");

scanf("%d", &num);

printf("You entered: %d", num);

6. Example Programs:

a) Print a Character:

#include <stdio.h>

int main() {

char ch;

printf("Enter character: ");

scanf(" %c", &ch);

printf("Character: %c", ch);

return 0;

b) Sum of Two Numbers:

#include <stdio.h>

int main() {

int x, y;

printf("Enter two numbers: ");

scanf("%d %d", &x, &y);

printf("Sum: %d", x + y);

return 0;

c) Floating Point Example:

#include <stdio.h>

int main() {

Page 3
C Programming Basics and Concepts

float x, y;

printf("Enter two float numbers: ");

scanf("%f %f", &x, &y);

printf("Sum: %.2f", x + y);

return 0;

7. Escape Sequence:

\n - New line (used for line break)

8. Arithmetic and Logical Operators in C:

Arithmetic: +, -, *, /, %

Logical: && (AND), || (OR), ! (NOT)

Relational: <, >, <=, >=, ==, !=

9. Example - Sum, Average, Product:

#include <stdio.h>

int main() {

int a, b, c;

printf("Enter three numbers: ");

scanf("%d %d %d", &a, &b, &c);

printf("Sum: %d\n", a + b + c);

printf("Average: %.2f\n", (a + b + c)/3.0);

printf("Product: %d\n", a * b * c);

return 0;

Conclusion:

This PDF contains basic C programs and concepts useful for MSc Physics students, covering variables,

input/output, operators, and format specifiers.

Page 4

You might also like