0% found this document useful (0 votes)
11 views3 pages

Getting Started With C

c programming

Uploaded by

emmanuelmututa25
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)
11 views3 pages

Getting Started With C

c programming

Uploaded by

emmanuelmututa25
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/ 3

C Programming Basics Tutorial

Numeric and Character Types

1. Numeric Types

Integers: Whole numbers without a decimal point.

Type: int
Format specifier: %d
Example:

int studentAge = 21;


printf("Student age: %d\n", studentAge);

Floating Point Numbers: Numbers with decimal places.

float : Precision up to 6-9 decimal places.


Format specifier: %f
Example:

float studentHeight = 67.3;


printf("Student height: %.2f\n", studentHeight); // Output limited to 2 decimal places

double : Double precision floating-point numbers with 15-16 decimal places.


Format specifier: %lf
Example:

double studentWeight = 55.82;


printf("Student weight: %.2lf\n", studentWeight);

2. Character Types

Character: Single characters, like 'A', 'B', or '5'.

Type: char
Format specifier: %c
Example:

char grade = 'A';


printf("Grade: %c\n", grade);

Strings: Arrays of characters, ending with a null terminator ( '\0' ).

Example:

char name[7] = "Justin"; // Interpreted as {'J', 'u', 's', 't', 'i', 'n', '\0'}
printf("Name: %s\n", name);

Arrays in C
An array is a collection of data elements of the same type stored at contiguous memory locations.

Syntax

dataType arrayName[number_of_elements] = {element1, element2, ...};

Example

int ages[5] = {21, 12, 14, 16, 22}; // Array of integers

Accessing and Modifying Arrays


You can access elements using indices, starting from 0.

ages[2] = 21; // Change the value of the 3rd element (index 2)

Example:

int ages[] = {19, 18, 12, 22, 33};


printf("%d\n", ages[2]); // Output: 12 (third element)

Character Arrays (Strings)

Strings are arrays of characters, but with a null character ( '\0' ) at the end to indicate the termination of the string.

char name[7] = "Justin"; // Equivalent to {'J', 'u', 's', 't', 'i', 'n', '\0'}
printf("%s\n", name); // Output: Justin

Input and Output Functions

1. printf() : Print Data to the Console

printf() is used to output data to the console.

Syntax:

printf("formatted string", variable);

Example:

printf("Student age: %d\n", studentAge);

Specifier Type

%d or %i Integer

%f Float

%lf Double

%c Character

%s String

2. scanf() : Reading Data from the Console

scanf() is used to read input from the user.

Syntax:

scanf("format specifier", &variable);

Example:

int numberOfCars;
printf("Enter number of cars: ");
scanf("%d", &numberOfCars); // Read integer from the user

Complete Example Code:


#include <stdio.h>

int main() {
int studentAge = 21;
float studentHeight = 67.3;
double studentWeight = 55.82;

int ages[] = {19, 18, 12, 22, 33};


ages[2] = 21; // Modify array element

char name[7] = "Justin";

printf("Student age: %d\n", studentAge);


printf("Student height: %.2f\n", studentHeight);
printf("Student weight: %.2lf\n", studentWeight);

printf("Updated age in array: %d\n", ages[2]);


printf("Student name: %s\n", name);

// Reading input
char carName[15];
printf("\nEnter car name: ");
scanf("%s", carName); // Read string input
printf("Car name: %s\n", carName);

int numberOfCars;
printf("Enter number of cars: ");
scanf("%d", &numberOfCars); // Read integer input
printf("Number of cars: %d\n", numberOfCars);

return 0;
}

Key Concepts Recap:

printf() : Used for output.


scanf() : Used for input.
int , float , double , char : Data types for numbers and characters.
Arrays: Collections of variables of the same type.

You might also like